Added unit test for ArvadosFile and Collection classes
[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 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     expectedResult <- c("animal/fish/shark", "animal/fish/blueFish")
30
31     resultsMatch <- length(expectedResult) == length(result) &&
32                     all(expectedResult == result)
33
34     expect_that(resultsMatch, is_true())
35 }) 
36
37 test_that(paste("getFileListing returns names of all direct children",
38                 "if fullPath is set to FALSE"), {
39
40     animal <- Subcollection$new("animal")
41     fish   <- Subcollection$new("fish")
42     shark  <- ArvadosFile$new("shark")
43     dog    <- ArvadosFile$new("dog")
44
45     animal$add(fish)
46     animal$add(dog)
47     fish$add(shark)
48
49     result <- animal$getFileListing(fullPath = FALSE)
50     expectedResult <- c("fish", "dog")
51
52     resultsMatch <- length(expectedResult) == length(result) &&
53                     all(expectedResult == result)
54
55     expect_that(resultsMatch, is_true())
56 }) 
57
58 test_that("add adds content to inside collection tree", {
59
60     animal <- Subcollection$new("animal")
61     fish   <- Subcollection$new("fish")
62     dog    <- ArvadosFile$new("dog")
63
64     animal$add(fish)
65     animal$add(dog)
66
67     animalContainsFish <- animal$get("fish")$getName() == fish$getName()
68     animalContainsDog  <- animal$get("dog")$getName()  == dog$getName()
69
70     expect_that(animalContainsFish, is_true())
71     expect_that(animalContainsDog, is_true())
72 }) 
73
74 test_that(paste("add raises exception if ArvadosFile/Subcollection", 
75                 "with same name already exists in the subcollection"), {
76
77     animal     <- Subcollection$new("animal")
78     fish       <- Subcollection$new("fish")
79     secondFish <- Subcollection$new("fish")
80     thirdFish  <- ArvadosFile$new("fish")
81
82     animal$add(fish)
83
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))
90 }) 
91
92 test_that(paste("add raises exception if passed argument is", 
93                 "not ArvadosFile or Subcollection"), {
94
95     animal <- Subcollection$new("animal")
96     number <- 10
97
98     expect_that(animal$add(number),
99                 throws_error(paste("Expected AravodsFile or Subcollection object,",
100                                    "got (numeric)."), fixed = TRUE))
101 }) 
102
103 test_that(paste("add post content to a REST service", 
104                 "if subcollection belongs to a collection"), {
105     
106     api <- Arvados$new("myToken", "myHostName")
107     api$setHttpClient(FakeHttpRequest$new())
108     api$setHttpParser(FakeHttpParser$new())
109
110     collectionContent <- c("animal", "animal/fish")
111     fakeREST <- FakeRESTService$new(collectionContent)
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     api <- Arvados$new("myToken", "myHostName")
160     api$setHttpClient(FakeHttpRequest$new())
161     api$setHttpParser(FakeHttpParser$new())
162
163     collectionContent <- c("animal", "animal/fish", "animal/dog")
164
165     fakeREST <- FakeRESTService$new(collectionContent)
166     api$setRESTService(fakeREST)
167     collection <- Collection$new(api, "myUUID")
168     animal <- collection$get("animal")
169
170     animal$remove("fish")
171
172     expect_that(fakeREST$deleteCallCount, equals(1))
173 }) 
174
175 test_that(paste("get returns ArvadosFile or Subcollection", 
176                 "if file or folder with given name exists"), {
177
178     animal <- Subcollection$new("animal")
179     fish   <- Subcollection$new("fish")
180     dog    <- ArvadosFile$new("dog")
181
182     animal$add(fish)
183     animal$add(dog)
184
185     returnedFish <- animal$get("fish")
186     returnedDog  <- animal$get("dog")
187
188     returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
189     returnedDogIsArvadosFile    <- "ArvadosFile"   %in% class(returnedDog)
190
191     expect_that(returnedFishIsSubcollection, is_true())
192     expect_that(returnedFish$getName(), equals("fish"))
193
194     expect_that(returnedDogIsArvadosFile, is_true())
195     expect_that(returnedDog$getName(), equals("dog"))
196 }) 
197
198 test_that(paste("get returns NULL if file or folder", 
199                 "with given name doesn't exists"), {
200
201     animal <- Subcollection$new("animal")
202     fish   <- Subcollection$new("fish")
203
204     animal$add(fish)
205
206     returnedDogIsNull <- is.null(animal$get("dog"))
207
208     expect_that(returnedDogIsNull, is_true())
209 }) 
210
211 test_that("getFirst returns first child in the subcollection", {
212
213     animal <- Subcollection$new("animal")
214     fish   <- Subcollection$new("fish")
215
216     animal$add(fish)
217
218     expect_that(animal$getFirst()$getName(), equals("fish"))
219 }) 
220
221 test_that("getFirst returns NULL if subcollection contains no children", {
222
223     animal <- Subcollection$new("animal")
224
225     returnedElementIsNull <- is.null(animal$getFirst())
226
227     expect_that(returnedElementIsNull, is_true())
228 }) 
229
230 test_that(paste("setCollection by default sets collection",
231                 "filed of subcollection and all its children"), {
232
233     animal <- Subcollection$new("animal")
234     fish   <- Subcollection$new("fish")
235     animal$add(fish)
236
237     animal$setCollection("myCollection")
238
239     expect_that(animal$getCollection(), equals("myCollection"))
240     expect_that(fish$getCollection(), equals("myCollection"))
241 }) 
242
243 test_that(paste("setCollection sets collection filed of subcollection only",
244                 "if parameter setRecursively is set to FALSE"), {
245
246     animal <- Subcollection$new("animal")
247     fish   <- Subcollection$new("fish")
248     animal$add(fish)
249
250     animal$setCollection("myCollection", setRecursively = FALSE)
251     fishCollectionIsNull <- is.null(fish$getCollection())
252
253     expect_that(animal$getCollection(), equals("myCollection"))
254     expect_that(fishCollectionIsNull, is_true())
255 }) 
256
257 test_that(paste("move raises exception if subcollection",
258                 "doesn't belong to any collection"), {
259
260     animal <- Subcollection$new("animal")
261
262     expect_that(animal$move("new/location"),
263                 throws_error("Subcollection doesn't belong to any collection"))
264 }) 
265
266 test_that("move raises exception if new location contains content with the same name", {
267
268     api <- Arvados$new("myToken", "myHostName")
269     api$setHttpClient(FakeHttpRequest$new())
270     api$setHttpParser(FakeHttpParser$new())
271
272     collectionContent <- c("animal",
273                            "animal/fish",
274                            "animal/dog",
275                            "animal/fish/shark",
276                            "fish")
277
278     fakeREST <- FakeRESTService$new(collectionContent)
279     api$setRESTService(fakeREST)
280     collection <- Collection$new(api, "myUUID")
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     api <- Arvados$new("myToken", "myHostName")
292     api$setHttpClient(FakeHttpRequest$new())
293     api$setHttpParser(FakeHttpParser$new())
294
295     collectionContent <- c("animal",
296                            "animal/fish",
297                            "animal/dog",
298                            "animal/fish/shark",
299                            "ball")
300
301     fakeREST <- FakeRESTService$new(collectionContent)
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     api <- Arvados$new("myToken", "myHostName")
314     api$setHttpClient(FakeHttpRequest$new())
315     api$setHttpParser(FakeHttpParser$new())
316
317     collectionContent <- c("animal",
318                            "animal/fish",
319                            "animal/dog",
320                            "animal/fish/shark",
321                            "ball")
322
323     fakeREST <- FakeRESTService$new(collectionContent)
324     api$setRESTService(fakeREST)
325     collection <- Collection$new(api, "myUUID")
326     fish <- collection$get("animal/fish")
327
328     fish$move("fish")
329     fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
330     fishExistsOnNewLocation <- !is.null(collection$get("fish"))
331
332     expect_that(fishIsNullOnOldLocation, is_true())
333     expect_that(fishExistsOnNewLocation, is_true())
334 }) 
335
336 test_that(paste("getSizeInBytes returns zero if subcollection",
337                 "is not part of a collection"), {
338
339     animal <- Subcollection$new("animal")
340
341     expect_that(animal$getSizeInBytes(), equals(0))
342 }) 
343
344 test_that(paste("getSizeInBytes delegates size calculation",
345                 "to REST service class"), {
346
347     api <- Arvados$new("myToken", "myHostName")
348     api$setHttpClient(FakeHttpRequest$new())
349     api$setHttpParser(FakeHttpParser$new())
350
351     collectionContent <- c("animal", "animal/fish")
352     returnSize <- 100
353
354     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
355     api$setRESTService(fakeREST)
356     collection <- Collection$new(api, "myUUID")
357     animal <- collection$get("animal")
358
359     resourceSize <- animal$getSizeInBytes()
360
361     expect_that(resourceSize, equals(100))
362 })