Merge branch 'master' of git.curoverse.com:arvados into 13076-r-autogen-api
[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
121     collection <- Collection$new("fakeUUID")
122     collection$setRESTService(fakeREST)
123     animal <- collection$get("animal")
124     dog <- ArvadosFile$new("dog")
125
126     animal$add(dog)
127
128     expect_that(fakeREST$createCallCount, equals(1))
129 }) 
130
131 test_that("remove removes content from subcollection", {
132
133     animal <- Subcollection$new("animal")
134     fish   <- Subcollection$new("fish")
135
136     animal$add(fish)
137     animal$remove("fish")
138
139     returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
140
141     expect_that(returnValueAfterRemovalIsNull, is_true())
142 }) 
143
144 test_that(paste("remove raises exception", 
145                 "if content to remove doesn't exist in the subcollection"), {
146
147     animal <- Subcollection$new("animal")
148
149     expect_that(animal$remove("fish"),
150                 throws_error(paste("Subcollection doesn't contains ArvadosFile",
151                                    "or Subcollection with specified name.")))
152 }) 
153
154 test_that("remove raises exception if passed argument is not character vector", {
155
156     animal <- Subcollection$new("animal")
157     number <- 10
158
159     expect_that(animal$remove(number),
160                 throws_error(paste("Expected character,",
161                                    "got (numeric)."), fixed = TRUE))
162 }) 
163
164 test_that(paste("remove removes content from REST service", 
165                 "if subcollection belongs to a collection"), {
166     
167     collectionContent <- c("animal", "animal/fish", "animal/dog")
168     fakeREST <- FakeRESTService$new(collectionContent)
169
170     collection <- Collection$new("fakeUUID")
171     collection$setRESTService(fakeREST)
172     animal <- collection$get("animal")
173
174     animal$remove("fish")
175
176     expect_that(fakeREST$deleteCallCount, equals(1))
177 }) 
178
179 test_that(paste("get returns ArvadosFile or Subcollection", 
180                 "if file or folder with given name exists"), {
181
182     animal <- Subcollection$new("animal")
183     fish   <- Subcollection$new("fish")
184     dog    <- ArvadosFile$new("dog")
185
186     animal$add(fish)
187     animal$add(dog)
188
189     returnedFish <- animal$get("fish")
190     returnedDog  <- animal$get("dog")
191
192     returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
193     returnedDogIsArvadosFile    <- "ArvadosFile"   %in% class(returnedDog)
194
195     expect_that(returnedFishIsSubcollection, is_true())
196     expect_that(returnedFish$getName(), equals("fish"))
197
198     expect_that(returnedDogIsArvadosFile, is_true())
199     expect_that(returnedDog$getName(), equals("dog"))
200 }) 
201
202 test_that(paste("get returns NULL if file or folder", 
203                 "with given name doesn't exists"), {
204
205     animal <- Subcollection$new("animal")
206     fish   <- Subcollection$new("fish")
207
208     animal$add(fish)
209
210     returnedDogIsNull <- is.null(animal$get("dog"))
211
212     expect_that(returnedDogIsNull, is_true())
213 }) 
214
215 test_that("getFirst returns first child in the subcollection", {
216
217     animal <- Subcollection$new("animal")
218     fish   <- Subcollection$new("fish")
219
220     animal$add(fish)
221
222     expect_that(animal$getFirst()$getName(), equals("fish"))
223 }) 
224
225 test_that("getFirst returns NULL if subcollection contains no children", {
226
227     animal <- Subcollection$new("animal")
228
229     returnedElementIsNull <- is.null(animal$getFirst())
230
231     expect_that(returnedElementIsNull, is_true())
232 }) 
233
234 test_that(paste("setCollection by default sets collection",
235                 "filed of subcollection and all its children"), {
236
237     animal <- Subcollection$new("animal")
238     fish   <- Subcollection$new("fish")
239     animal$add(fish)
240
241     animal$setCollection("myCollection")
242
243     expect_that(animal$getCollection(), equals("myCollection"))
244     expect_that(fish$getCollection(), equals("myCollection"))
245 }) 
246
247 test_that(paste("setCollection sets collection filed of subcollection only",
248                 "if parameter setRecursively is set to FALSE"), {
249
250     animal <- Subcollection$new("animal")
251     fish   <- Subcollection$new("fish")
252     animal$add(fish)
253
254     animal$setCollection("myCollection", setRecursively = FALSE)
255     fishCollectionIsNull <- is.null(fish$getCollection())
256
257     expect_that(animal$getCollection(), equals("myCollection"))
258     expect_that(fishCollectionIsNull, is_true())
259 }) 
260
261 test_that(paste("move raises exception if subcollection",
262                 "doesn't belong to any collection"), {
263
264     animal <- Subcollection$new("animal")
265
266     expect_that(animal$move("new/location"),
267                 throws_error("Subcollection doesn't belong to any collection"))
268 }) 
269
270 test_that("move raises exception if new location contains content with the same name", {
271
272     collectionContent <- c("animal",
273                            "animal/fish",
274                            "animal/dog",
275                            "animal/fish/shark",
276                            "fish")
277     fakeREST <- FakeRESTService$new(collectionContent)
278
279     collection <- Collection$new("fakeUUID")
280     collection$setRESTService(fakeREST)
281     fish <- collection$get("animal/fish")
282
283     expect_that(fish$move("fish"),
284                 throws_error("Destination already contains content with same name."))
285
286 }) 
287
288 test_that(paste("move raises exception if newLocationInCollection",
289                 "parameter is invalid"), {
290
291     collectionContent <- c("animal",
292                            "animal/fish",
293                            "animal/dog",
294                            "animal/fish/shark",
295                            "ball")
296     fakeREST <- FakeRESTService$new(collectionContent)
297
298     collection <- Collection$new("fakeUUID")
299     collection$setRESTService(fakeREST)
300     fish <- collection$get("animal/fish")
301
302     expect_that(fish$move("objects/dog"),
303                 throws_error("Unable to get destination subcollection"))
304 }) 
305
306 test_that("move moves subcollection inside collection tree", {
307
308     collectionContent <- c("animal",
309                            "animal/fish",
310                            "animal/dog",
311                            "animal/fish/shark",
312                            "ball")
313     fakeREST <- FakeRESTService$new(collectionContent)
314
315     collection <- Collection$new("fakeUUID")
316     collection$setRESTService(fakeREST)
317     fish <- collection$get("animal/fish")
318
319     fish$move("fish")
320     fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
321     fishExistsOnNewLocation <- !is.null(collection$get("fish"))
322
323     expect_that(fishIsNullOnOldLocation, is_true())
324     expect_that(fishExistsOnNewLocation, is_true())
325 }) 
326
327 test_that(paste("getSizeInBytes returns zero if subcollection",
328                 "is not part of a collection"), {
329
330     animal <- Subcollection$new("animal")
331
332     expect_that(animal$getSizeInBytes(), equals(0))
333 }) 
334
335 test_that(paste("getSizeInBytes delegates size calculation",
336                 "to REST service class"), {
337
338     collectionContent <- c("animal", "animal/fish")
339     returnSize <- 100
340     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
341
342     collection <- Collection$new("fakeUUID")
343     collection$setRESTService(fakeREST)
344     animal <- collection$get("animal")
345
346     resourceSize <- animal$getSizeInBytes()
347
348     expect_that(resourceSize, equals(100))
349 })