Fixed ArvadosFile/Subcollection move bug and improved http query
[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("connect returns textConnection opened",
110                 "in read mode when 'r' is passed as argument"), {
111
112     collectionContent <- c("animal", "animal/fish")
113     readContent <- "file content"
114     fakeREST <- FakeRESTService$new(collectionContent, readContent)
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(readLines(connection), equals("file content"))
124 }) 
125
126 test_that(paste("connect returns textConnection opened",
127                 "in write mode when 'w' is passed as argument"), {
128
129
130     collectionContent <- c("animal", "animal/fish")
131     fakeREST <- FakeRESTService$new(collectionContent)
132
133     api <- Arvados$new("myToken", "myHostName")
134     api$setRESTService(fakeREST)
135     collection <- Collection$new(api, "myUUID")
136     fish <- collection$get("animal/fish")
137
138     connection <- fish$connection("w")
139
140     writeLines("file", connection)
141     writeLines("content", connection)
142
143     writeResult <- textConnectionValue(connection)
144
145     expect_that(writeResult[1], equals("file"))
146     expect_that(writeResult[2], equals("content"))
147 }) 
148
149 test_that("flush sends data stored in a connection to a REST server", {
150
151
152     collectionContent <- c("animal", "animal/fish")
153     fakeREST <- FakeRESTService$new(collectionContent)
154
155     api <- Arvados$new("myToken", "myHostName")
156     api$setRESTService(fakeREST)
157     collection <- Collection$new(api, "myUUID")
158     fish <- collection$get("animal/fish")
159
160     connection <- fish$connection("w")
161
162     writeLines("file content", connection)
163
164     fish$flush()
165
166     expect_that(fakeREST$writeBuffer, equals("file content"))
167 }) 
168
169 test_that("write raises exception if file doesn't belong to a collection", {
170
171     dog <- ArvadosFile$new("dog")
172
173     expect_that(dog$write(),
174                 throws_error("ArvadosFile doesn't belong to any collection."))
175 }) 
176
177 test_that("write delegates writing operation to REST service class", {
178
179
180     collectionContent <- c("animal", "animal/fish")
181     fakeREST <- FakeRESTService$new(collectionContent)
182
183     api <- Arvados$new("myToken", "myHostName")
184     api$setRESTService(fakeREST)
185     collection <- Collection$new(api, "myUUID")
186     fish <- collection$get("animal/fish")
187     
188     fileContent <- fish$write("new file content")
189
190     expect_that(fakeREST$writeBuffer, equals("new file content"))
191 }) 
192
193 test_that(paste("move raises exception if arvados file",
194                 "doesn't belong to any collection"), {
195
196     animal <- ArvadosFile$new("animal")
197
198     expect_that(animal$move("new/location"),
199                 throws_error("ArvadosFile doesn't belong to any collection"))
200 }) 
201
202 test_that(paste("move raises exception if newLocationInCollection",
203                 "parameter is invalid"), {
204
205
206     collectionContent <- c("animal",
207                            "animal/fish",
208                            "animal/dog",
209                            "animal/fish/shark",
210                            "ball")
211     fakeREST <- FakeRESTService$new(collectionContent)
212
213     api <- Arvados$new("myToken", "myHostName")
214     api$setRESTService(fakeREST)
215
216     collection <- Collection$new(api, "myUUID")
217     dog <- collection$get("animal/dog")
218
219     expect_that(dog$move("objects/dog"),
220                 throws_error("Unable to get destination subcollection"))
221 }) 
222
223 test_that("move raises exception if new location contains content with the same name", {
224
225
226     collectionContent <- c("animal",
227                            "animal/fish",
228                            "animal/dog",
229                            "animal/fish/shark",
230                            "dog")
231     fakeREST <- FakeRESTService$new(collectionContent)
232
233     api <- Arvados$new("myToken", "myHostName")
234     api$setRESTService(fakeREST)
235     collection <- Collection$new(api, "myUUID")
236     dog <- collection$get("animal/dog")
237
238     expect_that(dog$move("dog"),
239                 throws_error("Destination already contains content with same name."))
240
241 }) 
242
243 test_that("move moves arvados file inside collection tree", {
244
245
246     collectionContent <- c("animal",
247                            "animal/fish",
248                            "animal/dog",
249                            "animal/fish/shark",
250                            "ball")
251     fakeREST <- FakeRESTService$new(collectionContent)
252
253     api <- Arvados$new("myToken", "myHostName")
254     api$setRESTService(fakeREST)
255     collection <- Collection$new(api, "myUUID")
256     dog <- collection$get("animal/dog")
257
258     dog$move("dog")
259     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
260     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
261
262     expect_that(dogIsNullOnOldLocation, is_true())
263     expect_that(dogExistsOnNewLocation, is_true())
264 })