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