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