18943: Fix tests
[arvados.git] / services / api / test / integration / select_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class SelectTest < ActionDispatch::IntegrationTest
8   test "should select just two columns" do
9     get "/arvados/v1/links",
10       params: {:format => :json, :select => ['uuid', 'link_class']},
11       headers: auth(:active)
12     assert_response :success
13     assert_equal json_response['items'].count, json_response['items'].select { |i|
14       i.count == 3 and i['uuid'] != nil and i['link_class'] != nil
15     }.count
16   end
17
18   test "fewer distinct than total count" do
19     get "/arvados/v1/links",
20       params: {:format => :json, :select => ['link_class']},
21       headers: auth(:active)
22     assert_response :success
23     distinct_unspecified = json_response['items']
24
25     get "/arvados/v1/links",
26       params: {:format => :json, :select => ['link_class'], :distinct => false},
27       headers: auth(:active)
28     assert_response :success
29     distinct_false = json_response['items']
30
31     get "/arvados/v1/links",
32       params: {:format => :json, :select => ['link_class'], :distinct => true},
33       headers: auth(:active)
34     assert_response :success
35     distinct = json_response['items']
36
37     assert_operator(distinct.count, :<, distinct_false.count,
38                     "distinct=true count should be less than distinct=false count")
39     assert_equal(distinct_unspecified.count, distinct_false.count,
40                     "distinct=false should be the default")
41     assert_equal distinct_false.uniq.count, distinct.count
42   end
43
44   test "select with order" do
45     get "/arvados/v1/links",
46       params: {:format => :json, :select => ['uuid'], :order => ["uuid asc"]},
47       headers: auth(:active)
48     assert_response :success
49
50     assert json_response['items'].length > 0
51
52     p = ""
53     json_response['items'].each do |i|
54       assert i['uuid'] > p
55       p = i['uuid']
56     end
57   end
58
59   test "select with default order" do
60     get "/arvados/v1/links",
61       params: {format: :json, select: ['uuid']},
62       headers: auth(:admin)
63     assert_response :success
64     uuids = json_response['items'].collect { |i| i['uuid'] }
65     assert_equal uuids, uuids.sort.reverse
66   end
67
68   def assert_link_classes_ascend(current_class, prev_class)
69     # Databases and Ruby don't always agree about string ordering with
70     # punctuation.  If the strings aren't ascending normally, check
71     # that they're equal up to punctuation.
72     if current_class < prev_class
73       class_prefix = current_class.split(/\W/).first
74       assert prev_class.start_with?(class_prefix)
75     end
76   end
77
78   test "select two columns with order" do
79     get "/arvados/v1/links",
80       params: {
81         :format => :json,
82         :select => ['link_class', 'uuid'], :order => ['link_class asc', "uuid desc"]
83       },
84       headers: auth(:active)
85     assert_response :success
86
87     assert json_response['items'].length > 0
88
89     prev_link_class = ""
90     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
91
92     json_response['items'].each do |i|
93       if prev_link_class != i['link_class']
94         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
95       end
96
97       assert_link_classes_ascend(i['link_class'], prev_link_class)
98       assert i['uuid'] < prev_uuid
99
100       prev_link_class = i['link_class']
101       prev_uuid = i['uuid']
102     end
103   end
104
105   test "select two columns with old-style order syntax" do
106     get "/arvados/v1/links",
107       params: {
108         :format => :json,
109         :select => ['link_class', 'uuid'], :order => 'link_class asc, uuid desc'
110       },
111       headers: auth(:active)
112     assert_response :success
113
114     assert json_response['items'].length > 0
115
116     prev_link_class = ""
117     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
118
119     json_response['items'].each do |i|
120       if prev_link_class != i['link_class']
121         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
122       end
123
124       assert_link_classes_ascend(i['link_class'], prev_link_class)
125       assert i['uuid'] < prev_uuid
126
127       prev_link_class = i['link_class']
128       prev_uuid = i['uuid']
129     end
130   end
131
132 end