1 source("fakes/FakeRESTService.R")
3 context("Subcollection")
5 test_that("getRelativePath returns path relative to the tree root", {
7 animal <- Subcollection$new("animal")
9 fish <- Subcollection$new("fish")
12 expect_that(animal$getRelativePath(), equals("animal"))
13 expect_that(fish$getRelativePath(), equals("animal/fish"))
16 test_that(paste("getFileListing by default returns sorted path of all files",
17 "relative to the current subcollection"), {
19 animal <- Subcollection$new("animal")
20 fish <- Subcollection$new("fish")
21 shark <- ArvadosFile$new("shark")
22 blueFish <- ArvadosFile$new("blueFish")
28 result <- animal$getFileListing()
31 expectedResult <- c("animal/fish/blueFish", "animal/fish/shark")
33 resultsMatch <- length(expectedResult) == length(result) &&
34 all(expectedResult == result)
36 expect_that(resultsMatch, is_true())
39 test_that(paste("getFileListing returns sorted names of all direct children",
40 "if fullPath is set to FALSE"), {
42 animal <- Subcollection$new("animal")
43 fish <- Subcollection$new("fish")
44 shark <- ArvadosFile$new("shark")
45 dog <- ArvadosFile$new("dog")
51 result <- animal$getFileListing(fullPath = FALSE)
52 expectedResult <- c("dog", "fish")
54 resultsMatch <- length(expectedResult) == length(result) &&
55 all(expectedResult == result)
57 expect_that(resultsMatch, is_true())
60 test_that("add adds content to inside collection tree", {
62 animal <- Subcollection$new("animal")
63 fish <- Subcollection$new("fish")
64 dog <- ArvadosFile$new("dog")
69 animalContainsFish <- animal$get("fish")$getName() == fish$getName()
70 animalContainsDog <- animal$get("dog")$getName() == dog$getName()
72 expect_that(animalContainsFish, is_true())
73 expect_that(animalContainsDog, is_true())
76 test_that(paste("add raises exception if ArvadosFile/Subcollection",
77 "with same name already exists in the subcollection"), {
79 animal <- Subcollection$new("animal")
80 fish <- Subcollection$new("fish")
81 secondFish <- Subcollection$new("fish")
82 thirdFish <- ArvadosFile$new("fish")
86 expect_that(animal$add(secondFish),
87 throws_error(paste("Subcollection already contains ArvadosFile or",
88 "Subcollection with same name."), fixed = TRUE))
89 expect_that(animal$add(thirdFish),
90 throws_error(paste("Subcollection already contains ArvadosFile or",
91 "Subcollection with same name."), fixed = TRUE))
94 test_that(paste("add raises exception if passed argument is",
95 "not ArvadosFile or Subcollection"), {
97 animal <- Subcollection$new("animal")
100 expect_that(animal$add(number),
101 throws_error(paste("Expected AravodsFile or Subcollection object,",
102 "got (numeric)."), fixed = TRUE))
105 test_that(paste("add post content to a REST service",
106 "if subcollection belongs to a collection"), {
108 collectionContent <- c("animal", "animal/fish")
109 fakeREST <- FakeRESTService$new(collectionContent)
111 api <- Arvados$new("myToken", "myHostName")
112 api$setRESTService(fakeREST)
114 collection <- Collection$new(api, "myUUID")
115 animal <- collection$get("animal")
116 dog <- ArvadosFile$new("dog")
120 expect_that(fakeREST$createCallCount, equals(1))
123 test_that("remove removes content from subcollection", {
125 animal <- Subcollection$new("animal")
126 fish <- Subcollection$new("fish")
129 animal$remove("fish")
131 returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
133 expect_that(returnValueAfterRemovalIsNull, is_true())
136 test_that(paste("remove raises exception",
137 "if content to remove doesn't exist in the subcollection"), {
139 animal <- Subcollection$new("animal")
141 expect_that(animal$remove("fish"),
142 throws_error(paste("Subcollection doesn't contains ArvadosFile",
143 "or Subcollection with specified name.")))
146 test_that("remove raises exception if passed argument is not character vector", {
148 animal <- Subcollection$new("animal")
151 expect_that(animal$remove(number),
152 throws_error(paste("Expected character,",
153 "got (numeric)."), fixed = TRUE))
156 test_that(paste("remove removes content from REST service",
157 "if subcollection belongs to a collection"), {
159 collectionContent <- c("animal", "animal/fish", "animal/dog")
160 fakeREST <- FakeRESTService$new(collectionContent)
162 api <- Arvados$new("myToken", "myHostName")
163 api$setRESTService(fakeREST)
164 collection <- Collection$new(api, "myUUID")
165 animal <- collection$get("animal")
167 animal$remove("fish")
169 expect_that(fakeREST$deleteCallCount, equals(1))
172 test_that(paste("get returns ArvadosFile or Subcollection",
173 "if file or folder with given name exists"), {
175 animal <- Subcollection$new("animal")
176 fish <- Subcollection$new("fish")
177 dog <- ArvadosFile$new("dog")
182 returnedFish <- animal$get("fish")
183 returnedDog <- animal$get("dog")
185 returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
186 returnedDogIsArvadosFile <- "ArvadosFile" %in% class(returnedDog)
188 expect_that(returnedFishIsSubcollection, is_true())
189 expect_that(returnedFish$getName(), equals("fish"))
191 expect_that(returnedDogIsArvadosFile, is_true())
192 expect_that(returnedDog$getName(), equals("dog"))
195 test_that(paste("get returns NULL if file or folder",
196 "with given name doesn't exists"), {
198 animal <- Subcollection$new("animal")
199 fish <- Subcollection$new("fish")
203 returnedDogIsNull <- is.null(animal$get("dog"))
205 expect_that(returnedDogIsNull, is_true())
208 test_that("getFirst returns first child in the subcollection", {
210 animal <- Subcollection$new("animal")
211 fish <- Subcollection$new("fish")
215 expect_that(animal$getFirst()$getName(), equals("fish"))
218 test_that("getFirst returns NULL if subcollection contains no children", {
220 animal <- Subcollection$new("animal")
222 returnedElementIsNull <- is.null(animal$getFirst())
224 expect_that(returnedElementIsNull, is_true())
227 test_that(paste("setCollection by default sets collection",
228 "filed of subcollection and all its children"), {
230 animal <- Subcollection$new("animal")
231 fish <- Subcollection$new("fish")
234 animal$setCollection("myCollection")
236 expect_that(animal$getCollection(), equals("myCollection"))
237 expect_that(fish$getCollection(), equals("myCollection"))
240 test_that(paste("setCollection sets collection filed of subcollection only",
241 "if parameter setRecursively is set to FALSE"), {
243 animal <- Subcollection$new("animal")
244 fish <- Subcollection$new("fish")
247 animal$setCollection("myCollection", setRecursively = FALSE)
248 fishCollectionIsNull <- is.null(fish$getCollection())
250 expect_that(animal$getCollection(), equals("myCollection"))
251 expect_that(fishCollectionIsNull, is_true())
254 test_that(paste("move raises exception if subcollection",
255 "doesn't belong to any collection"), {
257 animal <- Subcollection$new("animal")
259 expect_that(animal$move("new/location"),
260 throws_error("Subcollection doesn't belong to any collection"))
263 test_that("move raises exception if new location contains content with the same name", {
265 collectionContent <- c("animal",
270 fakeREST <- FakeRESTService$new(collectionContent)
272 api <- Arvados$new("myToken", "myHostName")
273 api$setRESTService(fakeREST)
274 collection <- Collection$new(api, "myUUID")
275 fish <- collection$get("animal/fish")
277 expect_that(fish$move("fish"),
278 throws_error("Destination already contains content with same name."))
282 test_that(paste("move raises exception if newLocationInCollection",
283 "parameter is invalid"), {
285 collectionContent <- c("animal",
290 fakeREST <- FakeRESTService$new(collectionContent)
292 api <- Arvados$new("myToken", "myHostName")
293 api$setRESTService(fakeREST)
295 collection <- Collection$new(api, "myUUID")
296 fish <- collection$get("animal/fish")
298 expect_that(fish$move("objects/dog"),
299 throws_error("Unable to get destination subcollection"))
302 test_that("move moves subcollection inside collection tree", {
304 collectionContent <- c("animal",
309 fakeREST <- FakeRESTService$new(collectionContent)
311 api <- Arvados$new("myToken", "myHostName")
312 api$setRESTService(fakeREST)
313 collection <- Collection$new(api, "myUUID")
314 fish <- collection$get("animal/fish")
317 fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
318 fishExistsOnNewLocation <- !is.null(collection$get("fish"))
320 expect_that(fishIsNullOnOldLocation, is_true())
321 expect_that(fishExistsOnNewLocation, is_true())
324 test_that(paste("getSizeInBytes returns zero if subcollection",
325 "is not part of a collection"), {
327 animal <- Subcollection$new("animal")
329 expect_that(animal$getSizeInBytes(), equals(0))
332 test_that(paste("getSizeInBytes delegates size calculation",
333 "to REST service class"), {
335 collectionContent <- c("animal", "animal/fish")
337 fakeREST <- FakeRESTService$new(collectionContent, returnSize)
339 api <- Arvados$new("myToken", "myHostName")
340 api$setRESTService(fakeREST)
341 collection <- Collection$new(api, "myUUID")
342 animal <- collection$get("animal")
344 resourceSize <- animal$getSizeInBytes()
346 expect_that(resourceSize, equals(100))