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_that(responseIsNull, is_true())
29 test_that("getFirst always returns NULL", {
31 dog <- ArvadosFile$new("dog")
33 responseIsNull <- is.null(dog$getFirst())
34 expect_that(responseIsNull, is_true())
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", {
85 collectionContent <- c("animal", "animal/fish")
86 fakeREST <- FakeRESTService$new(collectionContent)
88 api <- Arvados$new("myToken", "myHostName")
89 api$setRESTService(fakeREST)
90 collection <- Collection$new(api, "myUUID")
91 fish <- collection$get("animal/fish")
93 expect_that(fish$read(contentType = "text", offset = -1),
94 throws_error("Offset and length must be positive values."))
95 expect_that(fish$read(contentType = "text", length = -1),
96 throws_error("Offset and length must be positive values."))
97 expect_that(fish$read(contentType = "text", offset = -1, length = -1),
98 throws_error("Offset and length must be positive values."))
101 test_that("read delegates reading operation to REST service class", {
103 collectionContent <- c("animal", "animal/fish")
104 readContent <- "my file"
105 fakeREST <- FakeRESTService$new(collectionContent, readContent)
107 api <- Arvados$new("myToken", "myHostName")
108 api$setRESTService(fakeREST)
109 collection <- Collection$new(api, "myUUID")
110 fish <- collection$get("animal/fish")
112 fileContent <- fish$read("text")
114 expect_that(fileContent, equals("my file"))
115 expect_that(fakeREST$readCallCount, equals(1))
118 test_that(paste("connection delegates connection creation ro RESTService class",
119 "which returns curl connection opened in read mode when",
120 "'r' of 'rb' is passed as argument"), {
122 collectionContent <- c("animal", "animal/fish")
123 fakeREST <- FakeRESTService$new(collectionContent)
125 api <- Arvados$new("myToken", "myHostName")
126 api$setRESTService(fakeREST)
127 collection <- Collection$new(api, "myUUID")
128 fish <- collection$get("animal/fish")
130 connection <- fish$connection("r")
132 expect_that(fakeREST$getConnectionCallCount, equals(1))
135 test_that(paste("connection returns textConnection opened",
136 "in write mode when 'w' is passed as argument"), {
138 collectionContent <- c("animal", "animal/fish")
139 fakeREST <- FakeRESTService$new(collectionContent)
141 api <- Arvados$new("myToken", "myHostName")
142 api$setRESTService(fakeREST)
143 collection <- Collection$new(api, "myUUID")
144 fish <- collection$get("animal/fish")
146 connection <- fish$connection("w")
148 writeLines("file", connection)
149 writeLines("content", connection)
151 writeResult <- textConnectionValue(connection)
153 expect_that(writeResult[1], equals("file"))
154 expect_that(writeResult[2], equals("content"))
157 test_that("flush sends data stored in a connection to a REST server", {
160 collectionContent <- c("animal", "animal/fish")
161 fakeREST <- FakeRESTService$new(collectionContent)
163 api <- Arvados$new("myToken", "myHostName")
164 api$setRESTService(fakeREST)
165 collection <- Collection$new(api, "myUUID")
166 fish <- collection$get("animal/fish")
168 connection <- fish$connection("w")
170 writeLines("file content", connection)
174 expect_that(fakeREST$writeBuffer, equals("file content"))
177 test_that("write raises exception if file doesn't belong to a collection", {
179 dog <- ArvadosFile$new("dog")
181 expect_that(dog$write(),
182 throws_error("ArvadosFile doesn't belong to any collection."))
185 test_that("write delegates writing operation to REST service class", {
188 collectionContent <- c("animal", "animal/fish")
189 fakeREST <- FakeRESTService$new(collectionContent)
191 api <- Arvados$new("myToken", "myHostName")
192 api$setRESTService(fakeREST)
193 collection <- Collection$new(api, "myUUID")
194 fish <- collection$get("animal/fish")
196 fileContent <- fish$write("new file content")
198 expect_that(fakeREST$writeBuffer, equals("new file content"))
201 test_that(paste("move raises exception if arvados file",
202 "doesn't belong to any collection"), {
204 animal <- ArvadosFile$new("animal")
206 expect_that(animal$move("new/location"),
207 throws_error("ArvadosFile doesn't belong to any collection"))
210 test_that(paste("move raises exception if newLocationInCollection",
211 "parameter is invalid"), {
214 collectionContent <- c("animal",
220 fakeREST <- FakeRESTService$new(collectionContent)
222 api <- Arvados$new("myToken", "myHostName")
223 api$setRESTService(fakeREST)
225 collection <- Collection$new(api, "myUUID")
226 dog <- collection$get("animal/dog")
228 expect_that(dog$move("objects/dog"),
229 throws_error("Unable to get destination subcollection"))
232 test_that("move raises exception if new location contains content with the same name", {
235 collectionContent <- c("animal",
241 fakeREST <- FakeRESTService$new(collectionContent)
243 api <- Arvados$new("myToken", "myHostName")
244 api$setRESTService(fakeREST)
245 collection <- Collection$new(api, "myUUID")
246 dog <- collection$get("animal/dog")
248 expect_that(dog$move("dog"),
249 throws_error("Destination already contains content with same name."))
253 test_that("move moves arvados file inside collection tree", {
256 collectionContent <- c("animal",
262 fakeREST <- FakeRESTService$new(collectionContent)
264 api <- Arvados$new("myToken", "myHostName")
265 api$setRESTService(fakeREST)
266 collection <- Collection$new(api, "myUUID")
267 dog <- collection$get("animal/dog")
270 dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
271 dogExistsOnNewLocation <- !is.null(collection$get("dog"))
273 expect_that(dogIsNullOnOldLocation, is_true())
274 expect_that(dogExistsOnNewLocation, is_true())