b2b0bc9ccfecc66998e43f7d7b6de40c31a6d68e
[arvados.git] / sdk / R / tests / testthat / test-Subcollection.R
1 source("fakes/FakeRESTService.R")
2
3 context("Subcollection")
4
5 test_that("getRelativePath returns path relative to the tree root", {
6
7     animal <- Subcollection$new("animal")
8
9     fish <- Subcollection$new("fish")
10     animal$add(fish)
11
12     expect_that(animal$getRelativePath(), equals("animal"))
13     expect_that(fish$getRelativePath(), equals("animal/fish"))
14 }) 
15
16 test_that(paste("getFileListing by default returns sorted path of all files",
17                 "relative to the current subcollection"), {
18
19     animal   <- Subcollection$new("animal")
20     fish     <- Subcollection$new("fish")
21     shark    <- ArvadosFile$new("shark")
22     blueFish <- ArvadosFile$new("blueFish")
23
24     animal$add(fish)
25     fish$add(shark)
26     fish$add(blueFish)
27
28     result <- animal$getFileListing()
29
30     #expect sorted array
31     expectedResult <- c("animal/fish/blueFish", "animal/fish/shark")
32
33     resultsMatch <- length(expectedResult) == length(result) &&
34                     all(expectedResult == result)
35
36     expect_that(resultsMatch, is_true())
37 }) 
38
39 test_that(paste("getFileListing returns sorted names of all direct children",
40                 "if fullPath is set to FALSE"), {
41
42     animal <- Subcollection$new("animal")
43     fish   <- Subcollection$new("fish")
44     shark  <- ArvadosFile$new("shark")
45     dog    <- ArvadosFile$new("dog")
46
47     animal$add(fish)
48     animal$add(dog)
49     fish$add(shark)
50
51     result <- animal$getFileListing(fullPath = FALSE)
52     expectedResult <- c("dog", "fish")
53
54     resultsMatch <- length(expectedResult) == length(result) &&
55                     all(expectedResult == result)
56
57     expect_that(resultsMatch, is_true())
58 }) 
59
60 test_that("add adds content to inside collection tree", {
61
62     animal <- Subcollection$new("animal")
63     fish   <- Subcollection$new("fish")
64     dog    <- ArvadosFile$new("dog")
65
66     animal$add(fish)
67     animal$add(dog)
68
69     animalContainsFish <- animal$get("fish")$getName() == fish$getName()
70     animalContainsDog  <- animal$get("dog")$getName()  == dog$getName()
71
72     expect_that(animalContainsFish, is_true())
73     expect_that(animalContainsDog, is_true())
74 }) 
75
76 test_that("add raises exception if content name is empty string", {
77
78     animal     <- Subcollection$new("animal")
79     rootFolder <- Subcollection$new("")
80
81     expect_that(animal$add(rootFolder),
82                 throws_error("Content has invalid name.", fixed = TRUE))
83 })
84
85 test_that(paste("add raises exception if ArvadosFile/Subcollection", 
86                 "with same name already exists in the subcollection"), {
87
88     animal     <- Subcollection$new("animal")
89     fish       <- Subcollection$new("fish")
90     secondFish <- Subcollection$new("fish")
91     thirdFish  <- ArvadosFile$new("fish")
92
93     animal$add(fish)
94
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))
101 }) 
102
103 test_that(paste("add raises exception if passed argument is", 
104                 "not ArvadosFile or Subcollection"), {
105
106     animal <- Subcollection$new("animal")
107     number <- 10
108
109     expect_that(animal$add(number),
110                 throws_error(paste("Expected AravodsFile or Subcollection object,",
111                                    "got (numeric)."), fixed = TRUE))
112 }) 
113
114 test_that(paste("add post content to a REST service", 
115                 "if subcollection belongs to a collection"), {
116     
117     collectionContent <- c("animal", "animal/fish")
118     fakeREST <- FakeRESTService$new(collectionContent)
119
120     api <- Arvados$new("myToken", "myHostName")
121     api$setRESTService(fakeREST)
122
123     collection <- Collection$new(api, "myUUID")
124     animal <- collection$get("animal")
125     dog <- ArvadosFile$new("dog")
126
127     animal$add(dog)
128
129     expect_that(fakeREST$createCallCount, equals(1))
130 }) 
131
132 test_that("remove removes content from subcollection", {
133
134     animal <- Subcollection$new("animal")
135     fish   <- Subcollection$new("fish")
136
137     animal$add(fish)
138     animal$remove("fish")
139
140     returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
141
142     expect_that(returnValueAfterRemovalIsNull, is_true())
143 }) 
144
145 test_that(paste("remove raises exception", 
146                 "if content to remove doesn't exist in the subcollection"), {
147
148     animal <- Subcollection$new("animal")
149
150     expect_that(animal$remove("fish"),
151                 throws_error(paste("Subcollection doesn't contains ArvadosFile",
152                                    "or Subcollection with specified name.")))
153 }) 
154
155 test_that("remove raises exception if passed argument is not character vector", {
156
157     animal <- Subcollection$new("animal")
158     number <- 10
159
160     expect_that(animal$remove(number),
161                 throws_error(paste("Expected character,",
162                                    "got (numeric)."), fixed = TRUE))
163 }) 
164
165 test_that(paste("remove removes content from REST service", 
166                 "if subcollection belongs to a collection"), {
167     
168     collectionContent <- c("animal", "animal/fish", "animal/dog")
169     fakeREST <- FakeRESTService$new(collectionContent)
170
171     api <- Arvados$new("myToken", "myHostName")
172     api$setRESTService(fakeREST)
173     collection <- Collection$new(api, "myUUID")
174     animal <- collection$get("animal")
175
176     animal$remove("fish")
177
178     expect_that(fakeREST$deleteCallCount, equals(1))
179 }) 
180
181 test_that(paste("get returns ArvadosFile or Subcollection", 
182                 "if file or folder with given name exists"), {
183
184     animal <- Subcollection$new("animal")
185     fish   <- Subcollection$new("fish")
186     dog    <- ArvadosFile$new("dog")
187
188     animal$add(fish)
189     animal$add(dog)
190
191     returnedFish <- animal$get("fish")
192     returnedDog  <- animal$get("dog")
193
194     returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
195     returnedDogIsArvadosFile    <- "ArvadosFile"   %in% class(returnedDog)
196
197     expect_that(returnedFishIsSubcollection, is_true())
198     expect_that(returnedFish$getName(), equals("fish"))
199
200     expect_that(returnedDogIsArvadosFile, is_true())
201     expect_that(returnedDog$getName(), equals("dog"))
202 }) 
203
204 test_that(paste("get returns NULL if file or folder", 
205                 "with given name doesn't exists"), {
206
207     animal <- Subcollection$new("animal")
208     fish   <- Subcollection$new("fish")
209
210     animal$add(fish)
211
212     returnedDogIsNull <- is.null(animal$get("dog"))
213
214     expect_that(returnedDogIsNull, is_true())
215 }) 
216
217 test_that("getFirst returns first child in the subcollection", {
218
219     animal <- Subcollection$new("animal")
220     fish   <- Subcollection$new("fish")
221
222     animal$add(fish)
223
224     expect_that(animal$getFirst()$getName(), equals("fish"))
225 }) 
226
227 test_that("getFirst returns NULL if subcollection contains no children", {
228
229     animal <- Subcollection$new("animal")
230
231     returnedElementIsNull <- is.null(animal$getFirst())
232
233     expect_that(returnedElementIsNull, is_true())
234 }) 
235
236 test_that(paste("setCollection by default sets collection",
237                 "filed of subcollection and all its children"), {
238
239     animal <- Subcollection$new("animal")
240     fish   <- Subcollection$new("fish")
241     animal$add(fish)
242
243     animal$setCollection("myCollection")
244
245     expect_that(animal$getCollection(), equals("myCollection"))
246     expect_that(fish$getCollection(), equals("myCollection"))
247 }) 
248
249 test_that(paste("setCollection sets collection filed of subcollection only",
250                 "if parameter setRecursively is set to FALSE"), {
251
252     animal <- Subcollection$new("animal")
253     fish   <- Subcollection$new("fish")
254     animal$add(fish)
255
256     animal$setCollection("myCollection", setRecursively = FALSE)
257     fishCollectionIsNull <- is.null(fish$getCollection())
258
259     expect_that(animal$getCollection(), equals("myCollection"))
260     expect_that(fishCollectionIsNull, is_true())
261 }) 
262
263 test_that(paste("move raises exception if subcollection",
264                 "doesn't belong to any collection"), {
265
266     animal <- Subcollection$new("animal")
267
268     expect_that(animal$move("new/location"),
269                 throws_error("Subcollection doesn't belong to any collection"))
270 }) 
271
272 test_that("move raises exception if new location contains content with the same name", {
273
274     collectionContent <- c("animal",
275                            "animal/fish",
276                            "animal/dog",
277                            "animal/fish/shark",
278                            "fish")
279     fakeREST <- FakeRESTService$new(collectionContent)
280
281     api <- Arvados$new("myToken", "myHostName")
282     api$setRESTService(fakeREST)
283     collection <- Collection$new(api, "myUUID")
284     fish <- collection$get("animal/fish")
285
286     expect_that(fish$move("fish"),
287                 throws_error("Destination already contains content with same name."))
288
289 }) 
290
291 test_that(paste("move raises exception if newLocationInCollection",
292                 "parameter is invalid"), {
293
294     collectionContent <- c("animal",
295                            "animal/fish",
296                            "animal/dog",
297                            "animal/fish/shark",
298                            "ball")
299     fakeREST <- FakeRESTService$new(collectionContent)
300
301     api <- Arvados$new("myToken", "myHostName")
302     api$setRESTService(fakeREST)
303
304     collection <- Collection$new(api, "myUUID")
305     fish <- collection$get("animal/fish")
306
307     expect_that(fish$move("objects/dog"),
308                 throws_error("Unable to get destination subcollection"))
309 }) 
310
311 test_that("move moves subcollection inside collection tree", {
312
313     collectionContent <- c("animal",
314                            "animal/fish",
315                            "animal/dog",
316                            "animal/fish/shark",
317                            "ball")
318     fakeREST <- FakeRESTService$new(collectionContent)
319
320     api <- Arvados$new("myToken", "myHostName")
321     api$setRESTService(fakeREST)
322     collection <- Collection$new(api, "myUUID")
323     fish <- collection$get("animal/fish")
324
325     fish$move("fish")
326     fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
327     fishExistsOnNewLocation <- !is.null(collection$get("fish"))
328
329     expect_that(fishIsNullOnOldLocation, is_true())
330     expect_that(fishExistsOnNewLocation, is_true())
331 }) 
332
333 test_that(paste("getSizeInBytes returns zero if subcollection",
334                 "is not part of a collection"), {
335
336     animal <- Subcollection$new("animal")
337
338     expect_that(animal$getSizeInBytes(), equals(0))
339 }) 
340
341 test_that(paste("getSizeInBytes delegates size calculation",
342                 "to REST service class"), {
343
344     collectionContent <- c("animal", "animal/fish")
345     returnSize <- 100
346     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
347
348     api <- Arvados$new("myToken", "myHostName")
349     api$setRESTService(fakeREST)
350     collection <- Collection$new(api, "myUUID")
351     animal <- collection$get("animal")
352
353     resourceSize <- animal$getSizeInBytes()
354
355     expect_that(resourceSize, equals(100))
356 })