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