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 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()
29 expectedResult <- c("animal/fish/shark", "animal/fish/blueFish")
31 resultsMatch <- length(expectedResult) == length(result) &&
32 all(expectedResult == result)
34 expect_that(resultsMatch, is_true())
37 test_that(paste("getFileListing returns names of all direct children",
38 "if fullPath is set to FALSE"), {
40 animal <- Subcollection$new("animal")
41 fish <- Subcollection$new("fish")
42 shark <- ArvadosFile$new("shark")
43 dog <- ArvadosFile$new("dog")
49 result <- animal$getFileListing(fullPath = FALSE)
50 expectedResult <- c("fish", "dog")
52 resultsMatch <- length(expectedResult) == length(result) &&
53 all(expectedResult == result)
55 expect_that(resultsMatch, is_true())
58 test_that("add adds content to inside collection tree", {
60 animal <- Subcollection$new("animal")
61 fish <- Subcollection$new("fish")
62 dog <- ArvadosFile$new("dog")
67 animalContainsFish <- animal$get("fish")$getName() == fish$getName()
68 animalContainsDog <- animal$get("dog")$getName() == dog$getName()
70 expect_that(animalContainsFish, is_true())
71 expect_that(animalContainsDog, is_true())
74 test_that(paste("add raises exception if ArvadosFile/Subcollection",
75 "with same name already exists in the subcollection"), {
77 animal <- Subcollection$new("animal")
78 fish <- Subcollection$new("fish")
79 secondFish <- Subcollection$new("fish")
80 thirdFish <- ArvadosFile$new("fish")
84 expect_that(animal$add(secondFish),
85 throws_error(paste("Subcollection already contains ArvadosFile or",
86 "Subcollection with same name."), fixed = TRUE))
87 expect_that(animal$add(thirdFish),
88 throws_error(paste("Subcollection already contains ArvadosFile or",
89 "Subcollection with same name."), fixed = TRUE))
92 test_that(paste("add raises exception if passed argument is",
93 "not ArvadosFile or Subcollection"), {
95 animal <- Subcollection$new("animal")
98 expect_that(animal$add(number),
99 throws_error(paste("Expected AravodsFile or Subcollection object,",
100 "got (numeric)."), fixed = TRUE))
103 test_that(paste("add post content to a REST service",
104 "if subcollection belongs to a collection"), {
106 api <- Arvados$new("myToken", "myHostName")
107 api$setHttpClient(FakeHttpRequest$new())
108 api$setHttpParser(FakeHttpParser$new())
110 collectionContent <- c("animal", "animal/fish")
111 fakeREST <- FakeRESTService$new(collectionContent)
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 api <- Arvados$new("myToken", "myHostName")
160 api$setHttpClient(FakeHttpRequest$new())
161 api$setHttpParser(FakeHttpParser$new())
163 collectionContent <- c("animal", "animal/fish", "animal/dog")
165 fakeREST <- FakeRESTService$new(collectionContent)
166 api$setRESTService(fakeREST)
167 collection <- Collection$new(api, "myUUID")
168 animal <- collection$get("animal")
170 animal$remove("fish")
172 expect_that(fakeREST$deleteCallCount, equals(1))
175 test_that(paste("get returns ArvadosFile or Subcollection",
176 "if file or folder with given name exists"), {
178 animal <- Subcollection$new("animal")
179 fish <- Subcollection$new("fish")
180 dog <- ArvadosFile$new("dog")
185 returnedFish <- animal$get("fish")
186 returnedDog <- animal$get("dog")
188 returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
189 returnedDogIsArvadosFile <- "ArvadosFile" %in% class(returnedDog)
191 expect_that(returnedFishIsSubcollection, is_true())
192 expect_that(returnedFish$getName(), equals("fish"))
194 expect_that(returnedDogIsArvadosFile, is_true())
195 expect_that(returnedDog$getName(), equals("dog"))
198 test_that(paste("get returns NULL if file or folder",
199 "with given name doesn't exists"), {
201 animal <- Subcollection$new("animal")
202 fish <- Subcollection$new("fish")
206 returnedDogIsNull <- is.null(animal$get("dog"))
208 expect_that(returnedDogIsNull, is_true())
211 test_that("getFirst returns first child in the subcollection", {
213 animal <- Subcollection$new("animal")
214 fish <- Subcollection$new("fish")
218 expect_that(animal$getFirst()$getName(), equals("fish"))
221 test_that("getFirst returns NULL if subcollection contains no children", {
223 animal <- Subcollection$new("animal")
225 returnedElementIsNull <- is.null(animal$getFirst())
227 expect_that(returnedElementIsNull, is_true())
230 test_that(paste("setCollection by default sets collection",
231 "filed of subcollection and all its children"), {
233 animal <- Subcollection$new("animal")
234 fish <- Subcollection$new("fish")
237 animal$setCollection("myCollection")
239 expect_that(animal$getCollection(), equals("myCollection"))
240 expect_that(fish$getCollection(), equals("myCollection"))
243 test_that(paste("setCollection sets collection filed of subcollection only",
244 "if parameter setRecursively is set to FALSE"), {
246 animal <- Subcollection$new("animal")
247 fish <- Subcollection$new("fish")
250 animal$setCollection("myCollection", setRecursively = FALSE)
251 fishCollectionIsNull <- is.null(fish$getCollection())
253 expect_that(animal$getCollection(), equals("myCollection"))
254 expect_that(fishCollectionIsNull, is_true())
257 test_that(paste("move raises exception if subcollection",
258 "doesn't belong to any collection"), {
260 animal <- Subcollection$new("animal")
262 expect_that(animal$move("new/location"),
263 throws_error("Subcollection doesn't belong to any collection"))
266 test_that("move raises exception if new location contains content with the same name", {
268 api <- Arvados$new("myToken", "myHostName")
269 api$setHttpClient(FakeHttpRequest$new())
270 api$setHttpParser(FakeHttpParser$new())
272 collectionContent <- c("animal",
278 fakeREST <- FakeRESTService$new(collectionContent)
279 api$setRESTService(fakeREST)
280 collection <- Collection$new(api, "myUUID")
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 api <- Arvados$new("myToken", "myHostName")
292 api$setHttpClient(FakeHttpRequest$new())
293 api$setHttpParser(FakeHttpParser$new())
295 collectionContent <- c("animal",
301 fakeREST <- FakeRESTService$new(collectionContent)
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 api <- Arvados$new("myToken", "myHostName")
314 api$setHttpClient(FakeHttpRequest$new())
315 api$setHttpParser(FakeHttpParser$new())
317 collectionContent <- c("animal",
323 fakeREST <- FakeRESTService$new(collectionContent)
324 api$setRESTService(fakeREST)
325 collection <- Collection$new(api, "myUUID")
326 fish <- collection$get("animal/fish")
329 fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
330 fishExistsOnNewLocation <- !is.null(collection$get("fish"))
332 expect_that(fishIsNullOnOldLocation, is_true())
333 expect_that(fishExistsOnNewLocation, is_true())
336 test_that(paste("getSizeInBytes returns zero if subcollection",
337 "is not part of a collection"), {
339 animal <- Subcollection$new("animal")
341 expect_that(animal$getSizeInBytes(), equals(0))
344 test_that(paste("getSizeInBytes delegates size calculation",
345 "to REST service class"), {
347 api <- Arvados$new("myToken", "myHostName")
348 api$setHttpClient(FakeHttpRequest$new())
349 api$setHttpParser(FakeHttpParser$new())
351 collectionContent <- c("animal", "animal/fish")
354 fakeREST <- FakeRESTService$new(collectionContent, returnSize)
355 api$setRESTService(fakeREST)
356 collection <- Collection$new(api, "myUUID")
357 animal <- collection$get("animal")
359 resourceSize <- animal$getSizeInBytes()
361 expect_that(resourceSize, equals(100))