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)
121 collection <- Collection$new("fakeUUID")
122 collection$setRESTService(fakeREST)
123 animal <- collection$get("animal")
124 dog <- ArvadosFile$new("dog")
128 expect_that(fakeREST$createCallCount, equals(1))
131 test_that("remove removes content from subcollection", {
133 animal <- Subcollection$new("animal")
134 fish <- Subcollection$new("fish")
137 animal$remove("fish")
139 returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
141 expect_that(returnValueAfterRemovalIsNull, is_true())
144 test_that(paste("remove raises exception",
145 "if content to remove doesn't exist in the subcollection"), {
147 animal <- Subcollection$new("animal")
149 expect_that(animal$remove("fish"),
150 throws_error(paste("Subcollection doesn't contains ArvadosFile",
151 "or Subcollection with specified name.")))
154 test_that("remove raises exception if passed argument is not character vector", {
156 animal <- Subcollection$new("animal")
159 expect_that(animal$remove(number),
160 throws_error(paste("Expected character,",
161 "got (numeric)."), fixed = TRUE))
164 test_that(paste("remove removes content from REST service",
165 "if subcollection belongs to a collection"), {
167 collectionContent <- c("animal", "animal/fish", "animal/dog")
168 fakeREST <- FakeRESTService$new(collectionContent)
170 collection <- Collection$new("fakeUUID")
171 collection$setRESTService(fakeREST)
172 animal <- collection$get("animal")
174 animal$remove("fish")
176 expect_that(fakeREST$deleteCallCount, equals(1))
179 test_that(paste("get returns ArvadosFile or Subcollection",
180 "if file or folder with given name exists"), {
182 animal <- Subcollection$new("animal")
183 fish <- Subcollection$new("fish")
184 dog <- ArvadosFile$new("dog")
189 returnedFish <- animal$get("fish")
190 returnedDog <- animal$get("dog")
192 returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
193 returnedDogIsArvadosFile <- "ArvadosFile" %in% class(returnedDog)
195 expect_that(returnedFishIsSubcollection, is_true())
196 expect_that(returnedFish$getName(), equals("fish"))
198 expect_that(returnedDogIsArvadosFile, is_true())
199 expect_that(returnedDog$getName(), equals("dog"))
202 test_that(paste("get returns NULL if file or folder",
203 "with given name doesn't exists"), {
205 animal <- Subcollection$new("animal")
206 fish <- Subcollection$new("fish")
210 returnedDogIsNull <- is.null(animal$get("dog"))
212 expect_that(returnedDogIsNull, is_true())
215 test_that("getFirst returns first child in the subcollection", {
217 animal <- Subcollection$new("animal")
218 fish <- Subcollection$new("fish")
222 expect_that(animal$getFirst()$getName(), equals("fish"))
225 test_that("getFirst returns NULL if subcollection contains no children", {
227 animal <- Subcollection$new("animal")
229 returnedElementIsNull <- is.null(animal$getFirst())
231 expect_that(returnedElementIsNull, is_true())
234 test_that(paste("setCollection by default sets collection",
235 "filed of subcollection and all its children"), {
237 animal <- Subcollection$new("animal")
238 fish <- Subcollection$new("fish")
241 animal$setCollection("myCollection")
243 expect_that(animal$getCollection(), equals("myCollection"))
244 expect_that(fish$getCollection(), equals("myCollection"))
247 test_that(paste("setCollection sets collection filed of subcollection only",
248 "if parameter setRecursively is set to FALSE"), {
250 animal <- Subcollection$new("animal")
251 fish <- Subcollection$new("fish")
254 animal$setCollection("myCollection", setRecursively = FALSE)
255 fishCollectionIsNull <- is.null(fish$getCollection())
257 expect_that(animal$getCollection(), equals("myCollection"))
258 expect_that(fishCollectionIsNull, is_true())
261 test_that(paste("move raises exception if subcollection",
262 "doesn't belong to any collection"), {
264 animal <- Subcollection$new("animal")
266 expect_that(animal$move("new/location"),
267 throws_error("Subcollection doesn't belong to any collection"))
270 test_that("move raises exception if new location contains content with the same name", {
272 collectionContent <- c("animal",
277 fakeREST <- FakeRESTService$new(collectionContent)
279 collection <- Collection$new("fakeUUID")
280 collection$setRESTService(fakeREST)
281 fish <- collection$get("animal/fish")
283 expect_that(fish$move("fish"),
284 throws_error("Destination already contains content with same name."))
288 test_that(paste("move raises exception if newLocationInCollection",
289 "parameter is invalid"), {
291 collectionContent <- c("animal",
296 fakeREST <- FakeRESTService$new(collectionContent)
298 collection <- Collection$new("fakeUUID")
299 collection$setRESTService(fakeREST)
300 fish <- collection$get("animal/fish")
302 expect_that(fish$move("objects/dog"),
303 throws_error("Unable to get destination subcollection"))
306 test_that("move moves subcollection inside collection tree", {
308 collectionContent <- c("animal",
313 fakeREST <- FakeRESTService$new(collectionContent)
315 collection <- Collection$new("fakeUUID")
316 collection$setRESTService(fakeREST)
317 fish <- collection$get("animal/fish")
320 fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
321 fishExistsOnNewLocation <- !is.null(collection$get("fish"))
323 expect_that(fishIsNullOnOldLocation, is_true())
324 expect_that(fishExistsOnNewLocation, is_true())
327 test_that(paste("getSizeInBytes returns zero if subcollection",
328 "is not part of a collection"), {
330 animal <- Subcollection$new("animal")
332 expect_that(animal$getSizeInBytes(), equals(0))
335 test_that(paste("getSizeInBytes delegates size calculation",
336 "to REST service class"), {
338 collectionContent <- c("animal", "animal/fish")
340 fakeREST <- FakeRESTService$new(collectionContent, returnSize)
342 collection <- Collection$new("fakeUUID")
343 collection$setRESTService(fakeREST)
344 animal <- collection$get("animal")
346 resourceSize <- animal$getSizeInBytes()
348 expect_that(resourceSize, equals(100))