Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / test / unit / arvados_resource_list_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 ResourceListTest < ActiveSupport::TestCase
8
9   reset_api_fixtures :after_each_test, false
10
11   test 'links_for on a resource list that does not return links' do
12     use_token :active
13     results = Specimen.all
14     assert_equal [], results.links_for(api_fixture('users')['active']['uuid'])
15   end
16
17   test 'get all items by default' do
18     use_token :admin
19     a = 0
20     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').each do
21       a += 1
22     end
23     assert_equal 201, a
24   end
25
26   test 'prefetch all items' do
27     use_token :admin
28     a = 0
29     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').each do
30       a += 1
31     end
32     assert_equal 201, a
33   end
34
35   test 'get limited items' do
36     use_token :admin
37     a = 0
38     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').limit(51).each do
39       a += 1
40     end
41     assert_equal 51, a
42   end
43
44   test 'get limited items, limit % page_size != 0' do
45     skip "Requires server MAX_LIMIT < 200 which is not currently the default"
46
47     use_token :admin
48     max_page_size = Collection.
49       where(owner_uuid: 'zzzzz-j7d0g-0201collections').
50       limit(1000000000).
51       fetch_multiple_pages(false).
52       count
53     # Conditions necessary for this test to be valid:
54     assert_operator 200, :>, max_page_size
55     assert_operator 1, :<, max_page_size
56     # Verify that the server really sends max_page_size when asked for max_page_size+1
57     assert_equal max_page_size, Collection.
58       where(owner_uuid: 'zzzzz-j7d0g-0201collections').
59       limit(max_page_size+1).
60       fetch_multiple_pages(false).
61       results.
62       count
63     # Now that we know the max_page_size+1 is in the middle of page 2,
64     # make sure #each returns page 1 and only the requested part of
65     # page 2.
66     a = 0
67     saw_uuid = {}
68     Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').limit(max_page_size+1).each do |item|
69       a += 1
70       saw_uuid[item.uuid] = true
71     end
72     assert_equal max_page_size+1, a
73     # Ensure no overlap between pages
74     assert_equal max_page_size+1, saw_uuid.size
75   end
76
77   test 'get single page of items' do
78     use_token :admin
79     a = 0
80     c = Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').fetch_multiple_pages(false)
81     c.each do
82       a += 1
83     end
84
85     assert_operator a, :<, 201
86     assert_equal c.result_limit, a
87   end
88
89   test 'get empty set' do
90     use_token :admin
91     c = Collection.
92       where(owner_uuid: 'doesn-texis-tdoesntexistdoe').
93       fetch_multiple_pages(false)
94     # Important: check c.result_offset before calling c.results here.
95     assert_equal 0, c.result_offset
96     assert_equal 0, c.items_available
97     assert_empty c.results
98   end
99
100   test 'count=none' do
101     use_token :active
102     c = Collection.with_count('none')
103     assert_nil c.items_available
104     refute_empty c.results
105   end
106
107   test 'cache results across each(&block) calls' do
108     use_token :admin
109     c = Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').with_count('none')
110     c.each do |x|
111       x.description = 'foo'
112     end
113     found = 0
114     c.each do |x|
115       found += 1
116       # We should get the same objects we modified in the loop above
117       # -- not new objects built from another set of API responses.
118       assert_equal 'foo', x.description
119     end
120     assert_equal 201, found
121   end
122 end