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