1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
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
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']
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']
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']
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
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
50 assert json_response['items'].length > 0
53 json_response['items'].each do |i|
59 test "select with default order" do
60 get "/arvados/v1/links",
61 params: {format: :json, select: ['uuid']},
63 assert_response :success
64 uuids = json_response['items'].collect { |i| i['uuid'] }
65 assert_equal uuids, uuids.sort.reverse
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)
78 test "select two columns with order" do
79 get "/arvados/v1/links",
82 :select => ['link_class', 'uuid'], :order => ['link_class asc', "uuid desc"]
84 headers: auth(:active)
85 assert_response :success
87 assert json_response['items'].length > 0
90 prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
92 json_response['items'].each do |i|
93 if prev_link_class != i['link_class']
94 prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
97 assert_link_classes_ascend(i['link_class'], prev_link_class)
98 assert i['uuid'] < prev_uuid
100 prev_link_class = i['link_class']
101 prev_uuid = i['uuid']
105 test "select two columns with old-style order syntax" do
106 get "/arvados/v1/links",
109 :select => ['link_class', 'uuid'], :order => 'link_class asc, uuid desc'
111 headers: auth(:active)
112 assert_response :success
114 assert json_response['items'].length > 0
117 prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
119 json_response['items'].each do |i|
120 if prev_link_class != i['link_class']
121 prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
124 assert_link_classes_ascend(i['link_class'], prev_link_class)
125 assert i['uuid'] < prev_uuid
127 prev_link_class = i['link_class']
128 prev_uuid = i['uuid']