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     collectionContent <- c("animal", "animal/fish")
40     returnSize <- 100
41     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
42
43     api <- Arvados$new("myToken", "myHostName")
44     api$setRESTService(fakeREST)
45     collection <- Collection$new(api, "myUUID")
46     fish <- collection$get("animal/fish")
47
48     resourceSize <- fish$getSizeInBytes()
49
50     expect_that(resourceSize, equals(100))
51 }) 
52
53 test_that("getRelativePath returns path relative to the tree root", {
54
55     animal <- Subcollection$new("animal")
56     fish <- Subcollection$new("fish")
57     shark <- ArvadosFile$new("shark")
58
59     animal$add(fish)
60     fish$add(shark)
61
62     expect_that(shark$getRelativePath(), equals("animal/fish/shark"))
63 }) 
64
65 test_that("read raises exception if file doesn't belong to a collection", {
66
67     dog <- ArvadosFile$new("dog")
68
69     expect_that(dog$read(),
70                 throws_error("ArvadosFile doesn't belong to any collection."))
71 }) 
72
73 test_that("read raises exception offset or length is negative number", {
74
75
76     collectionContent <- c("animal", "animal/fish")
77     fakeREST <- FakeRESTService$new(collectionContent)
78
79     api <- Arvados$new("myToken", "myHostName")
80     api$setRESTService(fakeREST)
81     collection <- Collection$new(api, "myUUID")
82     fish <- collection$get("animal/fish")
83
84     expect_that(fish$read(contentType = "text", offset = -1),
85                 throws_error("Offset and length must be positive values."))
86     expect_that(fish$read(contentType = "text", length = -1),
87                 throws_error("Offset and length must be positive values."))
88     expect_that(fish$read(contentType = "text", offset = -1, length = -1),
89                 throws_error("Offset and length must be positive values."))
90 }) 
91
92 test_that("read delegates reading operation to REST service class", {
93
94     collectionContent <- c("animal", "animal/fish")
95     readContent <- "my file"
96     fakeREST <- FakeRESTService$new(collectionContent, readContent)
97
98     api <- Arvados$new("myToken", "myHostName")
99     api$setRESTService(fakeREST)
100     collection <- Collection$new(api, "myUUID")
101     fish <- collection$get("animal/fish")
102     
103     fileContent <- fish$read("text")
104
105     expect_that(fileContent, equals("my file"))
106     expect_that(fakeREST$readCallCount, equals(1))
107 }) 
108
109 test_that(paste("connection delegates connection creation ro RESTService class",
110                 "which returns curl connection opened in read mode when", 
111                 "'r' of 'rb' is passed as argument"), {
112
113     collectionContent <- c("animal", "animal/fish")
114     fakeREST <- FakeRESTService$new(collectionContent)
115
116     api <- Arvados$new("myToken", "myHostName")
117     api$setRESTService(fakeREST)
118     collection <- Collection$new(api, "myUUID")
119     fish <- collection$get("animal/fish")
120
121     connection <- fish$connection("r")
122
123     expect_that(fakeREST$getConnectionCallCount, equals(1))
124 }) 
125
126 test_that(paste("connection returns textConnection opened",
127                 "in write mode when 'w' is passed as argument"), {
128
129     collectionContent <- c("animal", "animal/fish")
130     fakeREST <- FakeRESTService$new(collectionContent)
131
132     api <- Arvados$new("myToken", "myHostName")
133     api$setRESTService(fakeREST)
134     collection <- Collection$new(api, "myUUID")
135     fish <- collection$get("animal/fish")
136
137     connection <- fish$connection("w")
138
139     writeLines("file", connection)
140     writeLines("content", connection)
141
142     writeResult <- textConnectionValue(connection)
143
144     expect_that(writeResult[1], equals("file"))
145     expect_that(writeResult[2], equals("content"))
146 }) 
147
148 test_that("flush sends data stored in a connection to a REST server", {
149
150
151     collectionContent <- c("animal", "animal/fish")
152     fakeREST <- FakeRESTService$new(collectionContent)
153
154     api <- Arvados$new("myToken", "myHostName")
155     api$setRESTService(fakeREST)
156     collection <- Collection$new(api, "myUUID")
157     fish <- collection$get("animal/fish")
158
159     connection <- fish$connection("w")
160
161     writeLines("file content", connection)
162
163     fish$flush()
164
165     expect_that(fakeREST$writeBuffer, equals("file content"))
166 }) 
167
168 test_that("write raises exception if file doesn't belong to a collection", {
169
170     dog <- ArvadosFile$new("dog")
171
172     expect_that(dog$write(),
173                 throws_error("ArvadosFile doesn't belong to any collection."))
174 }) 
175
176 test_that("write delegates writing operation to REST service class", {
177
178
179     collectionContent <- c("animal", "animal/fish")
180     fakeREST <- FakeRESTService$new(collectionContent)
181
182     api <- Arvados$new("myToken", "myHostName")
183     api$setRESTService(fakeREST)
184     collection <- Collection$new(api, "myUUID")
185     fish <- collection$get("animal/fish")
186     
187     fileContent <- fish$write("new file content")
188
189     expect_that(fakeREST$writeBuffer, equals("new file content"))
190 }) 
191
192 test_that(paste("move raises exception if arvados file",
193                 "doesn't belong to any collection"), {
194
195     animal <- ArvadosFile$new("animal")
196
197     expect_that(animal$move("new/location"),
198                 throws_error("ArvadosFile doesn't belong to any collection"))
199 }) 
200
201 test_that(paste("move raises exception if newLocationInCollection",
202                 "parameter is invalid"), {
203
204
205     collectionContent <- c("animal",
206                            "animal/fish",
207                            "animal/dog",
208                            "animal/fish/shark",
209                            "ball")
210     fakeREST <- FakeRESTService$new(collectionContent)
211
212     api <- Arvados$new("myToken", "myHostName")
213     api$setRESTService(fakeREST)
214
215     collection <- Collection$new(api, "myUUID")
216     dog <- collection$get("animal/dog")
217
218     expect_that(dog$move("objects/dog"),
219                 throws_error("Unable to get destination subcollection"))
220 }) 
221
222 test_that("move raises exception if new location contains content with the same name", {
223
224
225     collectionContent <- c("animal",
226                            "animal/fish",
227                            "animal/dog",
228                            "animal/fish/shark",
229                            "dog")
230     fakeREST <- FakeRESTService$new(collectionContent)
231
232     api <- Arvados$new("myToken", "myHostName")
233     api$setRESTService(fakeREST)
234     collection <- Collection$new(api, "myUUID")
235     dog <- collection$get("animal/dog")
236
237     expect_that(dog$move("dog"),
238                 throws_error("Destination already contains content with same name."))
239
240 }) 
241
242 test_that("move moves arvados file inside collection tree", {
243
244
245     collectionContent <- c("animal",
246                            "animal/fish",
247                            "animal/dog",
248                            "animal/fish/shark",
249                            "ball")
250     fakeREST <- FakeRESTService$new(collectionContent)
251
252     api <- Arvados$new("myToken", "myHostName")
253     api$setRESTService(fakeREST)
254     collection <- Collection$new(api, "myUUID")
255     dog <- collection$get("animal/dog")
256
257     dog$move("dog")
258     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
259     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
260
261     expect_that(dogIsNullOnOldLocation, is_true())
262     expect_that(dogExistsOnNewLocation, is_true())
263 })