Implemented custom print functions for all public classes
[arvados.git] / sdk / R / tests / testthat / test-Collection.R
1 source("fakes/FakeRESTService.R")
2
3 context("Collection")
4
5 test_that(paste("constructor creates file tree from text content",
6                 "retreived form REST service"), {
7
8
9     collectionContent <- c("animal",
10                            "animal/fish",
11                            "ball")
12     fakeREST <- FakeRESTService$new(collectionContent)
13
14     api <- Arvados$new("myToken", "myHostName")
15     api$setRESTService(fakeREST)
16     collection <- Collection$new(api, "myUUID")
17
18     root <- collection$get("")
19
20     expect_that(fakeREST$getCollectionContentCallCount, equals(1))
21     expect_that(root$getName(), equals(""))
22 })
23
24 test_that(paste("add raises exception if passed argumet is not",
25                 "ArvadosFile or Subcollection"), {
26
27     collectionContent <- c("animal",
28                            "animal/fish",
29                            "ball")
30     fakeREST <- FakeRESTService$new(collectionContent)
31
32     api <- Arvados$new("myToken", "myHostName")
33     api$setRESTService(fakeREST)
34     collection <- Collection$new(api, "myUUID")
35
36     newNumber <- 10
37
38     expect_that(collection$add(newNumber),
39     throws_error(paste("Expected AravodsFile or Subcollection",
40                        "object, got (numeric)."), fixed = TRUE))
41 })
42
43 test_that("add raises exception if relative path is not valid", {
44
45     collectionContent <- c("animal",
46                            "animal/fish",
47                            "ball")
48     fakeREST <- FakeRESTService$new(collectionContent)
49
50     api <- Arvados$new("myToken", "myHostName")
51     api$setRESTService(fakeREST)
52     collection <- Collection$new(api, "myUUID")
53
54     newPen <- ArvadosFile$new("pen")
55
56     expect_that(collection$add(newPen, "objects"),
57                 throws_error("Subcollection objects doesn't exist.",
58                               fixed = TRUE))
59 })
60
61 test_that(paste("add adds ArvadosFile or Subcollection",
62                 "to local tree structure and remote REST service"), {
63
64     collectionContent <- c("animal",
65                            "animal/fish",
66                            "ball")
67     fakeREST <- FakeRESTService$new(collectionContent)
68
69     api <- Arvados$new("myToken", "myHostName")
70     api$setRESTService(fakeREST)
71     collection <- Collection$new(api, "myUUID")
72
73     newDog <- ArvadosFile$new("dog")
74     collection$add(newDog, "animal")
75
76     dog <- collection$get("animal/dog")
77     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
78
79     expect_that(dogExistsInCollection, is_true())
80     expect_that(fakeREST$createCallCount, equals(1))
81 })
82
83 test_that("create raises exception if passed argumet is not character vector", {
84
85     collectionContent <- c("animal",
86                            "animal/fish",
87                            "ball")
88     fakeREST <- FakeRESTService$new(collectionContent)
89
90     api <- Arvados$new("myToken", "myHostName")
91     api$setRESTService(fakeREST)
92     collection <- Collection$new(api, "myUUID")
93
94     expect_that(collection$create(10),
95                 throws_error("Expected character vector, got (numeric).", 
96                              fixed = TRUE))
97 })
98
99 test_that("create raises exception if relative path is not valid", {
100
101     collectionContent <- c("animal",
102                            "animal/fish",
103                            "ball")
104     fakeREST <- FakeRESTService$new(collectionContent)
105
106     api <- Arvados$new("myToken", "myHostName")
107     api$setRESTService(fakeREST)
108     collection <- Collection$new(api, "myUUID")
109
110     newPen <- ArvadosFile$new("pen")
111
112     expect_that(collection$create(newPen, "objects"),
113                 throws_error("Subcollection objects doesn't exist.",
114                               fixed = TRUE))
115 })
116
117 test_that(paste("create adds files specified by fileNames",
118                 "to local tree structure and remote REST service"), {
119
120     collectionContent <- c("animal",
121                            "animal/fish",
122                            "ball")
123     fakeREST <- FakeRESTService$new(collectionContent)
124
125     api <- Arvados$new("myToken", "myHostName")
126     api$setRESTService(fakeREST)
127     collection <- Collection$new(api, "myUUID")
128
129     files <- c("dog", "cat")
130     collection$create(files, "animal")
131
132     dog <- collection$get("animal/dog")
133     cat <- collection$get("animal/cat")
134     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
135     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
136
137     expect_that(dogExistsInCollection, is_true())
138     expect_that(catExistsInCollection, is_true())
139     expect_that(fakeREST$createCallCount, equals(2))
140 })
141
142 test_that("remove raises exception if passed argumet is not character vector", {
143
144     collectionContent <- c("animal",
145                            "animal/fish",
146                            "ball")
147     fakeREST <- FakeRESTService$new(collectionContent)
148
149     api <- Arvados$new("myToken", "myHostName")
150     api$setRESTService(fakeREST)
151     collection <- Collection$new(api, "myUUID")
152
153     expect_that(collection$remove(10),
154                 throws_error("Expected character vector, got (numeric).", 
155                              fixed = TRUE))
156 })
157
158 test_that(paste("remove removes files specified by paths",
159                 "from local tree structure and from remote REST service"), {
160
161     collectionContent <- c("animal",
162                            "animal/fish",
163                            "animal/dog",
164                            "animal/cat",
165                            "ball")
166     fakeREST <- FakeRESTService$new(collectionContent)
167
168     api <- Arvados$new("myToken", "myHostName")
169     api$setRESTService(fakeREST)
170     collection <- Collection$new(api, "myUUID")
171
172     collection$remove(c("animal/dog", "animal/cat"))
173
174     dog <- collection$get("animal/dog")
175     cat <- collection$get("animal/dog")
176     dogExistsInCollection <- !is.null(dog) && dog$getName() == "dog"
177     catExistsInCollection <- !is.null(cat) && cat$getName() == "cat"
178
179     expect_that(dogExistsInCollection, is_false())
180     expect_that(catExistsInCollection, is_false())
181     expect_that(fakeREST$deleteCallCount, equals(2))
182 })
183
184 test_that(paste("move moves content to a new location inside file tree",
185                 "and on REST service"), {
186
187     collectionContent <- c("animal",
188                            "animal/dog",
189                            "ball")
190     fakeREST <- FakeRESTService$new(collectionContent)
191
192     api <- Arvados$new("myToken", "myHostName")
193     api$setRESTService(fakeREST)
194     collection <- Collection$new(api, "myUUID")
195
196     collection$move("animal/dog", "dog")
197
198     dogIsNullOnOldLocation <- is.null(collection$get("animal/dog"))
199     dogExistsOnNewLocation <- !is.null(collection$get("dog"))
200
201     expect_that(dogIsNullOnOldLocation, is_true())
202     expect_that(dogExistsOnNewLocation, is_true())
203     expect_that(fakeREST$moveCallCount, equals(1))
204 })
205
206 test_that("move raises exception if new location is not valid", {
207
208     collectionContent <- c("animal",
209                            "animal/fish",
210                            "ball")
211     fakeREST <- FakeRESTService$new(collectionContent)
212
213     api <- Arvados$new("myToken", "myHostName")
214     api$setRESTService(fakeREST)
215     collection <- Collection$new(api, "myUUID")
216
217     expect_that(collection$move("fish", "object"),
218                 throws_error("Content you want to move doesn't exist in the collection.",
219                              fixed = TRUE))
220 })
221
222 test_that("getFileListing returns sorted collection content received from REST service", {
223
224     collectionContent <- c("animal",
225                            "animal/fish",
226                            "ball")
227     fakeREST <- FakeRESTService$new(collectionContent)
228
229     api <- Arvados$new("myToken", "myHostName")
230     api$setRESTService(fakeREST)
231     collection <- Collection$new(api, "myUUID")
232
233     contentMatchExpected <- all(collection$getFileListing() == 
234                                 c("animal", "animal/fish", "ball"))
235
236     expect_that(contentMatchExpected, is_true())
237     #2 calls because Collection$new calls getFileListing once
238     expect_that(fakeREST$getCollectionContentCallCount, equals(2))
239
240 })
241
242 test_that("get returns arvados file or subcollection from internal tree structure", {
243
244     collectionContent <- c("animal",
245                            "animal/fish",
246                            "ball")
247     fakeREST <- FakeRESTService$new(collectionContent)
248
249     api <- Arvados$new("myToken", "myHostName")
250     api$setRESTService(fakeREST)
251     collection <- Collection$new(api, "myUUID")
252
253     fish <- collection$get("animal/fish")
254     fishIsNotNull <- !is.null(fish)
255
256     expect_that(fishIsNotNull, is_true())
257     expect_that(fish$getName(), equals("fish"))
258 })