Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / sdk / R / tests / testthat / test-ArvadosFile.R
1 source("fakes/FakeRESTService.R")
2
3 context("ArvadosFile")
4
5 test_that("getFileListing always returns file name", {
6
7     dog <- ArvadosFile$new("dog")
8
9     expect_that(dog$getFileListing(), equals("dog"))
10 }) 
11
12 test_that("get always returns NULL", {
13
14     dog <- ArvadosFile$new("dog")
15     
16     responseIsNull <- is.null(dog$get("something"))
17     expect_that(responseIsNull, is_true())
18 }) 
19
20 test_that("getFirst always returns NULL", {
21
22     dog <- ArvadosFile$new("dog")
23     
24     responseIsNull <- is.null(dog$getFirst())
25     expect_that(responseIsNull, is_true())
26 }) 
27
28 test_that(paste("getSizeInBytes returns zero if arvadosFile",
29                 "is not part of a collection"), {
30
31     dog <- ArvadosFile$new("dog")
32
33     expect_that(dog$getSizeInBytes(), equals(0))
34 }) 
35
36 test_that(paste("getSizeInBytes delegates size calculation",
37                 "to REST service class"), {
38
39     api <- Arvados$new("myToken", "myHostName")
40     api$setHttpClient(FakeHttpRequest$new())
41     api$setHttpParser(FakeHttpParser$new())
42
43     collectionContent <- c("animal", "animal/fish")
44     returnSize <- 100
45
46     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
47     api$setRESTService(fakeREST)
48     collection <- Collection$new(api, "myUUID")
49     fish <- collection$get("animal/fish")
50
51     resourceSize <- fish$getSizeInBytes()
52
53     expect_that(resourceSize, equals(100))
54 }) 
55
56 test_that("getRelativePath returns path relative to the tree root", {
57
58     animal <- Subcollection$new("animal")
59     fish <- Subcollection$new("fish")
60     shark <- ArvadosFile$new("shark")
61
62     animal$add(fish)
63     fish$add(shark)
64
65     expect_that(shark$getRelativePath(), equals("animal/fish/shark"))
66 }) 
67
68 test_that("read raises exception if file doesn't belong to a collection", {
69
70     dog <- ArvadosFile$new("dog")
71
72     expect_that(dog$read(),
73                 throws_error("ArvadosFile doesn't belong to any collection."))
74 }) 
75
76 test_that("read raises exception offset or length is negative number", {
77
78     api <- Arvados$new("myToken", "myHostName")
79     api$setHttpClient(FakeHttpRequest$new())
80     api$setHttpParser(FakeHttpParser$new())
81
82     collectionContent <- c("animal", "animal/fish")
83
84     fakeREST <- FakeRESTService$new(collectionContent)
85     api$setRESTService(fakeREST)
86     collection <- Collection$new(api, "myUUID")
87     fish <- collection$get("animal/fish")
88
89     expect_that(fish$read(contentType = "text", offset = -1),
90                 throws_error("Offset and length must be positive values."))
91     expect_that(fish$read(contentType = "text", length = -1),
92                 throws_error("Offset and length must be positive values."))
93     expect_that(fish$read(contentType = "text", offset = -1, length = -1),
94                 throws_error("Offset and length must be positive values."))
95 }) 
96
97 test_that("read delegates reading operation to REST service class", {
98
99     api <- Arvados$new("myToken", "myHostName")
100     api$setHttpClient(FakeHttpRequest$new())
101     api$setHttpParser(FakeHttpParser$new())
102
103     collectionContent <- c("animal", "animal/fish")
104     readContent <- "my file"
105
106     fakeREST <- FakeRESTService$new(collectionContent, readContent)
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("connect returns textConnection opened",
118                 "in read mode when 'r' is passed as argument"), {
119
120     api <- Arvados$new("myToken", "myHostName")
121     api$setHttpClient(FakeHttpRequest$new())
122     api$setHttpParser(FakeHttpParser$new())
123
124     collectionContent <- c("animal", "animal/fish")
125     readContent <- "file content"
126
127     fakeREST <- FakeRESTService$new(collectionContent, readContent)
128     api$setRESTService(fakeREST)
129     collection <- Collection$new(api, "myUUID")
130     fish <- collection$get("animal/fish")
131
132     connection <- fish$connection("r")
133
134     expect_that(readLines(connection), equals("file content"))
135 }) 
136
137 test_that(paste("connect returns textConnection opened",
138                 "in write mode when 'w' is passed as argument"), {
139
140     api <- Arvados$new("myToken", "myHostName")
141     api$setHttpClient(FakeHttpRequest$new())
142     api$setHttpParser(FakeHttpParser$new())
143
144     collectionContent <- c("animal", "animal/fish")
145
146     fakeREST <- FakeRESTService$new(collectionContent)
147     api$setRESTService(fakeREST)
148     collection <- Collection$new(api, "myUUID")
149     fish <- collection$get("animal/fish")
150
151     connection <- fish$connection("w")
152
153     writeLines("file", connection)
154     writeLines("content", connection)
155
156     writeResult <- textConnectionValue(connection)
157
158     expect_that(writeResult[1], equals("file"))
159     expect_that(writeResult[2], equals("content"))
160 }) 
161
162 test_that("flush sends data stored in a connection to a REST server", {
163
164     api <- Arvados$new("myToken", "myHostName")
165     api$setHttpClient(FakeHttpRequest$new())
166     api$setHttpParser(FakeHttpParser$new())
167
168     collectionContent <- c("animal", "animal/fish")
169
170     fakeREST <- FakeRESTService$new(collectionContent)
171     api$setRESTService(fakeREST)
172     collection <- Collection$new(api, "myUUID")
173     fish <- collection$get("animal/fish")
174
175     connection <- fish$connection("w")
176
177     writeLines("file content", connection)
178
179     fish$flush()
180
181     expect_that(fakeREST$writeBuffer, equals("file content"))
182 }) 
183
184 test_that("write raises exception if file doesn't belong to a collection", {
185
186     dog <- ArvadosFile$new("dog")
187
188     expect_that(dog$write(),
189                 throws_error("ArvadosFile doesn't belong to any collection."))
190 }) 
191
192 test_that("write delegates writing operation to REST service class", {
193
194     api <- Arvados$new("myToken", "myHostName")
195     api$setHttpClient(FakeHttpRequest$new())
196     api$setHttpParser(FakeHttpParser$new())
197
198     collectionContent <- c("animal", "animal/fish")
199
200     fakeREST <- FakeRESTService$new(collectionContent)
201     api$setRESTService(fakeREST)
202     collection <- Collection$new(api, "myUUID")
203     fish <- collection$get("animal/fish")
204     
205     fileContent <- fish$write("new file content")
206
207     expect_that(fakeREST$writeBuffer, equals("new file content"))
208 }) 
209
210 test_that(paste("move raises exception if arvados file",
211                 "doesn't belong to any collection"), {
212
213     animal <- ArvadosFile$new("animal")
214
215     expect_that(animal$move("new/location"),
216                 throws_error("ArvadosFile doesn't belong to any collection"))
217 }) 
218
219 test_that(paste("move raises exception if newLocationInCollection",
220                 "parameter is invalid"), {
221
222     api <- Arvados$new("myToken", "myHostName")
223     api$setHttpClient(FakeHttpRequest$new())
224     api$setHttpParser(FakeHttpParser$new())
225
226     collectionContent <- c("animal",
227                            "animal/fish",
228                            "animal/dog",
229                            "animal/fish/shark",
230                            "ball")
231
232     fakeREST <- FakeRESTService$new(collectionContent)
233     api$setRESTService(fakeREST)
234
235     collection <- Collection$new(api, "myUUID")
236     dog <- collection$get("animal/dog")
237
238     expect_that(dog$move("objects/dog"),
239                 throws_error("Unable to get destination subcollection"))
240 }) 
241
242 test_that("move raises exception if new location contains content with the same name", {
243
244     api <- Arvados$new("myToken", "myHostName")
245     api$setHttpClient(FakeHttpRequest$new())
246     api$setHttpParser(FakeHttpParser$new())
247
248     collectionContent <- c("animal",
249                            "animal/fish",
250                            "animal/dog",
251                            "animal/fish/shark",
252                            "dog")
253
254     fakeREST <- FakeRESTService$new(collectionContent)
255     api$setRESTService(fakeREST)
256     collection <- Collection$new(api, "myUUID")
257     dog <- collection$get("animal/dog")
258
259     expect_that(dog$move("dog"),
260                 throws_error("Destination already contains content with same name."))
261
262 }) 
263
264 test_that("move moves arvados file inside collection tree", {
265
266     api <- Arvados$new("myToken", "myHostName")
267     api$setHttpClient(FakeHttpRequest$new())
268     api$setHttpParser(FakeHttpParser$new())
269
270     collectionContent <- c("animal",
271                            "animal/fish",
272                            "animal/dog",
273                            "animal/fish/shark",
274                            "ball")
275
276     fakeREST <- FakeRESTService$new(collectionContent)
277     api$setRESTService(fakeREST)
278     collection <- Collection$new(api, "myUUID")
279     dog <- collection$get("animal/dog")
280
281     dog$move("dog")
282     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
283     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
284
285     expect_that(dogIsNullOnOldLocation, is_true())
286     expect_that(dogExistsOnNewLocation, is_true())
287 })