Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / sdk / R / tests / testthat / test-Collection.R
1 source("fakes/FakeRESTService.R")
2
3 context("Collection")
4
5 test_that(paste("constructor creates file tree from text content",
6                 "retreived form REST service"), {
7
8     api <- Arvados$new("myToken", "myHostName")
9     api$setHttpClient(FakeHttpRequest$new())
10     api$setHttpParser(FakeHttpParser$new())
11
12     collectionContent <- c("animal",
13                            "animal/fish",
14                            "ball")
15
16     fakeREST <- FakeRESTService$new(collectionContent)
17     api$setRESTService(fakeREST)
18     collection <- Collection$new(api, "myUUID")
19
20     root <- collection$get("")
21
22     expect_that(fakeREST$getCollectionContentCallCount, equals(1))
23     expect_that(root$getName(), equals(""))
24 })
25
26 test_that(paste("add raises exception if passed argumet is not",
27                 "ArvadosFile or Subcollection"), {
28
29     api <- Arvados$new("myToken", "myHostName")
30     api$setHttpClient(FakeHttpRequest$new())
31     api$setHttpParser(FakeHttpParser$new())
32
33     collectionContent <- c("animal",
34                            "animal/fish",
35                            "ball")
36
37     fakeREST <- FakeRESTService$new(collectionContent)
38     api$setRESTService(fakeREST)
39     collection <- Collection$new(api, "myUUID")
40
41     newNumber <- 10
42
43     expect_that(collection$add(newNumber),
44     throws_error(paste("Expected AravodsFile or Subcollection",
45                        "object, got (numeric)."), fixed = TRUE))
46 })
47
48 test_that("add raises exception if relative path is not valid", {
49
50     api <- Arvados$new("myToken", "myHostName")
51     api$setHttpClient(FakeHttpRequest$new())
52     api$setHttpParser(FakeHttpParser$new())
53
54     collectionContent <- c("animal",
55                            "animal/fish",
56                            "ball")
57
58     fakeREST <- FakeRESTService$new(collectionContent)
59     api$setRESTService(fakeREST)
60     collection <- Collection$new(api, "myUUID")
61
62     newPen <- ArvadosFile$new("pen")
63
64     expect_that(collection$add(newPen, "objects"),
65                 throws_error("Subcollection objects doesn't exist.",
66                               fixed = TRUE))
67 })
68
69 test_that(paste("add adds ArvadosFile or Subcollection",
70                 "to local tree structure and remote REST service"), {
71
72     api <- Arvados$new("myToken", "myHostName")
73     api$setHttpClient(FakeHttpRequest$new())
74     api$setHttpParser(FakeHttpParser$new())
75
76     collectionContent <- c("animal",
77                            "animal/fish",
78                            "ball")
79
80     fakeREST <- FakeRESTService$new(collectionContent)
81     api$setRESTService(fakeREST)
82     collection <- Collection$new(api, "myUUID")
83
84     newDog <- ArvadosFile$new("dog")
85     collection$add(newDog, "animal")
86
87     dog <- collection$get("animal/dog")
88     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
89
90     expect_that(dogExistsInCollection, is_true())
91     expect_that(fakeREST$createCallCount, equals(1))
92 })
93
94 test_that("create raises exception if passed argumet is not character vector", {
95
96     api <- Arvados$new("myToken", "myHostName")
97     api$setHttpClient(FakeHttpRequest$new())
98     api$setHttpParser(FakeHttpParser$new())
99
100     collectionContent <- c("animal",
101                            "animal/fish",
102                            "ball")
103
104     fakeREST <- FakeRESTService$new(collectionContent)
105     api$setRESTService(fakeREST)
106     collection <- Collection$new(api, "myUUID")
107
108     expect_that(collection$create(10),
109                 throws_error("Expected character vector, got (numeric).", 
110                              fixed = TRUE))
111 })
112
113 test_that("create raises exception if relative path is not valid", {
114
115     api <- Arvados$new("myToken", "myHostName")
116     api$setHttpClient(FakeHttpRequest$new())
117     api$setHttpParser(FakeHttpParser$new())
118
119     collectionContent <- c("animal",
120                            "animal/fish",
121                            "ball")
122
123     fakeREST <- FakeRESTService$new(collectionContent)
124     api$setRESTService(fakeREST)
125     collection <- Collection$new(api, "myUUID")
126
127     newPen <- ArvadosFile$new("pen")
128
129     expect_that(collection$create(newPen, "objects"),
130                 throws_error("Subcollection objects doesn't exist.",
131                               fixed = TRUE))
132 })
133
134 test_that(paste("create adds files specified by fileNames",
135                 "to local tree structure and remote REST service"), {
136
137     api <- Arvados$new("myToken", "myHostName")
138     api$setHttpClient(FakeHttpRequest$new())
139     api$setHttpParser(FakeHttpParser$new())
140
141     collectionContent <- c("animal",
142                            "animal/fish",
143                            "ball")
144
145     fakeREST <- FakeRESTService$new(collectionContent)
146     api$setRESTService(fakeREST)
147     collection <- Collection$new(api, "myUUID")
148
149     files <- c("dog", "cat")
150     collection$create(files, "animal")
151
152     dog <- collection$get("animal/dog")
153     cat <- collection$get("animal/cat")
154     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
155     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
156
157     expect_that(dogExistsInCollection, is_true())
158     expect_that(catExistsInCollection, is_true())
159     expect_that(fakeREST$createCallCount, equals(2))
160 })
161
162 test_that("remove raises exception if passed argumet is not character vector", {
163
164     api <- Arvados$new("myToken", "myHostName")
165     api$setHttpClient(FakeHttpRequest$new())
166     api$setHttpParser(FakeHttpParser$new())
167
168     collectionContent <- c("animal",
169                            "animal/fish",
170                            "ball")
171
172     fakeREST <- FakeRESTService$new(collectionContent)
173     api$setRESTService(fakeREST)
174     collection <- Collection$new(api, "myUUID")
175
176     expect_that(collection$remove(10),
177                 throws_error("Expected character vector, got (numeric).", 
178                              fixed = TRUE))
179 })
180
181 test_that(paste("remove removes files specified by paths",
182                 "from local tree structure and from remote REST service"), {
183
184     api <- Arvados$new("myToken", "myHostName")
185     api$setHttpClient(FakeHttpRequest$new())
186     api$setHttpParser(FakeHttpParser$new())
187
188     collectionContent <- c("animal",
189                            "animal/fish",
190                            "animal/dog",
191                            "animal/cat",
192                            "ball")
193
194     fakeREST <- FakeRESTService$new(collectionContent)
195     api$setRESTService(fakeREST)
196     collection <- Collection$new(api, "myUUID")
197
198     collection$remove(c("animal/dog", "animal/cat"))
199
200     dog <- collection$get("animal/dog")
201     cat <- collection$get("animal/dog")
202     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
203     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
204
205     expect_that(dogExistsInCollection, is_false())
206     expect_that(catExistsInCollection, is_false())
207     expect_that(fakeREST$deleteCallCount, equals(2))
208 })
209
210 test_that(paste("move moves content to a new location inside file tree",
211                 "and on REST service"), {
212
213     api <- Arvados$new("myToken", "myHostName")
214     api$setHttpClient(FakeHttpRequest$new())
215     api$setHttpParser(FakeHttpParser$new())
216
217     collectionContent <- c("animal",
218                            "animal/dog",
219                            "ball")
220
221     fakeREST <- FakeRESTService$new(collectionContent)
222     api$setRESTService(fakeREST)
223     collection <- Collection$new(api, "myUUID")
224
225     collection$move("animal/dog", "dog")
226
227     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
228     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
229
230     expect_that(dogIsNullOnOldLocation, is_true())
231     expect_that(dogExistsOnNewLocation, is_true())
232     expect_that(fakeREST$moveCallCount, equals(1))
233 })
234
235 test_that("move raises exception if new location is not valid", {
236
237     api <- Arvados$new("myToken", "myHostName")
238     api$setHttpClient(FakeHttpRequest$new())
239     api$setHttpParser(FakeHttpParser$new())
240
241     collectionContent <- c("animal",
242                            "animal/fish",
243                            "ball")
244
245     fakeREST <- FakeRESTService$new(collectionContent)
246     api$setRESTService(fakeREST)
247     collection <- Collection$new(api, "myUUID")
248
249     expect_that(collection$move("fish", "object"),
250                 throws_error("Element you want to move doesn't exist in the collection.",
251                              fixed = TRUE))
252 })
253
254 test_that("getFileListing returns collection content received from REST service", {
255
256     api <- Arvados$new("myToken", "myHostName")
257     api$setHttpClient(FakeHttpRequest$new())
258     api$setHttpParser(FakeHttpParser$new())
259
260     collectionContent <- c("animal",
261                            "animal/fish",
262                            "ball")
263
264     fakeREST <- FakeRESTService$new(collectionContent)
265     api$setRESTService(fakeREST)
266     collection <- Collection$new(api, "myUUID")
267
268     contentMatchExpected <- all(collection$getFileListing() == 
269                                 c("animal", "animal/fish", "ball"))
270
271     expect_that(contentMatchExpected, is_true())
272     #2 calls because Collection$new calls getFileListing once
273     expect_that(fakeREST$getCollectionContentCallCount, equals(2))
274
275 })
276
277 test_that("get returns arvados file or subcollection from internal tree structure", {
278
279     api <- Arvados$new("myToken", "myHostName")
280     api$setHttpClient(FakeHttpRequest$new())
281     api$setHttpParser(FakeHttpParser$new())
282
283     collectionContent <- c("animal",
284                            "animal/fish",
285                            "ball")
286
287     fakeREST <- FakeRESTService$new(collectionContent)
288     api$setRESTService(fakeREST)
289     collection <- Collection$new(api, "myUUID")
290
291     fish <- collection$get("animal/fish")
292     fishIsNotNull <- !is.null(fish)
293
294     expect_that(fishIsNotNull, is_true())
295     expect_that(fish$getName(), equals("fish"))
296 })