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 collectionContent <- c("animal", "animal/fish")
107 fakeREST <- FakeRESTService$new(collectionContent)
109 api <- Arvados$new("myToken", "myHostName")
110 api$setRESTService(fakeREST)
112 collection <- Collection$new(api, "myUUID")
113 animal <- collection$get("animal")
114 dog <- ArvadosFile$new("dog")
118 expect_that(fakeREST$createCallCount, equals(1))
121 test_that("remove removes content from subcollection", {
123 animal <- Subcollection$new("animal")
124 fish <- Subcollection$new("fish")
127 animal$remove("fish")
129 returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
131 expect_that(returnValueAfterRemovalIsNull, is_true())
134 test_that(paste("remove raises exception",
135 "if content to remove doesn't exist in the subcollection"), {
137 animal <- Subcollection$new("animal")
139 expect_that(animal$remove("fish"),
140 throws_error(paste("Subcollection doesn't contains ArvadosFile",
141 "or Subcollection with specified name.")))
144 test_that("remove raises exception if passed argument is not character vector", {
146 animal <- Subcollection$new("animal")
149 expect_that(animal$remove(number),
150 throws_error(paste("Expected character,",
151 "got (numeric)."), fixed = TRUE))
154 test_that(paste("remove removes content from REST service",
155 "if subcollection belongs to a collection"), {
157 collectionContent <- c("animal", "animal/fish", "animal/dog")
158 fakeREST <- FakeRESTService$new(collectionContent)
160 api <- Arvados$new("myToken", "myHostName")
161 api$setRESTService(fakeREST)
162 collection <- Collection$new(api, "myUUID")
163 animal <- collection$get("animal")
165 animal$remove("fish")
167 expect_that(fakeREST$deleteCallCount, equals(1))
170 test_that(paste("get returns ArvadosFile or Subcollection",
171 "if file or folder with given name exists"), {
173 animal <- Subcollection$new("animal")
174 fish <- Subcollection$new("fish")
175 dog <- ArvadosFile$new("dog")
180 returnedFish <- animal$get("fish")
181 returnedDog <- animal$get("dog")
183 returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
184 returnedDogIsArvadosFile <- "ArvadosFile" %in% class(returnedDog)
186 expect_that(returnedFishIsSubcollection, is_true())
187 expect_that(returnedFish$getName(), equals("fish"))
189 expect_that(returnedDogIsArvadosFile, is_true())
190 expect_that(returnedDog$getName(), equals("dog"))
193 test_that(paste("get returns NULL if file or folder",
194 "with given name doesn't exists"), {
196 animal <- Subcollection$new("animal")
197 fish <- Subcollection$new("fish")
201 returnedDogIsNull <- is.null(animal$get("dog"))
203 expect_that(returnedDogIsNull, is_true())
206 test_that("getFirst returns first child in the subcollection", {
208 animal <- Subcollection$new("animal")
209 fish <- Subcollection$new("fish")
213 expect_that(animal$getFirst()$getName(), equals("fish"))
216 test_that("getFirst returns NULL if subcollection contains no children", {
218 animal <- Subcollection$new("animal")
220 returnedElementIsNull <- is.null(animal$getFirst())
222 expect_that(returnedElementIsNull, is_true())
225 test_that(paste("setCollection by default sets collection",
226 "filed of subcollection and all its children"), {
228 animal <- Subcollection$new("animal")
229 fish <- Subcollection$new("fish")
232 animal$setCollection("myCollection")
234 expect_that(animal$getCollection(), equals("myCollection"))
235 expect_that(fish$getCollection(), equals("myCollection"))
238 test_that(paste("setCollection sets collection filed of subcollection only",
239 "if parameter setRecursively is set to FALSE"), {
241 animal <- Subcollection$new("animal")
242 fish <- Subcollection$new("fish")
245 animal$setCollection("myCollection", setRecursively = FALSE)
246 fishCollectionIsNull <- is.null(fish$getCollection())
248 expect_that(animal$getCollection(), equals("myCollection"))
249 expect_that(fishCollectionIsNull, is_true())
252 test_that(paste("move raises exception if subcollection",
253 "doesn't belong to any collection"), {
255 animal <- Subcollection$new("animal")
257 expect_that(animal$move("new/location"),
258 throws_error("Subcollection doesn't belong to any collection"))
261 test_that("move raises exception if new location contains content with the same name", {
263 collectionContent <- c("animal",
268 fakeREST <- FakeRESTService$new(collectionContent)
270 api <- Arvados$new("myToken", "myHostName")
271 api$setRESTService(fakeREST)
272 collection <- Collection$new(api, "myUUID")
273 fish <- collection$get("animal/fish")
275 expect_that(fish$move("fish"),
276 throws_error("Destination already contains content with same name."))
280 test_that(paste("move raises exception if newLocationInCollection",
281 "parameter is invalid"), {
283 collectionContent <- c("animal",
288 fakeREST <- FakeRESTService$new(collectionContent)
290 api <- Arvados$new("myToken", "myHostName")
291 api$setRESTService(fakeREST)
293 collection <- Collection$new(api, "myUUID")
294 fish <- collection$get("animal/fish")
296 expect_that(fish$move("objects/dog"),
297 throws_error("Unable to get destination subcollection"))
300 test_that("move moves subcollection inside collection tree", {
302 collectionContent <- c("animal",
307 fakeREST <- FakeRESTService$new(collectionContent)
309 api <- Arvados$new("myToken", "myHostName")
310 api$setRESTService(fakeREST)
311 collection <- Collection$new(api, "myUUID")
312 fish <- collection$get("animal/fish")
315 fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
316 fishExistsOnNewLocation <- !is.null(collection$get("fish"))
318 expect_that(fishIsNullOnOldLocation, is_true())
319 expect_that(fishExistsOnNewLocation, is_true())
322 test_that(paste("getSizeInBytes returns zero if subcollection",
323 "is not part of a collection"), {
325 animal <- Subcollection$new("animal")
327 expect_that(animal$getSizeInBytes(), equals(0))
330 test_that(paste("getSizeInBytes delegates size calculation",
331 "to REST service class"), {
333 collectionContent <- c("animal", "animal/fish")
335 fakeREST <- FakeRESTService$new(collectionContent, returnSize)
337 api <- Arvados$new("myToken", "myHostName")
338 api$setRESTService(fakeREST)
339 collection <- Collection$new(api, "myUUID")
340 animal <- collection$get("animal")
342 resourceSize <- animal$getSizeInBytes()
344 expect_that(resourceSize, equals(100))