remove trailing white space from all files
[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_that(responseIsNull, is_true())
27 })
28
29 test_that("getFirst always returns NULL", {
30
31     dog <- ArvadosFile$new("dog")
32
33     responseIsNull <- is.null(dog$getFirst())
34     expect_that(responseIsNull, is_true())
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
85     collectionContent <- c("animal", "animal/fish")
86     fakeREST <- FakeRESTService$new(collectionContent)
87
88     api <- Arvados$new("myToken", "myHostName")
89     api$setRESTService(fakeREST)
90     collection <- Collection$new(api, "myUUID")
91     fish <- collection$get("animal/fish")
92
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."))
99 })
100
101 test_that("read delegates reading operation to REST service class", {
102
103     collectionContent <- c("animal", "animal/fish")
104     readContent <- "my file"
105     fakeREST <- FakeRESTService$new(collectionContent, readContent)
106
107     api <- Arvados$new("myToken", "myHostName")
108     api$setRESTService(fakeREST)
109     collection <- Collection$new(api, "myUUID")
110     fish <- collection$get("animal/fish")
111
112     fileContent <- fish$read("text")
113
114     expect_that(fileContent, equals("my file"))
115     expect_that(fakeREST$readCallCount, equals(1))
116 })
117
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"), {
121
122     collectionContent <- c("animal", "animal/fish")
123     fakeREST <- FakeRESTService$new(collectionContent)
124
125     api <- Arvados$new("myToken", "myHostName")
126     api$setRESTService(fakeREST)
127     collection <- Collection$new(api, "myUUID")
128     fish <- collection$get("animal/fish")
129
130     connection <- fish$connection("r")
131
132     expect_that(fakeREST$getConnectionCallCount, equals(1))
133 })
134
135 test_that(paste("connection returns textConnection opened",
136                 "in write mode when 'w' is passed as argument"), {
137
138     collectionContent <- c("animal", "animal/fish")
139     fakeREST <- FakeRESTService$new(collectionContent)
140
141     api <- Arvados$new("myToken", "myHostName")
142     api$setRESTService(fakeREST)
143     collection <- Collection$new(api, "myUUID")
144     fish <- collection$get("animal/fish")
145
146     connection <- fish$connection("w")
147
148     writeLines("file", connection)
149     writeLines("content", connection)
150
151     writeResult <- textConnectionValue(connection)
152
153     expect_that(writeResult[1], equals("file"))
154     expect_that(writeResult[2], equals("content"))
155 })
156
157 test_that("flush sends data stored in a connection to a REST server", {
158
159
160     collectionContent <- c("animal", "animal/fish")
161     fakeREST <- FakeRESTService$new(collectionContent)
162
163     api <- Arvados$new("myToken", "myHostName")
164     api$setRESTService(fakeREST)
165     collection <- Collection$new(api, "myUUID")
166     fish <- collection$get("animal/fish")
167
168     connection <- fish$connection("w")
169
170     writeLines("file content", connection)
171
172     fish$flush()
173
174     expect_that(fakeREST$writeBuffer, equals("file content"))
175 })
176
177 test_that("write raises exception if file doesn't belong to a collection", {
178
179     dog <- ArvadosFile$new("dog")
180
181     expect_that(dog$write(),
182                 throws_error("ArvadosFile doesn't belong to any collection."))
183 })
184
185 test_that("write delegates writing operation to REST service class", {
186
187
188     collectionContent <- c("animal", "animal/fish")
189     fakeREST <- FakeRESTService$new(collectionContent)
190
191     api <- Arvados$new("myToken", "myHostName")
192     api$setRESTService(fakeREST)
193     collection <- Collection$new(api, "myUUID")
194     fish <- collection$get("animal/fish")
195
196     fileContent <- fish$write("new file content")
197
198     expect_that(fakeREST$writeBuffer, equals("new file content"))
199 })
200
201 test_that(paste("move raises exception if arvados file",
202                 "doesn't belong to any collection"), {
203
204     animal <- ArvadosFile$new("animal")
205
206     expect_that(animal$move("new/location"),
207                 throws_error("ArvadosFile doesn't belong to any collection"))
208 })
209
210 test_that(paste("move raises exception if newLocationInCollection",
211                 "parameter is invalid"), {
212
213
214     collectionContent <- c("animal",
215                            "animal/fish",
216                            "animal/dog",
217                            "animal/fish/shark",
218                            "ball")
219
220     fakeREST <- FakeRESTService$new(collectionContent)
221
222     api <- Arvados$new("myToken", "myHostName")
223     api$setRESTService(fakeREST)
224
225     collection <- Collection$new(api, "myUUID")
226     dog <- collection$get("animal/dog")
227
228     expect_that(dog$move("objects/dog"),
229                 throws_error("Unable to get destination subcollection"))
230 })
231
232 test_that("move raises exception if new location contains content with the same name", {
233
234
235     collectionContent <- c("animal",
236                            "animal/fish",
237                            "animal/dog",
238                            "animal/fish/shark",
239                            "dog")
240
241     fakeREST <- FakeRESTService$new(collectionContent)
242
243     api <- Arvados$new("myToken", "myHostName")
244     api$setRESTService(fakeREST)
245     collection <- Collection$new(api, "myUUID")
246     dog <- collection$get("animal/dog")
247
248     expect_that(dog$move("dog"),
249                 throws_error("Destination already contains content with same name."))
250
251 })
252
253 test_that("move moves arvados file inside collection tree", {
254
255
256     collectionContent <- c("animal",
257                            "animal/fish",
258                            "animal/dog",
259                            "animal/fish/shark",
260                            "ball")
261
262     fakeREST <- FakeRESTService$new(collectionContent)
263
264     api <- Arvados$new("myToken", "myHostName")
265     api$setRESTService(fakeREST)
266     collection <- Collection$new(api, "myUUID")
267     dog <- collection$get("animal/dog")
268
269     dog$move("dog")
270     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
271     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
272
273     expect_that(dogIsNullOnOldLocation, is_true())
274     expect_that(dogExistsOnNewLocation, is_true())
275 })