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