Merge branch 'main' from workbench2.git
[arvados.git] / sdk / R / tests / testthat / test-ArvadosFile.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("ArvadosFile")
8
9 test_that("constructor raises error if  file name is empty string", {
10
11     expect_that(ArvadosFile$new(""), throws_error("Invalid name."))
12 })
13
14 test_that("getFileListing always returns file name", {
15
16     dog <- ArvadosFile$new("dog")
17
18     expect_that(dog$getFileListing(), equals("dog"))
19 })
20
21 test_that("get always returns NULL", {
22
23     dog <- ArvadosFile$new("dog")
24
25     responseIsNull <- is.null(dog$get("something"))
26     expect_true(responseIsNull)
27 })
28
29 test_that("getFirst always returns NULL", {
30
31     dog <- ArvadosFile$new("dog")
32
33     responseIsNull <- is.null(dog$getFirst())
34     expect_true(responseIsNull)
35 })
36
37 test_that(paste("getSizeInBytes returns zero if arvadosFile",
38                 "is not part of a collection"), {
39
40     dog <- ArvadosFile$new("dog")
41
42     expect_that(dog$getSizeInBytes(), equals(0))
43 })
44
45 test_that(paste("getSizeInBytes delegates size calculation",
46                 "to REST service class"), {
47
48     collectionContent <- c("animal", "animal/fish")
49     returnSize <- 100
50     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
51
52     api <- Arvados$new("myToken", "myHostName")
53     api$setRESTService(fakeREST)
54     collection <- Collection$new(api, "myUUID")
55     fish <- collection$get("animal/fish")
56
57     resourceSize <- fish$getSizeInBytes()
58
59     expect_that(resourceSize, equals(100))
60 })
61
62 test_that("getRelativePath returns path relative to the tree root", {
63
64     animal <- Subcollection$new("animal")
65     fish <- Subcollection$new("fish")
66     shark <- ArvadosFile$new("shark")
67
68     animal$add(fish)
69     fish$add(shark)
70
71     expect_that(shark$getRelativePath(), equals("animal/fish/shark"))
72 })
73
74 test_that("read raises exception if file doesn't belong to a collection", {
75
76     dog <- ArvadosFile$new("dog")
77
78     expect_that(dog$read(),
79                 throws_error("ArvadosFile doesn't belong to any collection."))
80 })
81
82 test_that("read raises exception offset or length is negative number", {
83
84     collectionContent <- c("animal", "animal/fish")
85     fakeREST <- FakeRESTService$new(collectionContent)
86
87     api <- Arvados$new("myToken", "myHostName")
88     api$setRESTService(fakeREST)
89     collection <- Collection$new(api, "myUUID")
90     fish <- collection$get("animal/fish")
91
92     expect_that(fish$read(contentType = "text", offset = -1),
93                 throws_error("Offset and length must be positive values."))
94     expect_that(fish$read(contentType = "text", length = -1),
95                 throws_error("Offset and length must be positive values."))
96     expect_that(fish$read(contentType = "text", offset = -1, length = -1),
97                 throws_error("Offset and length must be positive values."))
98 })
99
100 test_that("read delegates reading operation to REST service class", {
101
102     collectionContent <- c("animal", "animal/fish")
103     readContent <- "my file"
104     fakeREST <- FakeRESTService$new(collectionContent, readContent)
105
106     api <- Arvados$new("myToken", "myHostName")
107     api$setRESTService(fakeREST)
108     collection <- Collection$new(api, "myUUID")
109     fish <- collection$get("animal/fish")
110
111     fileContent <- fish$read("text")
112
113     expect_that(fileContent, equals("my file"))
114     expect_that(fakeREST$readCallCount, equals(1))
115 })
116
117 test_that(paste("connection delegates connection creation ro RESTService class",
118                 "which returns curl connection opened in read mode when",
119                 "'r' of 'rb' is passed as argument"), {
120
121     collectionContent <- c("animal", "animal/fish")
122     fakeREST <- FakeRESTService$new(collectionContent)
123
124     api <- Arvados$new("myToken", "myHostName")
125     api$setRESTService(fakeREST)
126     collection <- Collection$new(api, "myUUID")
127     fish <- collection$get("animal/fish")
128
129     connection <- fish$connection("r")
130
131     expect_that(fakeREST$getConnectionCallCount, equals(1))
132 })
133
134 test_that(paste("connection returns textConnection opened",
135                 "in write mode when 'w' is passed as argument"), {
136
137     collectionContent <- c("animal", "animal/fish")
138     fakeREST <- FakeRESTService$new(collectionContent)
139
140     api <- Arvados$new("myToken", "myHostName")
141     api$setRESTService(fakeREST)
142     collection <- Collection$new(api, "myUUID")
143     fish <- collection$get("animal/fish")
144
145     connection <- fish$connection("w")
146
147     writeLines("file", connection)
148     writeLines("content", connection)
149
150     writeResult <- textConnectionValue(connection)
151
152     expect_that(writeResult[1], equals("file"))
153     expect_that(writeResult[2], equals("content"))
154 })
155
156 test_that("flush sends data stored in a connection to a REST server", {
157
158     collectionContent <- c("animal", "animal/fish")
159     fakeREST <- FakeRESTService$new(collectionContent)
160
161     api <- Arvados$new("myToken", "myHostName")
162     api$setRESTService(fakeREST)
163     collection <- Collection$new(api, "myUUID")
164     fish <- collection$get("animal/fish")
165
166     connection <- fish$connection("w")
167
168     writeLines("file content", connection)
169
170     fish$flush()
171
172     expect_that(fakeREST$writeBuffer, equals("file content"))
173 })
174
175 test_that("write raises exception if file doesn't belong to a collection", {
176
177     dog <- ArvadosFile$new("dog")
178
179     expect_that(dog$write(),
180                 throws_error("ArvadosFile doesn't belong to any collection."))
181 })
182
183 test_that("write delegates writing operation to REST service class", {
184
185
186     collectionContent <- c("animal", "animal/fish")
187     fakeREST <- FakeRESTService$new(collectionContent)
188
189     api <- Arvados$new("myToken", "myHostName")
190     api$setRESTService(fakeREST)
191     collection <- Collection$new(api, "myUUID")
192     fish <- collection$get("animal/fish")
193
194     fileContent <- fish$write("new file content")
195
196     expect_that(fakeREST$writeBuffer, equals("new file content"))
197 })
198
199 test_that(paste("move raises exception if arvados file",
200                 "doesn't belong to any collection"), {
201
202     animal <- ArvadosFile$new("animal")
203
204     expect_that(animal$move("new/location"),
205                 throws_error("ArvadosFile doesn't belong to any collection."))
206 })
207
208 test_that(paste("move raises exception if newLocationInCollection",
209                 "parameter is invalid"), {
210
211     collectionContent <- c("animal",
212                            "animal/fish",
213                            "animal/dog",
214                            "animal/fish/shark",
215                            "ball")
216
217     fakeREST <- FakeRESTService$new(collectionContent)
218
219     api <- Arvados$new("myToken", "myHostName")
220     api$setRESTService(fakeREST)
221
222     collection <- Collection$new(api, "myUUID")
223     dog <- collection$get("animal/dog")
224
225     expect_that(dog$move("objects/dog"),
226                 throws_error("Unable to get destination subcollection."))
227 })
228
229 test_that("move raises exception if new location contains content with the same name", {
230
231
232     collectionContent <- c("animal",
233                            "animal/fish",
234                            "animal/dog",
235                            "animal/fish/shark",
236                            "dog")
237
238     fakeREST <- FakeRESTService$new(collectionContent)
239
240     api <- Arvados$new("myToken", "myHostName")
241     api$setRESTService(fakeREST)
242     collection <- Collection$new(api, "myUUID")
243     dog <- collection$get("animal/dog")
244
245     expect_that(dog$move("dog"),
246                 throws_error("Destination already contains content with same name."))
247
248 })
249
250 test_that("move moves arvados file inside collection tree", {
251
252     collectionContent <- c("animal",
253                            "animal/fish",
254                            "animal/dog",
255                            "animal/fish/shark",
256                            "ball")
257
258     fakeREST <- FakeRESTService$new(collectionContent)
259
260     api <- Arvados$new("myToken", "myHostName")
261     api$setRESTService(fakeREST)
262     collection <- Collection$new(api, "myUUID")
263     dog <- collection$get("animal/dog")
264
265     dog$move("dog")
266     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
267     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
268
269     expect_true(dogIsNullOnOldLocation)
270     expect_true(dogExistsOnNewLocation)
271 })
272
273 test_that(paste("copy raises exception if arvados file",
274                 "doesn't belong to any collection"), {
275
276     animal <- ArvadosFile$new("animal")
277
278     expect_that(animal$copy("new/location"),
279                 throws_error("ArvadosFile doesn't belong to any collection."))
280 })
281
282 test_that(paste("copy raises exception if location parameter is invalid"), {
283
284     collectionContent <- c("animal",
285                            "animal/fish",
286                            "animal/dog",
287                            "animal/fish/shark",
288                            "ball")
289
290     fakeREST <- FakeRESTService$new(collectionContent)
291
292     api <- Arvados$new("myToken", "myHostName")
293     api$setRESTService(fakeREST)
294
295     collection <- Collection$new(api, "myUUID")
296     dog <- collection$get("animal/dog")
297
298     expect_that(dog$copy("objects/dog"),
299                 throws_error("Unable to get destination subcollection."))
300 })
301
302 test_that("copy raises exception if new location contains content with the same name", {
303
304
305     collectionContent <- c("animal",
306                            "animal/fish",
307                            "animal/dog",
308                            "animal/fish/shark",
309                            "dog")
310
311     fakeREST <- FakeRESTService$new(collectionContent)
312
313     api <- Arvados$new("myToken", "myHostName")
314     api$setRESTService(fakeREST)
315     collection <- Collection$new(api, "myUUID")
316     dog <- collection$get("animal/dog")
317
318     expect_that(dog$copy("dog"),
319                 throws_error("Destination already contains content with same name."))
320
321 })
322
323 test_that("copy copies arvados file inside collection tree", {
324
325     collectionContent <- c("animal",
326                            "animal/fish",
327                            "animal/dog",
328                            "animal/fish/shark",
329                            "ball")
330
331     fakeREST <- FakeRESTService$new(collectionContent)
332
333     api <- Arvados$new("myToken", "myHostName")
334     api$setRESTService(fakeREST)
335     collection <- Collection$new(api, "myUUID")
336     dog <- collection$get("animal/dog")
337
338     dog$copy("dog")
339     dogExistsOnOldLocation <- !is.null(collection$get("animal/dog"))
340     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
341
342     expect_true(dogExistsOnOldLocation)
343     expect_true(dogExistsOnNewLocation)
344 })
345
346 test_that("duplicate performs deep cloning of Arvados file", {
347     arvFile <- ArvadosFile$new("foo")
348     newFile1 <- arvFile$duplicate()
349     newFile2 <- arvFile$duplicate("bar")
350
351     expect_that(newFile1$getFileListing(), equals(arvFile$getFileListing()))
352     expect_that(newFile2$getFileListing(), equals(c("bar")))
353 })