17417: Add arm64 packages for our Golang components.
[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'], :distinct => false},
21       headers: auth(:active)
22     assert_response :success
23     links = json_response['items']
24
25     get "/arvados/v1/links",
26       params: {:format => :json, :select => ['link_class'], :distinct => true},
27       headers: auth(:active)
28     assert_response :success
29     distinct = json_response['items']
30
31     assert_operator(distinct.count, :<, links.count,
32                     "distinct count should be less than link count")
33     assert_equal links.uniq.count, distinct.count
34   end
35
36   test "select with order" do
37     get "/arvados/v1/links",
38       params: {:format => :json, :select => ['uuid'], :order => ["uuid asc"]},
39       headers: auth(:active)
40     assert_response :success
41
42     assert json_response['items'].length > 0
43
44     p = ""
45     json_response['items'].each do |i|
46       assert i['uuid'] > p
47       p = i['uuid']
48     end
49   end
50
51   test "select with default order" do
52     get "/arvados/v1/links",
53       params: {format: :json, select: ['uuid']},
54       headers: auth(:admin)
55     assert_response :success
56     uuids = json_response['items'].collect { |i| i['uuid'] }
57     assert_equal uuids, uuids.sort
58   end
59
60   def assert_link_classes_ascend(current_class, prev_class)
61     # Databases and Ruby don't always agree about string ordering with
62     # punctuation.  If the strings aren't ascending normally, check
63     # that they're equal up to punctuation.
64     if current_class < prev_class
65       class_prefix = current_class.split(/\W/).first
66       assert prev_class.start_with?(class_prefix)
67     end
68   end
69
70   test "select two columns with order" do
71     get "/arvados/v1/links",
72       params: {
73         :format => :json,
74         :select => ['link_class', 'uuid'], :order => ['link_class asc', "uuid desc"]
75       },
76       headers: auth(:active)
77     assert_response :success
78
79     assert json_response['items'].length > 0
80
81     prev_link_class = ""
82     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
83
84     json_response['items'].each do |i|
85       if prev_link_class != i['link_class']
86         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
87       end
88
89       assert_link_classes_ascend(i['link_class'], prev_link_class)
90       assert i['uuid'] < prev_uuid
91
92       prev_link_class = i['link_class']
93       prev_uuid = i['uuid']
94     end
95   end
96
97   test "select two columns with old-style order syntax" do
98     get "/arvados/v1/links",
99       params: {
100         :format => :json,
101         :select => ['link_class', 'uuid'], :order => 'link_class asc, uuid desc'
102       },
103       headers: auth(:active)
104     assert_response :success
105
106     assert json_response['items'].length > 0
107
108     prev_link_class = ""
109     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
110
111     json_response['items'].each do |i|
112       if prev_link_class != i['link_class']
113         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
114       end
115
116       assert_link_classes_ascend(i['link_class'], prev_link_class)
117       assert i['uuid'] < prev_uuid
118
119       prev_link_class = i['link_class']
120       prev_uuid = i['uuid']
121     end
122   end
123
124 end