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