8784: Fix test for latest firefox.
[arvados.git] / services / api / test / integration / select_test.rb
1 require 'test_helper'
2
3 class SelectTest < ActionDispatch::IntegrationTest
4   test "should select just two columns" do
5     get "/arvados/v1/links", {:format => :json, :select => ['uuid', 'link_class']}, auth(:active)
6     assert_response :success
7     assert_equal json_response['items'].count, json_response['items'].select { |i|
8       i.count == 3 and i['uuid'] != nil and i['link_class'] != nil
9     }.count
10   end
11
12   test "fewer distinct than total count" do
13     get "/arvados/v1/links", {:format => :json, :select => ['link_class'], :distinct => false}, auth(:active)
14     assert_response :success
15     links = json_response['items']
16
17     get "/arvados/v1/links", {:format => :json, :select => ['link_class'], :distinct => true}, auth(:active)
18     assert_response :success
19     distinct = json_response['items']
20
21     assert_operator(distinct.count, :<, links.count,
22                     "distinct count should be less than link count")
23     assert_equal links.uniq.count, distinct.count
24   end
25
26   test "select with order" do
27     get "/arvados/v1/links", {:format => :json, :select => ['uuid'], :order => ["uuid asc"]}, auth(:active)
28     assert_response :success
29
30     assert json_response['items'].length > 0
31
32     p = ""
33     json_response['items'].each do |i|
34       assert i['uuid'] > p
35       p = i['uuid']
36     end
37   end
38
39   test "select with default order" do
40     get "/arvados/v1/links", {format: :json, select: ['uuid']}, auth(:admin)
41     assert_response :success
42     uuids = json_response['items'].collect { |i| i['uuid'] }
43     assert_equal uuids, uuids.sort
44   end
45
46   def assert_link_classes_ascend(current_class, prev_class)
47     # Databases and Ruby don't always agree about string ordering with
48     # punctuation.  If the strings aren't ascending normally, check
49     # that they're equal up to punctuation.
50     if current_class < prev_class
51       class_prefix = current_class.split(/\W/).first
52       assert prev_class.start_with?(class_prefix)
53     end
54   end
55
56   test "select two columns with order" do
57     get "/arvados/v1/links", {:format => :json, :select => ['link_class', 'uuid'], :order => ['link_class asc', "uuid desc"]}, auth(:active)
58     assert_response :success
59
60     assert json_response['items'].length > 0
61
62     prev_link_class = ""
63     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
64
65     json_response['items'].each do |i|
66       if prev_link_class != i['link_class']
67         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
68       end
69
70       assert_link_classes_ascend(i['link_class'], prev_link_class)
71       assert i['uuid'] < prev_uuid
72
73       prev_link_class = i['link_class']
74       prev_uuid = i['uuid']
75     end
76   end
77
78   test "select two columns with old-style order syntax" do
79     get "/arvados/v1/links", {:format => :json, :select => ['link_class', 'uuid'], :order => 'link_class asc, uuid desc'}, auth(:active)
80     assert_response :success
81
82     assert json_response['items'].length > 0
83
84     prev_link_class = ""
85     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
86
87     json_response['items'].each do |i|
88       if prev_link_class != i['link_class']
89         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
90       end
91
92       assert_link_classes_ascend(i['link_class'], prev_link_class)
93       assert i['uuid'] < prev_uuid
94
95       prev_link_class = i['link_class']
96       prev_uuid = i['uuid']
97     end
98   end
99
100 end