Implement copy method, update move method and remove trailing
[arvados.git] / sdk / R / tests / testthat / test-Subcollection.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("Subcollection")
8
9 test_that("getRelativePath returns path relative to the tree root", {
10
11     animal <- Subcollection$new("animal")
12
13     fish <- Subcollection$new("fish")
14     animal$add(fish)
15
16     expect_that(animal$getRelativePath(), equals("animal"))
17     expect_that(fish$getRelativePath(), equals("animal/fish"))
18 })
19
20 test_that(paste("getFileListing by default returns sorted path of all files",
21                 "relative to the current subcollection"), {
22
23     animal   <- Subcollection$new("animal")
24     fish     <- Subcollection$new("fish")
25     shark    <- ArvadosFile$new("shark")
26     blueFish <- ArvadosFile$new("blueFish")
27
28     animal$add(fish)
29     fish$add(shark)
30     fish$add(blueFish)
31
32     result <- animal$getFileListing()
33
34     #expect sorted array
35     expectedResult <- c("animal/fish/blueFish", "animal/fish/shark")
36
37     resultsMatch <- length(expectedResult) == length(result) &&
38                     all(expectedResult == result)
39
40     expect_that(resultsMatch, is_true())
41 })
42
43 test_that(paste("getFileListing returns sorted names of all direct children",
44                 "if fullPath is set to FALSE"), {
45
46     animal <- Subcollection$new("animal")
47     fish   <- Subcollection$new("fish")
48     shark  <- ArvadosFile$new("shark")
49     dog    <- ArvadosFile$new("dog")
50
51     animal$add(fish)
52     animal$add(dog)
53     fish$add(shark)
54
55     result <- animal$getFileListing(fullPath = FALSE)
56     expectedResult <- c("dog", "fish")
57
58     resultsMatch <- length(expectedResult) == length(result) &&
59                     all(expectedResult == result)
60
61     expect_that(resultsMatch, is_true())
62 })
63
64 test_that("add adds content to inside collection tree", {
65
66     animal <- Subcollection$new("animal")
67     fish   <- Subcollection$new("fish")
68     dog    <- ArvadosFile$new("dog")
69
70     animal$add(fish)
71     animal$add(dog)
72
73     animalContainsFish <- animal$get("fish")$getName() == fish$getName()
74     animalContainsDog  <- animal$get("dog")$getName()  == dog$getName()
75
76     expect_that(animalContainsFish, is_true())
77     expect_that(animalContainsDog, is_true())
78 })
79
80 test_that("add raises exception if content name is empty string", {
81
82     animal     <- Subcollection$new("animal")
83     rootFolder <- Subcollection$new("")
84
85     expect_that(animal$add(rootFolder),
86                 throws_error("Content has invalid name.", fixed = TRUE))
87 })
88
89 test_that(paste("add raises exception if ArvadosFile/Subcollection",
90                 "with same name already exists in the subcollection"), {
91
92     animal     <- Subcollection$new("animal")
93     fish       <- Subcollection$new("fish")
94     secondFish <- Subcollection$new("fish")
95     thirdFish  <- ArvadosFile$new("fish")
96
97     animal$add(fish)
98
99     expect_that(animal$add(secondFish),
100                 throws_error(paste("Subcollection already contains ArvadosFile or",
101                                    "Subcollection with same name."), fixed = TRUE))
102     expect_that(animal$add(thirdFish),
103                 throws_error(paste("Subcollection already contains ArvadosFile or",
104                                    "Subcollection with same name."), fixed = TRUE))
105 })
106
107 test_that(paste("add raises exception if passed argument is",
108                 "not ArvadosFile or Subcollection"), {
109
110     animal <- Subcollection$new("animal")
111     number <- 10
112
113     expect_that(animal$add(number),
114                 throws_error(paste("Expected AravodsFile or Subcollection object,",
115                                    "got (numeric)."), fixed = TRUE))
116 })
117
118 test_that(paste("add post content to a REST service",
119                 "if subcollection belongs to a collection"), {
120
121     collectionContent <- c("animal", "animal/fish")
122     fakeREST <- FakeRESTService$new(collectionContent)
123
124     api <- Arvados$new("myToken", "myHostName")
125     api$setRESTService(fakeREST)
126
127     collection <- Collection$new(api, "myUUID")
128     animal <- collection$get("animal")
129     dog <- ArvadosFile$new("dog")
130
131     animal$add(dog)
132
133     expect_that(fakeREST$createCallCount, equals(1))
134 })
135
136 test_that("remove removes content from subcollection", {
137
138     animal <- Subcollection$new("animal")
139     fish   <- Subcollection$new("fish")
140
141     animal$add(fish)
142     animal$remove("fish")
143
144     returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
145
146     expect_that(returnValueAfterRemovalIsNull, is_true())
147 })
148
149 test_that(paste("remove raises exception",
150                 "if content to remove doesn't exist in the subcollection"), {
151
152     animal <- Subcollection$new("animal")
153
154     expect_that(animal$remove("fish"),
155                 throws_error(paste("Subcollection doesn't contains ArvadosFile",
156                                    "or Subcollection with specified name.")))
157 })
158
159 test_that("remove raises exception if passed argument is not character vector", {
160
161     animal <- Subcollection$new("animal")
162     number <- 10
163
164     expect_that(animal$remove(number),
165                 throws_error(paste("Expected character,",
166                                    "got (numeric)."), fixed = TRUE))
167 })
168
169 test_that(paste("remove removes content from REST service",
170                 "if subcollection belongs to a collection"), {
171
172     collectionContent <- c("animal", "animal/fish", "animal/dog")
173     fakeREST <- FakeRESTService$new(collectionContent)
174
175     api <- Arvados$new("myToken", "myHostName")
176     api$setRESTService(fakeREST)
177     collection <- Collection$new(api, "myUUID")
178     animal <- collection$get("animal")
179
180     animal$remove("fish")
181
182     expect_that(fakeREST$deleteCallCount, equals(1))
183 })
184
185 test_that(paste("get returns ArvadosFile or Subcollection",
186                 "if file or folder with given name exists"), {
187
188     animal <- Subcollection$new("animal")
189     fish   <- Subcollection$new("fish")
190     dog    <- ArvadosFile$new("dog")
191
192     animal$add(fish)
193     animal$add(dog)
194
195     returnedFish <- animal$get("fish")
196     returnedDog  <- animal$get("dog")
197
198     returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
199     returnedDogIsArvadosFile    <- "ArvadosFile"   %in% class(returnedDog)
200
201     expect_that(returnedFishIsSubcollection, is_true())
202     expect_that(returnedFish$getName(), equals("fish"))
203
204     expect_that(returnedDogIsArvadosFile, is_true())
205     expect_that(returnedDog$getName(), equals("dog"))
206 })
207
208 test_that(paste("get returns NULL if file or folder",
209                 "with given name doesn't exists"), {
210
211     animal <- Subcollection$new("animal")
212     fish   <- Subcollection$new("fish")
213
214     animal$add(fish)
215
216     returnedDogIsNull <- is.null(animal$get("dog"))
217
218     expect_that(returnedDogIsNull, is_true())
219 })
220
221 test_that("getFirst returns first child in the subcollection", {
222
223     animal <- Subcollection$new("animal")
224     fish   <- Subcollection$new("fish")
225
226     animal$add(fish)
227
228     expect_that(animal$getFirst()$getName(), equals("fish"))
229 })
230
231 test_that("getFirst returns NULL if subcollection contains no children", {
232
233     animal <- Subcollection$new("animal")
234
235     returnedElementIsNull <- is.null(animal$getFirst())
236
237     expect_that(returnedElementIsNull, is_true())
238 })
239
240 test_that(paste("setCollection by default sets collection",
241                 "filed of subcollection and all its children"), {
242
243     animal <- Subcollection$new("animal")
244     fish   <- Subcollection$new("fish")
245     animal$add(fish)
246
247     animal$setCollection("myCollection")
248
249     expect_that(animal$getCollection(), equals("myCollection"))
250     expect_that(fish$getCollection(), equals("myCollection"))
251 })
252
253 test_that(paste("setCollection sets collection filed of subcollection only",
254                 "if parameter setRecursively is set to FALSE"), {
255
256     animal <- Subcollection$new("animal")
257     fish   <- Subcollection$new("fish")
258     animal$add(fish)
259
260     animal$setCollection("myCollection", setRecursively = FALSE)
261     fishCollectionIsNull <- is.null(fish$getCollection())
262
263     expect_that(animal$getCollection(), equals("myCollection"))
264     expect_that(fishCollectionIsNull, is_true())
265 })
266
267 test_that(paste("move raises exception if subcollection",
268                 "doesn't belong to any collection"), {
269
270     animal <- Subcollection$new("animal")
271
272     expect_that(animal$move("new/location"),
273                 throws_error("Subcollection doesn't belong to any collection"))
274 })
275
276 test_that("move raises exception if new location contains content with the same name", {
277
278     collectionContent <- c("animal",
279                            "animal/fish",
280                            "animal/dog",
281                            "animal/fish/shark",
282                            "fish")
283     fakeREST <- FakeRESTService$new(collectionContent)
284
285     api <- Arvados$new("myToken", "myHostName")
286     api$setRESTService(fakeREST)
287     collection <- Collection$new(api, "myUUID")
288     fish <- collection$get("animal/fish")
289
290     expect_that(fish$move("fish"),
291                 throws_error("Destination already contains content with same name."))
292
293 })
294
295 test_that(paste("move raises exception if newLocationInCollection",
296                 "parameter is invalid"), {
297
298     collectionContent <- c("animal",
299                            "animal/fish",
300                            "animal/dog",
301                            "animal/fish/shark",
302                            "ball")
303     fakeREST <- FakeRESTService$new(collectionContent)
304
305     api <- Arvados$new("myToken", "myHostName")
306     api$setRESTService(fakeREST)
307
308     collection <- Collection$new(api, "myUUID")
309     fish <- collection$get("animal/fish")
310
311     expect_that(fish$move("objects/dog"),
312                 throws_error("Unable to get destination subcollection."))
313 })
314
315 test_that("move moves subcollection inside collection tree", {
316
317     collectionContent <- c("animal",
318                            "animal/fish",
319                            "animal/dog",
320                            "animal/fish/shark",
321                            "ball")
322     fakeREST <- FakeRESTService$new(collectionContent)
323
324     api <- Arvados$new("myToken", "myHostName")
325     api$setRESTService(fakeREST)
326     collection <- Collection$new(api, "myUUID")
327     fish <- collection$get("animal/fish")
328
329     fish$move("fish")
330     fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
331     fishExistsOnNewLocation <- !is.null(collection$get("fish"))
332
333     expect_that(fishIsNullOnOldLocation, is_true())
334     expect_that(fishExistsOnNewLocation, is_true())
335 })
336
337 test_that(paste("getSizeInBytes returns zero if subcollection",
338                 "is not part of a collection"), {
339
340     animal <- Subcollection$new("animal")
341
342     expect_that(animal$getSizeInBytes(), equals(0))
343 })
344
345 test_that(paste("getSizeInBytes delegates size calculation",
346                 "to REST service class"), {
347
348     collectionContent <- c("animal", "animal/fish")
349     returnSize <- 100
350     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
351
352     api <- Arvados$new("myToken", "myHostName")
353     api$setRESTService(fakeREST)
354     collection <- Collection$new(api, "myUUID")
355     animal <- collection$get("animal")
356
357     resourceSize <- animal$getSizeInBytes()
358
359     expect_that(resourceSize, equals(100))
360 })
361
362 #########################
363 test_that(paste("copy raises exception if subcollection",
364                 "doesn't belong to any collection"), {
365
366     animal <- Subcollection$new("animal")
367
368     expect_that(animal$copy("new/location"),
369                 throws_error("Subcollection doesn't belong to any collection."))
370 })
371
372 test_that("copy raises exception if new location contains content with the same name", {
373
374     collectionContent <- c("animal",
375                            "animal/fish",
376                            "animal/dog",
377                            "animal/fish/shark",
378                            "fish")
379     fakeREST <- FakeRESTService$new(collectionContent)
380
381     api <- Arvados$new("myToken", "myHostName")
382     api$setRESTService(fakeREST)
383     collection <- Collection$new(api, "myUUID")
384     fish <- collection$get("animal/fish")
385
386     expect_that(fish$copy("fish"),
387                 throws_error("Destination already contains content with same name."))
388
389 })
390
391 test_that(paste("copy raises exception if location parameter is invalid"), {
392
393     collectionContent <- c("animal",
394                            "animal/fish",
395                            "animal/dog",
396                            "animal/fish/shark",
397                            "ball")
398     fakeREST <- FakeRESTService$new(collectionContent)
399
400     api <- Arvados$new("myToken", "myHostName")
401     api$setRESTService(fakeREST)
402
403     collection <- Collection$new(api, "myUUID")
404     fish <- collection$get("animal/fish")
405
406     expect_that(fish$copy("objects/dog"),
407                 throws_error("Unable to get destination subcollection."))
408 })
409
410 test_that("copy copies subcollection inside collection tree", {
411
412     collectionContent <- c("animal",
413                            "animal/fish",
414                            "animal/dog",
415                            "animal/fish/shark",
416                            "ball")
417     fakeREST <- FakeRESTService$new(collectionContent)
418
419     api <- Arvados$new("myToken", "myHostName")
420     api$setRESTService(fakeREST)
421     collection <- Collection$new(api, "myUUID")
422     fish <- collection$get("animal/fish")
423
424     fish$copy("fish")
425     fishExistsOnOldLocation <- !is.null(collection$get("animal/fish"))
426     fishExistsOnNewLocation <- !is.null(collection$get("fish"))
427
428     expect_that(fishExistsOnOldLocation, is_true())
429     expect_that(fishExistsOnNewLocation, is_true())
430 })
431
432 test_that("duplicate performs deep cloning of Subcollection", {
433     foo <- ArvadosFile$new("foo")
434     bar <- ArvadosFile$new("bar")
435     sub <- Subcollection$new("qux")
436     sub$add(foo)
437     sub$add(bar)
438
439     newSub1 <- sub$duplicate()
440     newSub2 <- sub$duplicate("quux")
441
442     expect_that(newSub1$getFileListing(), equals(sub$getFileListing()))
443     expect_that(sort(newSub2$getFileListing()), equals(c("quux/bar", "quux/foo")))
444 })