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("add raises exception if content name is empty string", {
78 animal <- Subcollection$new("animal")
79 rootFolder <- Subcollection$new("")
81 expect_that(animal$add(rootFolder),
82 throws_error("Content has invalid name.", fixed = TRUE))
85 test_that(paste("add raises exception if ArvadosFile/Subcollection",
86 "with same name already exists in the subcollection"), {
88 animal <- Subcollection$new("animal")
89 fish <- Subcollection$new("fish")
90 secondFish <- Subcollection$new("fish")
91 thirdFish <- ArvadosFile$new("fish")
95 expect_that(animal$add(secondFish),
96 throws_error(paste("Subcollection already contains ArvadosFile or",
97 "Subcollection with same name."), fixed = TRUE))
98 expect_that(animal$add(thirdFish),
99 throws_error(paste("Subcollection already contains ArvadosFile or",
100 "Subcollection with same name."), fixed = TRUE))
103 test_that(paste("add raises exception if passed argument is",
104 "not ArvadosFile or Subcollection"), {
106 animal <- Subcollection$new("animal")
109 expect_that(animal$add(number),
110 throws_error(paste("Expected AravodsFile or Subcollection object,",
111 "got (numeric)."), fixed = TRUE))
114 test_that(paste("add post content to a REST service",
115 "if subcollection belongs to a collection"), {
117 collectionContent <- c("animal", "animal/fish")
118 fakeREST <- FakeRESTService$new(collectionContent)
120 api <- Arvados$new("myToken", "myHostName")
121 api$setRESTService(fakeREST)
123 collection <- Collection$new(api, "myUUID")
124 animal <- collection$get("animal")
125 dog <- ArvadosFile$new("dog")
129 expect_that(fakeREST$createCallCount, equals(1))
132 test_that("remove removes content from subcollection", {
134 animal <- Subcollection$new("animal")
135 fish <- Subcollection$new("fish")
138 animal$remove("fish")
140 returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
142 expect_that(returnValueAfterRemovalIsNull, is_true())
145 test_that(paste("remove raises exception",
146 "if content to remove doesn't exist in the subcollection"), {
148 animal <- Subcollection$new("animal")
150 expect_that(animal$remove("fish"),
151 throws_error(paste("Subcollection doesn't contains ArvadosFile",
152 "or Subcollection with specified name.")))
155 test_that("remove raises exception if passed argument is not character vector", {
157 animal <- Subcollection$new("animal")
160 expect_that(animal$remove(number),
161 throws_error(paste("Expected character,",
162 "got (numeric)."), fixed = TRUE))
165 test_that(paste("remove removes content from REST service",
166 "if subcollection belongs to a collection"), {
168 collectionContent <- c("animal", "animal/fish", "animal/dog")
169 fakeREST <- FakeRESTService$new(collectionContent)
171 api <- Arvados$new("myToken", "myHostName")
172 api$setRESTService(fakeREST)
173 collection <- Collection$new(api, "myUUID")
174 animal <- collection$get("animal")
176 animal$remove("fish")
178 expect_that(fakeREST$deleteCallCount, equals(1))
181 test_that(paste("get returns ArvadosFile or Subcollection",
182 "if file or folder with given name exists"), {
184 animal <- Subcollection$new("animal")
185 fish <- Subcollection$new("fish")
186 dog <- ArvadosFile$new("dog")
191 returnedFish <- animal$get("fish")
192 returnedDog <- animal$get("dog")
194 returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
195 returnedDogIsArvadosFile <- "ArvadosFile" %in% class(returnedDog)
197 expect_that(returnedFishIsSubcollection, is_true())
198 expect_that(returnedFish$getName(), equals("fish"))
200 expect_that(returnedDogIsArvadosFile, is_true())
201 expect_that(returnedDog$getName(), equals("dog"))
204 test_that(paste("get returns NULL if file or folder",
205 "with given name doesn't exists"), {
207 animal <- Subcollection$new("animal")
208 fish <- Subcollection$new("fish")
212 returnedDogIsNull <- is.null(animal$get("dog"))
214 expect_that(returnedDogIsNull, is_true())
217 test_that("getFirst returns first child in the subcollection", {
219 animal <- Subcollection$new("animal")
220 fish <- Subcollection$new("fish")
224 expect_that(animal$getFirst()$getName(), equals("fish"))
227 test_that("getFirst returns NULL if subcollection contains no children", {
229 animal <- Subcollection$new("animal")
231 returnedElementIsNull <- is.null(animal$getFirst())
233 expect_that(returnedElementIsNull, is_true())
236 test_that(paste("setCollection by default sets collection",
237 "filed of subcollection and all its children"), {
239 animal <- Subcollection$new("animal")
240 fish <- Subcollection$new("fish")
243 animal$setCollection("myCollection")
245 expect_that(animal$getCollection(), equals("myCollection"))
246 expect_that(fish$getCollection(), equals("myCollection"))
249 test_that(paste("setCollection sets collection filed of subcollection only",
250 "if parameter setRecursively is set to FALSE"), {
252 animal <- Subcollection$new("animal")
253 fish <- Subcollection$new("fish")
256 animal$setCollection("myCollection", setRecursively = FALSE)
257 fishCollectionIsNull <- is.null(fish$getCollection())
259 expect_that(animal$getCollection(), equals("myCollection"))
260 expect_that(fishCollectionIsNull, is_true())
263 test_that(paste("move raises exception if subcollection",
264 "doesn't belong to any collection"), {
266 animal <- Subcollection$new("animal")
268 expect_that(animal$move("new/location"),
269 throws_error("Subcollection doesn't belong to any collection"))
272 test_that("move raises exception if new location contains content with the same name", {
274 collectionContent <- c("animal",
279 fakeREST <- FakeRESTService$new(collectionContent)
281 api <- Arvados$new("myToken", "myHostName")
282 api$setRESTService(fakeREST)
283 collection <- Collection$new(api, "myUUID")
284 fish <- collection$get("animal/fish")
286 expect_that(fish$move("fish"),
287 throws_error("Destination already contains content with same name."))
291 test_that(paste("move raises exception if newLocationInCollection",
292 "parameter is invalid"), {
294 collectionContent <- c("animal",
299 fakeREST <- FakeRESTService$new(collectionContent)
301 api <- Arvados$new("myToken", "myHostName")
302 api$setRESTService(fakeREST)
304 collection <- Collection$new(api, "myUUID")
305 fish <- collection$get("animal/fish")
307 expect_that(fish$move("objects/dog"),
308 throws_error("Unable to get destination subcollection"))
311 test_that("move moves subcollection inside collection tree", {
313 collectionContent <- c("animal",
318 fakeREST <- FakeRESTService$new(collectionContent)
320 api <- Arvados$new("myToken", "myHostName")
321 api$setRESTService(fakeREST)
322 collection <- Collection$new(api, "myUUID")
323 fish <- collection$get("animal/fish")
326 fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
327 fishExistsOnNewLocation <- !is.null(collection$get("fish"))
329 expect_that(fishIsNullOnOldLocation, is_true())
330 expect_that(fishExistsOnNewLocation, is_true())
333 test_that(paste("getSizeInBytes returns zero if subcollection",
334 "is not part of a collection"), {
336 animal <- Subcollection$new("animal")
338 expect_that(animal$getSizeInBytes(), equals(0))
341 test_that(paste("getSizeInBytes delegates size calculation",
342 "to REST service class"), {
344 collectionContent <- c("animal", "animal/fish")
346 fakeREST <- FakeRESTService$new(collectionContent, returnSize)
348 api <- Arvados$new("myToken", "myHostName")
349 api$setRESTService(fakeREST)
350 collection <- Collection$new(api, "myUUID")
351 animal <- collection$get("animal")
353 resourceSize <- animal$getSizeInBytes()
355 expect_that(resourceSize, equals(100))