21200: copied branch from arvados-workbench2 Arvados-DCO-1.1-Signed-off-by: Lisa...
[arvados.git] / sdk / R / tests / testthat / test-Collection.R
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 source("fakes/FakeRESTService.R")
6
7 context("Collection")
8
9 test_that(paste("constructor creates file tree from text content",
10                 "retreived form REST service"), {
11
12     collectionContent <- c("animal", "animal/fish", "ball")
13     fakeREST <- FakeRESTService$new(collectionContent)
14
15     api <- Arvados$new("myToken", "myHostName")
16     api$setRESTService(fakeREST)
17     collection <- Collection$new(api, "myUUID")
18
19     root <- collection$get("")
20
21     expect_that(fakeREST$getCollectionContentCallCount, equals(1))
22     expect_that(root$getName(), equals(""))
23 })
24
25 test_that(paste("add raises exception if passed argumet is not",
26                 "ArvadosFile or Subcollection"), {
27
28     collectionContent <- c("animal", "animal/fish", "ball")
29     fakeREST <- FakeRESTService$new(collectionContent)
30
31     api <- Arvados$new("myToken", "myHostName")
32     api$setRESTService(fakeREST)
33     collection <- Collection$new(api, "myUUID")
34
35     newNumber <- 10
36
37     expect_that(collection$add(newNumber),
38     throws_error(paste("Expected AravodsFile or Subcollection",
39                        "object, got (numeric)."), fixed = TRUE))
40 })
41
42 test_that("add raises exception if relative path is not valid", {
43
44     collectionContent <- c("animal", "animal/fish", "ball")
45     fakeREST <- FakeRESTService$new(collectionContent)
46
47     api <- Arvados$new("myToken", "myHostName")
48     api$setRESTService(fakeREST)
49     collection <- Collection$new(api, "myUUID")
50
51     newPen <- ArvadosFile$new("pen")
52
53     expect_that(collection$add(newPen, "objects"),
54                 throws_error("Subcollection objects doesn't exist.",
55                               fixed = TRUE))
56 })
57
58 test_that("add raises exception if content name is empty string", {
59
60     collectionContent <- c("animal", "animal/fish")
61     fakeREST <- FakeRESTService$new(collectionContent)
62
63     api <- Arvados$new("myToken", "myHostName")
64     api$setRESTService(fakeREST)
65     collection <- Collection$new(api, "myUUID")
66
67     rootFolder <- Subcollection$new("")
68
69     expect_that(collection$add(rootFolder),
70                 throws_error("Content has invalid name.", fixed = TRUE))
71 })
72
73 test_that(paste("add adds ArvadosFile or Subcollection",
74                 "to local tree structure and remote REST service"), {
75
76     collectionContent <- c("animal", "animal/fish", "ball")
77     fakeREST <- FakeRESTService$new(collectionContent)
78
79     api <- Arvados$new("myToken", "myHostName")
80     api$setRESTService(fakeREST)
81     collection <- Collection$new(api, "myUUID")
82
83     newDog <- ArvadosFile$new("dog")
84     collection$add(newDog, "animal")
85
86     dog <- collection$get("animal/dog")
87     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
88
89     expect_true(dogExistsInCollection)
90     expect_that(fakeREST$createCallCount, equals(1))
91 })
92
93 test_that("create raises exception if passed argumet is not character vector", {
94
95     collectionContent <- c("animal", "animal/fish", "ball")
96     fakeREST <- FakeRESTService$new(collectionContent)
97
98     api <- Arvados$new("myToken", "myHostName")
99     api$setRESTService(fakeREST)
100     collection <- Collection$new(api, "myUUID")
101
102     expect_that(collection$create(10),
103                 throws_error("Expected character vector, got (numeric).",
104                              fixed = TRUE))
105 })
106
107 test_that(paste("create adds files specified by fileNames",
108                 "to local tree structure and remote REST service"), {
109
110     fakeREST <- FakeRESTService$new()
111     api <- Arvados$new("myToken", "myHostName")
112     api$setRESTService(fakeREST)
113     collection <- Collection$new(api, "myUUID")
114
115     collection$create(c("animal/dog", "animal/cat"))
116
117     dog <- collection$get("animal/dog")
118     cat <- collection$get("animal/cat")
119     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
120     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
121
122     expect_true(dogExistsInCollection)
123     expect_true(catExistsInCollection)
124     expect_that(fakeREST$createCallCount, equals(2))
125 })
126
127 test_that("remove raises exception if passed argumet is not character vector", {
128
129     collectionContent <- c("animal", "animal/fish", "ball")
130     fakeREST <- FakeRESTService$new(collectionContent)
131
132     api <- Arvados$new("myToken", "myHostName")
133     api$setRESTService(fakeREST)
134     collection <- Collection$new(api, "myUUID")
135
136     expect_that(collection$remove(10),
137                 throws_error("Expected character vector, got (numeric).",
138                              fixed = TRUE))
139 })
140
141 test_that("remove raises exception if user tries to remove root folder", {
142
143     collectionContent <- c("animal", "animal/fish")
144     fakeREST <- FakeRESTService$new(collectionContent)
145
146     api <- Arvados$new("myToken", "myHostName")
147     api$setRESTService(fakeREST)
148     collection <- Collection$new(api, "myUUID")
149
150     expect_that(collection$remove(""),
151                 throws_error("You can't delete root folder.", fixed = TRUE))
152 })
153
154 test_that(paste("remove removes files specified by paths",
155                 "from local tree structure and from remote REST service"), {
156
157     collectionContent <- c("animal", "animal/fish", "animal/dog", "animal/cat", "ball")
158     fakeREST <- FakeRESTService$new(collectionContent)
159
160     api <- Arvados$new("myToken", "myHostName")
161     api$setRESTService(fakeREST)
162     collection <- Collection$new(api, "myUUID")
163
164     collection$remove(c("animal/dog", "animal/cat"))
165
166     dog <- collection$get("animal/dog")
167     cat <- collection$get("animal/dog")
168     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
169     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
170
171     expect_false(dogExistsInCollection)
172     expect_false(catExistsInCollection)
173     expect_that(fakeREST$deleteCallCount, equals(2))
174 })
175
176 test_that(paste("move moves content to a new location inside file tree",
177                 "and on REST service"), {
178
179     collectionContent <- c("animal", "animal/dog", "ball")
180     fakeREST <- FakeRESTService$new(collectionContent)
181
182     api <- Arvados$new("myToken", "myHostName")
183     api$setRESTService(fakeREST)
184     collection <- Collection$new(api, "myUUID")
185
186     collection$move("animal/dog", "dog")
187
188     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
189     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
190
191     expect_true(dogIsNullOnOldLocation)
192     expect_true(dogExistsOnNewLocation)
193     expect_that(fakeREST$moveCallCount, equals(1))
194 })
195
196 test_that("move raises exception if new location is not valid", {
197
198     collectionContent <- c("animal", "animal/fish", "ball")
199     fakeREST <- FakeRESTService$new(collectionContent)
200
201     api <- Arvados$new("myToken", "myHostName")
202     api$setRESTService(fakeREST)
203     collection <- Collection$new(api, "myUUID")
204
205     expect_that(collection$move("fish", "object"),
206                 throws_error("Content you want to move doesn't exist in the collection.",
207                              fixed = TRUE))
208 })
209
210 test_that("getFileListing returns sorted collection content received from REST service", {
211
212     collectionContent <- c("animal", "animal/fish", "ball")
213     fakeREST <- FakeRESTService$new(collectionContent)
214
215     api <- Arvados$new("myToken", "myHostName")
216     api$setRESTService(fakeREST)
217     collection <- Collection$new(api, "myUUID")
218
219     contentMatchExpected <- all(collection$getFileListing() ==
220                                 c("animal", "animal/fish", "ball"))
221
222     expect_true(contentMatchExpected)
223     #2 calls because Collection$new calls getFileListing once
224     expect_that(fakeREST$getCollectionContentCallCount, equals(2))
225
226 })
227
228 test_that("get returns arvados file or subcollection from internal tree structure", {
229
230     collectionContent <- c("animal", "animal/fish", "ball")
231     fakeREST <- FakeRESTService$new(collectionContent)
232
233     api <- Arvados$new("myToken", "myHostName")
234     api$setRESTService(fakeREST)
235     collection <- Collection$new(api, "myUUID")
236
237     fish <- collection$get("animal/fish")
238     fishIsNotNull <- !is.null(fish)
239
240     expect_true(fishIsNotNull)
241     expect_that(fish$getName(), equals("fish"))
242
243     ball <- collection$get("ball")
244     ballIsNotNull <- !is.null(ball)
245
246     expect_true(ballIsNotNull)
247     expect_that(ball$getName(), equals("ball"))
248 })
249
250 test_that(paste("copy copies content to a new location inside file tree",
251                 "and on REST service"), {
252
253     collectionContent <- c("animal", "animal/dog", "ball")
254     fakeREST <- FakeRESTService$new(collectionContent)
255
256     api <- Arvados$new("myToken", "myHostName")
257     api$setRESTService(fakeREST)
258     collection <- Collection$new(api, "myUUID")
259
260     collection$copy("animal/dog", "dog")
261
262     dogExistsOnOldLocation <- !is.null(collection$get("animal/dog"))
263     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
264
265     expect_true(dogExistsOnOldLocation)
266     expect_true(dogExistsOnNewLocation)
267     expect_that(fakeREST$copyCallCount, equals(1))
268 })
269
270 test_that("copy raises exception if new location is not valid", {
271
272     collectionContent <- c("animal", "animal/fish", "ball")
273     fakeREST <- FakeRESTService$new(collectionContent)
274
275     api <- Arvados$new("myToken", "myHostName")
276     api$setRESTService(fakeREST)
277     collection <- Collection$new(api, "myUUID")
278
279     expect_that(collection$copy("fish", "object"),
280                 throws_error("Content you want to copy doesn't exist in the collection.",
281                              fixed = TRUE))
282 })
283
284 test_that("refresh invalidates current tree structure", {
285
286     collectionContent <- c("animal", "animal/fish", "ball")
287     fakeREST <- FakeRESTService$new(collectionContent)
288
289     api <- Arvados$new("myToken", "myHostName")
290     api$setRESTService(fakeREST)
291     collection <- Collection$new(api, "aaaaa-j7d0g-ccccccccccccccc")
292
293     # Before refresh
294     fish <- collection$get("animal/fish")
295     expect_that(fish$getName(), equals("fish"))
296     expect_that(fish$getCollection()$uuid, equals("aaaaa-j7d0g-ccccccccccccccc"))
297
298     collection$refresh()
299
300     # After refresh
301     expect_that(fish$getName(), equals("fish"))
302     expect_true(is.null(fish$getCollection()))
303 })