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