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