1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 source("fakes/FakeRESTService.R")
9 test_that("constructor raises error if file name is empty string", {
11 expect_that(ArvadosFile$new(""), throws_error("Invalid name."))
14 test_that("getFileListing always returns file name", {
16 dog <- ArvadosFile$new("dog")
18 expect_that(dog$getFileListing(), equals("dog"))
21 test_that("get always returns NULL", {
23 dog <- ArvadosFile$new("dog")
25 responseIsNull <- is.null(dog$get("something"))
26 expect_true(responseIsNull)
29 test_that("getFirst always returns NULL", {
31 dog <- ArvadosFile$new("dog")
33 responseIsNull <- is.null(dog$getFirst())
34 expect_true(responseIsNull)
37 test_that(paste("getSizeInBytes returns zero if arvadosFile",
38 "is not part of a collection"), {
40 dog <- ArvadosFile$new("dog")
42 expect_that(dog$getSizeInBytes(), equals(0))
45 test_that(paste("getSizeInBytes delegates size calculation",
46 "to REST service class"), {
48 collectionContent <- c("animal", "animal/fish")
50 fakeREST <- FakeRESTService$new(collectionContent, returnSize)
52 api <- Arvados$new("myToken", "myHostName")
53 api$setRESTService(fakeREST)
54 collection <- Collection$new(api, "myUUID")
55 fish <- collection$get("animal/fish")
57 resourceSize <- fish$getSizeInBytes()
59 expect_that(resourceSize, equals(100))
62 test_that("getRelativePath returns path relative to the tree root", {
64 animal <- Subcollection$new("animal")
65 fish <- Subcollection$new("fish")
66 shark <- ArvadosFile$new("shark")
71 expect_that(shark$getRelativePath(), equals("animal/fish/shark"))
74 test_that("read raises exception if file doesn't belong to a collection", {
76 dog <- ArvadosFile$new("dog")
78 expect_that(dog$read(),
79 throws_error("ArvadosFile doesn't belong to any collection."))
82 test_that("read raises exception offset or length is negative number", {
84 collectionContent <- c("animal", "animal/fish")
85 fakeREST <- FakeRESTService$new(collectionContent)
87 api <- Arvados$new("myToken", "myHostName")
88 api$setRESTService(fakeREST)
89 collection <- Collection$new(api, "myUUID")
90 fish <- collection$get("animal/fish")
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."))
100 test_that("read delegates reading operation to REST service class", {
102 collectionContent <- c("animal", "animal/fish")
103 readContent <- "my file"
104 fakeREST <- FakeRESTService$new(collectionContent, readContent)
106 api <- Arvados$new("myToken", "myHostName")
107 api$setRESTService(fakeREST)
108 collection <- Collection$new(api, "myUUID")
109 fish <- collection$get("animal/fish")
111 fileContent <- fish$read("text")
113 expect_that(fileContent, equals("my file"))
114 expect_that(fakeREST$readCallCount, equals(1))
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"), {
121 collectionContent <- c("animal", "animal/fish")
122 fakeREST <- FakeRESTService$new(collectionContent)
124 api <- Arvados$new("myToken", "myHostName")
125 api$setRESTService(fakeREST)
126 collection <- Collection$new(api, "myUUID")
127 fish <- collection$get("animal/fish")
129 connection <- fish$connection("r")
131 expect_that(fakeREST$getConnectionCallCount, equals(1))
134 test_that(paste("connection returns textConnection opened",
135 "in write mode when 'w' is passed as argument"), {
137 collectionContent <- c("animal", "animal/fish")
138 fakeREST <- FakeRESTService$new(collectionContent)
140 api <- Arvados$new("myToken", "myHostName")
141 api$setRESTService(fakeREST)
142 collection <- Collection$new(api, "myUUID")
143 fish <- collection$get("animal/fish")
145 connection <- fish$connection("w")
147 writeLines("file", connection)
148 writeLines("content", connection)
150 writeResult <- textConnectionValue(connection)
152 expect_that(writeResult[1], equals("file"))
153 expect_that(writeResult[2], equals("content"))
156 test_that("flush sends data stored in a connection to a REST server", {
158 collectionContent <- c("animal", "animal/fish")
159 fakeREST <- FakeRESTService$new(collectionContent)
161 api <- Arvados$new("myToken", "myHostName")
162 api$setRESTService(fakeREST)
163 collection <- Collection$new(api, "myUUID")
164 fish <- collection$get("animal/fish")
166 connection <- fish$connection("w")
168 writeLines("file content", connection)
172 expect_that(fakeREST$writeBuffer, equals("file content"))
175 test_that("write raises exception if file doesn't belong to a collection", {
177 dog <- ArvadosFile$new("dog")
179 expect_that(dog$write(),
180 throws_error("ArvadosFile doesn't belong to any collection."))
183 test_that("write delegates writing operation to REST service class", {
186 collectionContent <- c("animal", "animal/fish")
187 fakeREST <- FakeRESTService$new(collectionContent)
189 api <- Arvados$new("myToken", "myHostName")
190 api$setRESTService(fakeREST)
191 collection <- Collection$new(api, "myUUID")
192 fish <- collection$get("animal/fish")
194 fileContent <- fish$write("new file content")
196 expect_that(fakeREST$writeBuffer, equals("new file content"))
199 test_that(paste("move raises exception if arvados file",
200 "doesn't belong to any collection"), {
202 animal <- ArvadosFile$new("animal")
204 expect_that(animal$move("new/location"),
205 throws_error("ArvadosFile doesn't belong to any collection."))
208 test_that(paste("move raises exception if newLocationInCollection",
209 "parameter is invalid"), {
211 collectionContent <- c("animal",
217 fakeREST <- FakeRESTService$new(collectionContent)
219 api <- Arvados$new("myToken", "myHostName")
220 api$setRESTService(fakeREST)
222 collection <- Collection$new(api, "myUUID")
223 dog <- collection$get("animal/dog")
225 expect_that(dog$move("objects/dog"),
226 throws_error("Unable to get destination subcollection."))
229 test_that("move raises exception if new location contains content with the same name", {
232 collectionContent <- c("animal",
238 fakeREST <- FakeRESTService$new(collectionContent)
240 api <- Arvados$new("myToken", "myHostName")
241 api$setRESTService(fakeREST)
242 collection <- Collection$new(api, "myUUID")
243 dog <- collection$get("animal/dog")
245 expect_that(dog$move("dog"),
246 throws_error("Destination already contains content with same name."))
250 test_that("move moves arvados file inside collection tree", {
252 collectionContent <- c("animal",
258 fakeREST <- FakeRESTService$new(collectionContent)
260 api <- Arvados$new("myToken", "myHostName")
261 api$setRESTService(fakeREST)
262 collection <- Collection$new(api, "myUUID")
263 dog <- collection$get("animal/dog")
266 dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
267 dogExistsOnNewLocation <- !is.null(collection$get("dog"))
269 expect_true(dogIsNullOnOldLocation)
270 expect_true(dogExistsOnNewLocation)
273 test_that(paste("copy raises exception if arvados file",
274 "doesn't belong to any collection"), {
276 animal <- ArvadosFile$new("animal")
278 expect_that(animal$copy("new/location"),
279 throws_error("ArvadosFile doesn't belong to any collection."))
282 test_that(paste("copy raises exception if location parameter is invalid"), {
284 collectionContent <- c("animal",
290 fakeREST <- FakeRESTService$new(collectionContent)
292 api <- Arvados$new("myToken", "myHostName")
293 api$setRESTService(fakeREST)
295 collection <- Collection$new(api, "myUUID")
296 dog <- collection$get("animal/dog")
298 expect_that(dog$copy("objects/dog"),
299 throws_error("Unable to get destination subcollection."))
302 test_that("copy raises exception if new location contains content with the same name", {
305 collectionContent <- c("animal",
311 fakeREST <- FakeRESTService$new(collectionContent)
313 api <- Arvados$new("myToken", "myHostName")
314 api$setRESTService(fakeREST)
315 collection <- Collection$new(api, "myUUID")
316 dog <- collection$get("animal/dog")
318 expect_that(dog$copy("dog"),
319 throws_error("Destination already contains content with same name."))
323 test_that("copy copies arvados file inside collection tree", {
325 collectionContent <- c("animal",
331 fakeREST <- FakeRESTService$new(collectionContent)
333 api <- Arvados$new("myToken", "myHostName")
334 api$setRESTService(fakeREST)
335 collection <- Collection$new(api, "myUUID")
336 dog <- collection$get("animal/dog")
339 dogExistsOnOldLocation <- !is.null(collection$get("animal/dog"))
340 dogExistsOnNewLocation <- !is.null(collection$get("dog"))
342 expect_true(dogExistsOnOldLocation)
343 expect_true(dogExistsOnNewLocation)
346 test_that("duplicate performs deep cloning of Arvados file", {
347 arvFile <- ArvadosFile$new("foo")
348 newFile1 <- arvFile$duplicate()
349 newFile2 <- arvFile$duplicate("bar")
351 expect_that(newFile1$getFileListing(), equals(arvFile$getFileListing()))
352 expect_that(newFile2$getFileListing(), equals(c("bar")))