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