2871: add tests for preload_collections_for_objects helper method
[arvados.git] / apps / workbench / test / functional / application_controller_test.rb
1 require 'test_helper'
2
3 class ApplicationControllerTest < ActionController::TestCase
4
5   test "links for object" do
6     use_token :active
7
8     ac = ApplicationController.new
9
10     link_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
11
12     links = ac.send :links_for_object, link_head_uuid
13
14     assert links, 'Expected links'
15     assert links.is_a?(Array), 'Expected an array'
16     assert links.size > 0, 'Expected at least one link'
17     assert links[0][:uuid], 'Expected uuid for the head_link'
18   end
19
20   test "links for no such object" do
21     use_token :active
22
23     ac = ApplicationController.new
24
25     links = ac.send :links_for_object, "no-such-uuid"
26
27     assert links, 'Expected links'
28     assert links.is_a?(Array), 'Expected an array'
29     assert links.size == 0, 'Expected no links'
30   end
31
32   test "preload links for given uuids" do
33     use_token :active
34
35     ac = ApplicationController.new
36
37     link_head_uuid1 = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
38     link_head_uuid2 = api_fixture('links')['bar_file_readable_by_active']['head_uuid']
39
40     uuids = [link_head_uuid1, link_head_uuid2]
41     links = ac.send :preload_links_for_objects, uuids
42
43     assert links, 'Expected links'
44     assert links.is_a?(Hash), 'Expected a hash'
45     assert links.size == 2, 'Expected two objects in the preloaded links hash'
46     assert links[link_head_uuid1], 'Expected links for the passed in head_uuid'
47     assert links[link_head_uuid2], 'Expected links for the passed in head_uuid'
48   end
49
50   test "preload links for object and uuids" do
51     use_token :active
52
53     ac = ApplicationController.new
54
55     link1_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
56     link2_uuid = api_fixture('links')['bar_file_readable_by_active']['uuid']
57     link3_head_uuid = api_fixture('links')['bar_file_readable_by_active']['head_uuid']
58
59     link2_object = User.find(api_fixture('users')['active']['uuid'])
60     link2_object_uuid = link2_object['uuid']
61
62     uuids = [link1_head_uuid, link2_object, link3_head_uuid]
63     links = ac.send :preload_links_for_objects, uuids
64
65     assert links, 'Expected links'
66     assert links.is_a?(Hash), 'Expected a hash'
67     assert links.size == 3, 'Expected two objects in the preloaded links hash'
68     assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
69     assert links[link2_object_uuid], 'Expected links for the passed in object uuid'
70     assert links[link3_head_uuid], 'Expected links for the passed in link head_uuid'
71   end
72
73   test "preload links for wrong typed input" do
74     use_token :active
75
76     ac = ApplicationController.new
77
78     assert_raise ArgumentError do
79       ac.send :preload_links_for_objects, 'input not an array'
80     end
81   end
82
83   test "preload links for nil input" do
84     use_token :active
85
86     ac = ApplicationController.new
87
88     assert_raise ArgumentError do
89       ac.send :preload_links_for_objects, nil
90     end
91   end
92
93   test "preload links for empty array input" do
94     use_token :active
95
96     ac = ApplicationController.new
97
98     links = ac.send :preload_links_for_objects, []
99
100     assert links, 'Expected links'
101     assert links.is_a?(Hash), 'Expected a hash'
102     assert links.size == 0, 'Expected no objects in the preloaded links hash'
103   end
104
105   test "collections for object" do
106     use_token :active
107
108     ac = ApplicationController.new
109
110     uuid = api_fixture('collections')['foo_file']['uuid']
111
112     collections = ac.send :collections_for_object, uuid
113
114     assert collections, 'Expected collections'
115     assert collections.is_a?(Array), 'Expected an array'
116     assert collections.size == 1, 'Expected one collection object'
117     assert_equal collections[0][:uuid], uuid, 'Expected uuid not found in collections'
118   end
119
120   test "collections for no such object" do
121     use_token :active
122
123     ac = ApplicationController.new
124
125     collections = ac.send :collections_for_object, "no-such-uuid"
126
127     assert collections, 'Expected collections'
128     assert collections.is_a?(Array), 'Expected an array'
129     assert collections.size == 0, 'Expected no collection'
130   end
131
132   test "preload collections for given uuids" do
133     use_token :active
134
135     ac = ApplicationController.new
136
137     uuid1 = api_fixture('collections')['foo_file']['uuid']
138     uuid2 = api_fixture('collections')['bar_file']['uuid']
139
140     uuids = [uuid1, uuid2]
141     collections = ac.send :preload_collections_for_objects, uuids
142
143     assert collections, 'Expected collection'
144     assert collections.is_a?(Hash), 'Expected a hash'
145     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
146     assert collections[uuid1], 'Expected collections for the passed in uuid'
147     assert_equal collections[uuid1].size, 1, 'Expected one collection for the passed in uuid'
148     assert collections[uuid2], 'Expected collections for the passed in uuid'
149     assert_equal collections[uuid2].size, 1, 'Expected one collection for the passed in uuid'
150   end
151
152   test "preload collections for wrong typed input" do
153     use_token :active
154
155     ac = ApplicationController.new
156
157     assert_raise ArgumentError do
158       ac.send :preload_collections_for_objects, 'input not an array'
159     end
160   end
161
162   test "preload collections for nil input" do
163     use_token :active
164
165     ac = ApplicationController.new
166
167     assert_raise ArgumentError do
168       ac.send :preload_collections_for_objects, nil
169     end
170   end
171
172   test "preload collections for empty array input" do
173     use_token :active
174
175     ac = ApplicationController.new
176
177     collections = ac.send :preload_links_for_objects, []
178
179     assert collections, 'Expected collections'
180     assert collections.is_a?(Hash), 'Expected a hash'
181     assert collections.size == 0, 'Expected no objects in the preloaded collections hash'
182   end
183 end