Merge branch 'master' into 3140-project-content-tabs
[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 distinct.count < links.count, "distinct count should be less than link count"
22     assert_equal links.uniq.count, distinct.count
23   end
24
25   test "select with order" do
26     get "/arvados/v1/links", {:format => :json, :select => ['uuid'], :order => ["uuid asc"]}, auth(:active)
27     assert_response :success
28
29     assert json_response['items'].length > 0
30
31     p = ""
32     json_response['items'].each do |i|
33       assert i['uuid'] > p
34       p = i['uuid']
35     end
36   end
37
38   def assert_link_classes_ascend(current_class, prev_class)
39     # Databases and Ruby don't always agree about string ordering with
40     # punctuation.  If the strings aren't ascending normally, check
41     # that they're equal up to punctuation.
42     if current_class < prev_class
43       class_prefix = current_class.split(/\W/).first
44       assert prev_class.start_with?(class_prefix)
45     end
46   end
47
48   test "select two columns with order" do
49     get "/arvados/v1/links", {:format => :json, :select => ['link_class', 'uuid'], :order => ['link_class asc', "uuid desc"]}, auth(:active)
50     assert_response :success
51
52     assert json_response['items'].length > 0
53
54     prev_link_class = ""
55     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
56
57     json_response['items'].each do |i|
58       if prev_link_class != i['link_class']
59         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
60       end
61
62       assert_link_classes_ascend(i['link_class'], prev_link_class)
63       assert i['uuid'] < prev_uuid
64
65       prev_link_class = i['link_class']
66       prev_uuid = i['uuid']
67     end
68   end
69
70   test "select two columns with old-style order syntax" do
71     get "/arvados/v1/links", {:format => :json, :select => ['link_class', 'uuid'], :order => 'link_class asc, uuid desc'}, auth(:active)
72     assert_response :success
73
74     assert json_response['items'].length > 0
75
76     prev_link_class = ""
77     prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
78
79     json_response['items'].each do |i|
80       if prev_link_class != i['link_class']
81         prev_uuid = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
82       end
83
84       assert_link_classes_ascend(i['link_class'], prev_link_class)
85       assert i['uuid'] < prev_uuid
86
87       prev_link_class = i['link_class']
88       prev_uuid = i['uuid']
89     end
90   end
91
92 end