1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 source("fakes/FakeRESTService.R")
7 context("Subcollection")
9 test_that("getRelativePath returns path relative to the tree root", {
11 animal <- Subcollection$new("animal")
13 fish <- Subcollection$new("fish")
16 expect_that(animal$getRelativePath(), equals("animal"))
17 expect_that(fish$getRelativePath(), equals("animal/fish"))
20 test_that(paste("getFileListing by default returns sorted path of all files",
21 "relative to the current subcollection"), {
23 animal <- Subcollection$new("animal")
24 fish <- Subcollection$new("fish")
25 shark <- ArvadosFile$new("shark")
26 blueFish <- ArvadosFile$new("blueFish")
32 result <- animal$getFileListing()
35 expectedResult <- c("animal/fish/blueFish", "animal/fish/shark")
37 resultsMatch <- length(expectedResult) == length(result) &&
38 all(expectedResult == result)
40 expect_true(resultsMatch)
43 test_that(paste("getFileListing returns sorted names of all direct children",
44 "if fullPath is set to FALSE"), {
46 animal <- Subcollection$new("animal")
47 fish <- Subcollection$new("fish")
48 shark <- ArvadosFile$new("shark")
49 dog <- ArvadosFile$new("dog")
55 result <- animal$getFileListing(fullPath = FALSE)
56 expectedResult <- c("dog", "fish")
58 resultsMatch <- length(expectedResult) == length(result) &&
59 all(expectedResult == result)
61 expect_true(resultsMatch)
64 test_that("add adds content to inside collection tree", {
66 animal <- Subcollection$new("animal")
67 fish <- Subcollection$new("fish")
68 dog <- ArvadosFile$new("dog")
73 animalContainsFish <- animal$get("fish")$getName() == fish$getName()
74 animalContainsDog <- animal$get("dog")$getName() == dog$getName()
76 expect_true(animalContainsFish)
77 expect_true(animalContainsDog)
80 test_that("add raises exception if content name is empty string", {
82 animal <- Subcollection$new("animal")
83 rootFolder <- Subcollection$new("")
85 expect_that(animal$add(rootFolder),
86 throws_error("Content has invalid name.", fixed = TRUE))
89 test_that(paste("add raises exception if ArvadosFile/Subcollection",
90 "with same name already exists in the subcollection"), {
92 animal <- Subcollection$new("animal")
93 fish <- Subcollection$new("fish")
94 secondFish <- Subcollection$new("fish")
95 thirdFish <- ArvadosFile$new("fish")
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))
107 test_that(paste("add raises exception if passed argument is",
108 "not ArvadosFile or Subcollection"), {
110 animal <- Subcollection$new("animal")
113 expect_that(animal$add(number),
114 throws_error(paste("Expected AravodsFile or Subcollection object,",
115 "got (numeric)."), fixed = TRUE))
118 test_that(paste("add post content to a REST service",
119 "if subcollection belongs to a collection"), {
121 collectionContent <- c("animal", "animal/fish")
122 fakeREST <- FakeRESTService$new(collectionContent)
124 api <- Arvados$new("myToken", "myHostName")
125 api$setRESTService(fakeREST)
127 collection <- Collection$new(api, "myUUID")
128 animal <- collection$get("animal")
129 dog <- ArvadosFile$new("dog")
133 expect_that(fakeREST$createCallCount, equals(1))
136 test_that("remove removes content from subcollection", {
138 animal <- Subcollection$new("animal")
139 fish <- Subcollection$new("fish")
142 animal$remove("fish")
144 returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
146 expect_true(returnValueAfterRemovalIsNull)
149 test_that(paste("remove raises exception",
150 "if content to remove doesn't exist in the subcollection"), {
152 animal <- Subcollection$new("animal")
154 expect_that(animal$remove("fish"),
155 throws_error(paste("Subcollection doesn't contains ArvadosFile",
156 "or Subcollection with specified name.")))
159 test_that("remove raises exception if passed argument is not character vector", {
161 animal <- Subcollection$new("animal")
164 expect_that(animal$remove(number),
165 throws_error(paste("Expected character,",
166 "got (numeric)."), fixed = TRUE))
169 test_that(paste("remove removes content from REST service",
170 "if subcollection belongs to a collection"), {
172 collectionContent <- c("animal", "animal/fish", "animal/dog")
173 fakeREST <- FakeRESTService$new(collectionContent)
175 api <- Arvados$new("myToken", "myHostName")
176 api$setRESTService(fakeREST)
177 collection <- Collection$new(api, "myUUID")
178 animal <- collection$get("animal")
180 animal$remove("fish")
182 expect_that(fakeREST$deleteCallCount, equals(1))
185 test_that(paste("get returns ArvadosFile or Subcollection",
186 "if file or folder with given name exists"), {
188 animal <- Subcollection$new("animal")
189 fish <- Subcollection$new("fish")
190 dog <- ArvadosFile$new("dog")
195 returnedFish <- animal$get("fish")
196 returnedDog <- animal$get("dog")
198 returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
199 returnedDogIsArvadosFile <- "ArvadosFile" %in% class(returnedDog)
201 expect_true(returnedFishIsSubcollection)
202 expect_that(returnedFish$getName(), equals("fish"))
204 expect_true(returnedDogIsArvadosFile)
205 expect_that(returnedDog$getName(), equals("dog"))
208 test_that(paste("get returns NULL if file or folder",
209 "with given name doesn't exists"), {
211 animal <- Subcollection$new("animal")
212 fish <- Subcollection$new("fish")
216 returnedDogIsNull <- is.null(animal$get("dog"))
218 expect_true(returnedDogIsNull)
221 test_that("getFirst returns first child in the subcollection", {
223 animal <- Subcollection$new("animal")
224 fish <- Subcollection$new("fish")
228 expect_that(animal$getFirst()$getName(), equals("fish"))
231 test_that("getFirst returns NULL if subcollection contains no children", {
233 animal <- Subcollection$new("animal")
235 returnedElementIsNull <- is.null(animal$getFirst())
237 expect_true(returnedElementIsNull)
240 test_that(paste("setCollection by default sets collection",
241 "filed of subcollection and all its children"), {
243 animal <- Subcollection$new("animal")
244 fish <- Subcollection$new("fish")
247 animal$setCollection("myCollection")
249 expect_that(animal$getCollection(), equals("myCollection"))
250 expect_that(fish$getCollection(), equals("myCollection"))
253 test_that(paste("setCollection sets collection filed of subcollection only",
254 "if parameter setRecursively is set to FALSE"), {
256 animal <- Subcollection$new("animal")
257 fish <- Subcollection$new("fish")
260 animal$setCollection("myCollection", setRecursively = FALSE)
261 fishCollectionIsNull <- is.null(fish$getCollection())
263 expect_that(animal$getCollection(), equals("myCollection"))
264 expect_true(fishCollectionIsNull)
267 test_that(paste("move raises exception if subcollection",
268 "doesn't belong to any collection"), {
270 animal <- Subcollection$new("animal")
272 expect_that(animal$move("new/location"),
273 throws_error("Subcollection doesn't belong to any collection"))
276 test_that("move raises exception if new location contains content with the same name", {
278 collectionContent <- c("animal",
283 fakeREST <- FakeRESTService$new(collectionContent)
285 api <- Arvados$new("myToken", "myHostName")
286 api$setRESTService(fakeREST)
287 collection <- Collection$new(api, "myUUID")
288 fish <- collection$get("animal/fish")
290 expect_that(fish$move("fish"),
291 throws_error("Destination already contains content with same name."))
295 test_that(paste("move raises exception if newLocationInCollection",
296 "parameter is invalid"), {
298 collectionContent <- c("animal",
303 fakeREST <- FakeRESTService$new(collectionContent)
305 api <- Arvados$new("myToken", "myHostName")
306 api$setRESTService(fakeREST)
308 collection <- Collection$new(api, "myUUID")
309 fish <- collection$get("animal/fish")
311 expect_that(fish$move("objects/dog"),
312 throws_error("Unable to get destination subcollection."))
315 test_that("move moves subcollection inside collection tree", {
317 collectionContent <- c("animal",
322 fakeREST <- FakeRESTService$new(collectionContent)
324 api <- Arvados$new("myToken", "myHostName")
325 api$setRESTService(fakeREST)
326 collection <- Collection$new(api, "myUUID")
327 fish <- collection$get("animal/fish")
330 fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
331 fishExistsOnNewLocation <- !is.null(collection$get("fish"))
333 expect_true(fishIsNullOnOldLocation)
334 expect_true(fishExistsOnNewLocation)
337 test_that(paste("getSizeInBytes returns zero if subcollection",
338 "is not part of a collection"), {
340 animal <- Subcollection$new("animal")
342 expect_that(animal$getSizeInBytes(), equals(0))
345 test_that(paste("getSizeInBytes delegates size calculation",
346 "to REST service class"), {
348 collectionContent <- c("animal", "animal/fish")
350 fakeREST <- FakeRESTService$new(collectionContent, returnSize)
352 api <- Arvados$new("myToken", "myHostName")
353 api$setRESTService(fakeREST)
354 collection <- Collection$new(api, "myUUID")
355 animal <- collection$get("animal")
357 resourceSize <- animal$getSizeInBytes()
359 expect_that(resourceSize, equals(100))
362 #########################
363 test_that(paste("copy raises exception if subcollection",
364 "doesn't belong to any collection"), {
366 animal <- Subcollection$new("animal")
368 expect_that(animal$copy("new/location"),
369 throws_error("Subcollection doesn't belong to any collection."))
372 test_that("copy raises exception if new location contains content with the same name", {
374 collectionContent <- c("animal",
379 fakeREST <- FakeRESTService$new(collectionContent)
381 api <- Arvados$new("myToken", "myHostName")
382 api$setRESTService(fakeREST)
383 collection <- Collection$new(api, "myUUID")
384 fish <- collection$get("animal/fish")
386 expect_that(fish$copy("fish"),
387 throws_error("Destination already contains content with same name."))
391 test_that(paste("copy raises exception if location parameter is invalid"), {
393 collectionContent <- c("animal",
398 fakeREST <- FakeRESTService$new(collectionContent)
400 api <- Arvados$new("myToken", "myHostName")
401 api$setRESTService(fakeREST)
403 collection <- Collection$new(api, "myUUID")
404 fish <- collection$get("animal/fish")
406 expect_that(fish$copy("objects/dog"),
407 throws_error("Unable to get destination subcollection."))
410 test_that("copy copies subcollection inside collection tree", {
412 collectionContent <- c("animal",
417 fakeREST <- FakeRESTService$new(collectionContent)
419 api <- Arvados$new("myToken", "myHostName")
420 api$setRESTService(fakeREST)
421 collection <- Collection$new(api, "myUUID")
422 fish <- collection$get("animal/fish")
425 fishExistsOnOldLocation <- !is.null(collection$get("animal/fish"))
426 fishExistsOnNewLocation <- !is.null(collection$get("fish"))
428 expect_true(fishExistsOnOldLocation)
429 expect_true(fishExistsOnNewLocation)
432 test_that("duplicate performs deep cloning of Subcollection", {
433 foo <- ArvadosFile$new("foo")
434 bar <- ArvadosFile$new("bar")
435 sub <- Subcollection$new("qux")
439 newSub1 <- sub$duplicate()
440 newSub2 <- sub$duplicate("quux")
442 expect_that(newSub1$getFileListing(), equals(sub$getFileListing()))
443 expect_that(sort(newSub2$getFileListing()), equals(c("quux/bar", "quux/foo")))