Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[arvados.git] / apps / workbench / test / unit / arvados_resource_list_test.rb
1 require 'test_helper'
2
3 class ResourceListTest < ActiveSupport::TestCase
4
5   test 'links_for on a resource list that does not return links' do
6     use_token :active
7     results = Specimen.all
8     assert_equal [], results.links_for(api_fixture('users')['active']['uuid'])
9   end
10
11   test 'get all items by default' do
12     use_token :admin
13     a = 0
14     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').each do
15       a += 1
16     end
17     assert_equal 201, a
18   end
19
20   test 'prefetch all items' do
21     use_token :admin
22     a = 0
23     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').each do
24       a += 1
25     end
26     assert_equal 201, a
27   end
28
29   test 'get limited items' do
30     use_token :admin
31     a = 0
32     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').limit(51).each do
33       a += 1
34     end
35     assert_equal 51, a
36   end
37
38   test 'get limited items, limit % page_size != 0' do
39     skip "Requires server MAX_LIMIT < 200 which is not currently the default"
40
41     use_token :admin
42     max_page_size = Collection.
43       where(owner_uuid: 'zzzzz-j7d0g-0201collections').
44       limit(1000000000).
45       fetch_multiple_pages(false).
46       count
47     # Conditions necessary for this test to be valid:
48     assert_operator 200, :>, max_page_size
49     assert_operator 1, :<, max_page_size
50     # Verify that the server really sends max_page_size when asked for max_page_size+1
51     assert_equal max_page_size, Collection.
52       where(owner_uuid: 'zzzzz-j7d0g-0201collections').
53       limit(max_page_size+1).
54       fetch_multiple_pages(false).
55       results.
56       count
57     # Now that we know the max_page_size+1 is in the middle of page 2,
58     # make sure #each returns page 1 and only the requested part of
59     # page 2.
60     a = 0
61     saw_uuid = {}
62     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').limit(max_page_size+1).each do |item|
63       a += 1
64       saw_uuid[item.uuid] = true
65     end
66     assert_equal max_page_size+1, a
67     # Ensure no overlap between pages
68     assert_equal max_page_size+1, saw_uuid.size
69   end
70
71   test 'get single page of items' do
72     use_token :admin
73     a = 0
74     c = Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').fetch_multiple_pages(false)
75     c.each do
76       a += 1
77     end
78
79     assert_operator a, :<, 201
80     assert_equal c.result_limit, a
81   end
82
83   test 'get empty set' do
84     use_token :admin
85     c = Collection.
86       where(owner_uuid: 'doesn-texis-tdoesntexistdoe').
87       fetch_multiple_pages(false)
88     # Important: check c.result_offset before calling c.results here.
89     assert_equal 0, c.result_offset
90     assert_equal 0, c.items_available
91     assert_empty c.results
92   end
93
94 end