Improve Collections create and move methods and update documentation
[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(paste("create adds files specified by fileNames",
108                 "to local tree structure and remote REST service"), {
109
110     fakeREST <- FakeRESTService$new()
111     api <- Arvados$new("myToken", "myHostName")
112     api$setRESTService(fakeREST)
113     collection <- Collection$new(api, "myUUID")
114
115     collection$create(c("animal/dog", "animal/cat"))
116
117     dog <- collection$get("animal/dog")
118     cat <- collection$get("animal/cat")
119     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
120     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
121
122     expect_that(dogExistsInCollection, is_true())
123     expect_that(catExistsInCollection, is_true())
124     expect_that(fakeREST$createCallCount, equals(2))
125 })
126
127 test_that("remove raises exception if passed argumet is not character vector", {
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     expect_that(collection$remove(10),
137                 throws_error("Expected character vector, got (numeric).",
138                              fixed = TRUE))
139 })
140
141 test_that("remove raises exception if user tries to remove root folder", {
142
143     collectionContent <- c("animal", "animal/fish")
144     fakeREST <- FakeRESTService$new(collectionContent)
145
146     api <- Arvados$new("myToken", "myHostName")
147     api$setRESTService(fakeREST)
148     collection <- Collection$new(api, "myUUID")
149
150     expect_that(collection$remove(""),
151                 throws_error("You can't delete root folder.", fixed = TRUE))
152 })
153
154 test_that(paste("remove removes files specified by paths",
155                 "from local tree structure and from remote REST service"), {
156
157     collectionContent <- c("animal", "animal/fish", "animal/dog", "animal/cat", "ball")
158     fakeREST <- FakeRESTService$new(collectionContent)
159
160     api <- Arvados$new("myToken", "myHostName")
161     api$setRESTService(fakeREST)
162     collection <- Collection$new(api, "myUUID")
163
164     collection$remove(c("animal/dog", "animal/cat"))
165
166     dog <- collection$get("animal/dog")
167     cat <- collection$get("animal/dog")
168     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
169     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
170
171     expect_that(dogExistsInCollection, is_false())
172     expect_that(catExistsInCollection, is_false())
173     expect_that(fakeREST$deleteCallCount, equals(2))
174 })
175
176 test_that(paste("move moves content to a new location inside file tree",
177                 "and on REST service"), {
178
179     collectionContent <- c("animal", "animal/dog", "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$move("animal/dog", "dog")
187
188     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
189     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
190
191     expect_that(dogIsNullOnOldLocation, is_true())
192     expect_that(dogExistsOnNewLocation, is_true())
193     expect_that(fakeREST$moveCallCount, equals(1))
194 })
195
196 test_that("move raises exception if new location is not valid", {
197
198     collectionContent <- c("animal", "animal/fish", "ball")
199     fakeREST <- FakeRESTService$new(collectionContent)
200
201     api <- Arvados$new("myToken", "myHostName")
202     api$setRESTService(fakeREST)
203     collection <- Collection$new(api, "myUUID")
204
205     expect_that(collection$move("fish", "object"),
206                 throws_error("Content you want to move doesn't exist in the collection.",
207                              fixed = TRUE))
208 })
209
210 test_that("getFileListing returns sorted collection content received from REST service", {
211
212     collectionContent <- c("animal", "animal/fish", "ball")
213     fakeREST <- FakeRESTService$new(collectionContent)
214
215     api <- Arvados$new("myToken", "myHostName")
216     api$setRESTService(fakeREST)
217     collection <- Collection$new(api, "myUUID")
218
219     contentMatchExpected <- all(collection$getFileListing() ==
220                                 c("animal", "animal/fish", "ball"))
221
222     expect_that(contentMatchExpected, is_true())
223     #2 calls because Collection$new calls getFileListing once
224     expect_that(fakeREST$getCollectionContentCallCount, equals(2))
225
226 })
227
228 test_that("get returns arvados file or subcollection from internal tree structure", {
229
230     collectionContent <- c("animal", "animal/fish", "ball")
231     fakeREST <- FakeRESTService$new(collectionContent)
232
233     api <- Arvados$new("myToken", "myHostName")
234     api$setRESTService(fakeREST)
235     collection <- Collection$new(api, "myUUID")
236
237     fish <- collection$get("animal/fish")
238     fishIsNotNull <- !is.null(fish)
239
240     expect_that(fishIsNotNull, is_true())
241     expect_that(fish$getName(), equals("fish"))
242 })
243
244 test_that(paste("copy copies content to a new location inside file tree",
245                 "and on REST service"), {
246
247     collectionContent <- c("animal", "animal/dog", "ball")
248     fakeREST <- FakeRESTService$new(collectionContent)
249
250     api <- Arvados$new("myToken", "myHostName")
251     api$setRESTService(fakeREST)
252     collection <- Collection$new(api, "myUUID")
253
254     collection$copy("animal/dog", "dog")
255
256     dogExistsOnOldLocation <- !is.null(collection$get("animal/dog"))
257     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
258
259     expect_that(dogExistsOnOldLocation, is_true())
260     expect_that(dogExistsOnNewLocation, is_true())
261     expect_that(fakeREST$copyCallCount, equals(1))
262 })
263
264 test_that("copy raises exception if new location is not valid", {
265
266     collectionContent <- c("animal", "animal/fish", "ball")
267     fakeREST <- FakeRESTService$new(collectionContent)
268
269     api <- Arvados$new("myToken", "myHostName")
270     api$setRESTService(fakeREST)
271     collection <- Collection$new(api, "myUUID")
272
273     expect_that(collection$copy("fish", "object"),
274                 throws_error("Content you want to copy doesn't exist in the collection.",
275                              fixed = TRUE))
276 })
277
278 test_that("refresh invalidates current tree structure", {
279
280     collectionContent <- c("animal", "animal/fish", "ball")
281     fakeREST <- FakeRESTService$new(collectionContent)
282
283     api <- Arvados$new("myToken", "myHostName")
284     api$setRESTService(fakeREST)
285     collection <- Collection$new(api, "aaaaa-j7d0g-ccccccccccccccc")
286
287     # Before refresh
288     fish <- collection$get("animal/fish")
289     expect_that(fish$getName(), equals("fish"))
290     expect_that(fish$getCollection()$uuid, equals("aaaaa-j7d0g-ccccccccccccccc"))
291
292     collection$refresh()
293
294     # After refresh
295     expect_that(fish$getName(), equals("fish"))
296     expect_true(is.null(fish$getCollection()))
297 })