Factored out HttpRequest and HttpParser classes from Arvados class to
[arvados.git] / sdk / R / tests / testthat / test-Arvados.R
1 context("Arvados API")
2
3 source("fakes/FakeRESTService.R")
4
5 test_that("Constructor will use environment variables if no parameters are passed to it", {
6
7     Sys.setenv(ARVADOS_API_HOST  = "environment_api_host")
8     Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
9
10     arv <- Arvados$new()
11
12     Sys.unsetenv("ARVADOS_API_HOST")
13     Sys.unsetenv("ARVADOS_API_TOKEN")
14
15     expect_that("https://environment_api_host/arvados/v1/",
16                 equals(arv$getHostName())) 
17
18     expect_that("environment_api_token",
19                 equals(arv$getToken())) 
20 }) 
21
22 test_that("Constructor preferes constructor fields over environment variables", {
23
24     Sys.setenv(ARVADOS_API_HOST  = "environment_api_host")
25     Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
26
27     arv <- Arvados$new("constructor_api_token", "constructor_api_host")
28
29     Sys.unsetenv("ARVADOS_API_HOST")
30     Sys.unsetenv("ARVADOS_API_TOKEN")
31
32     expect_that("https://constructor_api_host/arvados/v1/",
33                 equals(arv$getHostName())) 
34
35     expect_that("constructor_api_token",
36                 equals(arv$getToken())) 
37 }) 
38
39 test_that("Constructor raises exception if fields and environment variables are not provided", {
40
41     expect_that(Arvados$new(),
42                 throws_error(paste0("Please provide host name and authentification token",
43                                     " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
44                                     " environment variables.")))
45 }) 
46
47 test_that("getCollection delegates operation to RESTService class", {
48
49     arv <- Arvados$new("token", "hostName")
50     fakeREST <- FakeRESTService$new()
51     arv$setRESTService(fakeREST)
52     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
53
54     arv$getCollection(collectionUUID)
55
56     expect_that(fakeREST$getResourceCallCount, equals(1))
57 }) 
58
59 test_that("listCollection delegates operation to RESTService class", {
60
61     arv <- Arvados$new("token", "hostName")
62     fakeREST <- FakeRESTService$new()
63     arv$setRESTService(fakeREST)
64
65     arv$listCollections()
66
67     expect_that(fakeREST$listResourcesCallCount, equals(1))
68 }) 
69
70 test_that("listAllCollection delegates operation to RESTService class", {
71
72     arv <- Arvados$new("token", "hostName")
73     fakeREST <- FakeRESTService$new()
74     arv$setRESTService(fakeREST)
75
76     arv$listAllCollections()
77
78     expect_that(fakeREST$fetchAllItemsCallCount, equals(1))
79 }) 
80
81 test_that("deleteCollection delegates operation to RESTService class", {
82
83     arv <- Arvados$new("token", "hostName")
84     fakeREST <- FakeRESTService$new()
85     arv$setRESTService(fakeREST)
86     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
87
88     arv$deleteCollection(collectionUUID)
89
90     expect_that(fakeREST$deleteResourceCallCount, equals(1))
91 }) 
92
93 test_that("updateCollection delegates operation to RESTService class", {
94
95     arv <- Arvados$new("token", "hostName")
96     fakeREST <- FakeRESTService$new()
97     arv$setRESTService(fakeREST)
98     newCollectionContent <- list(newName = "Brand new shiny name")
99     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
100
101     arv$updateCollection(collectionUUID, newCollectionContent)
102
103     expect_that(fakeREST$updateResourceCallCount, equals(1))
104 }) 
105
106 test_that("createCollection delegates operation to RESTService class", {
107
108     arv <- Arvados$new("token", "hostName")
109     fakeREST <- FakeRESTService$new()
110     arv$setRESTService(fakeREST)
111     collectionContent <- list(newName = "Brand new shiny name")
112
113     arv$createCollection(collectionContent)
114
115     expect_that(fakeREST$createResourceCallCount, equals(1))
116 }) 
117
118 test_that("getProject delegates operation to RESTService class", {
119
120     arv <- Arvados$new("token", "hostName")
121     fakeREST <- FakeRESTService$new()
122     arv$setRESTService(fakeREST)
123     projectUUID <- "aaaaa-j7d0g-ccccccccccccccc"
124
125     arv$getCollection(projectUUID)
126
127     expect_that(fakeREST$getResourceCallCount, equals(1))
128 }) 
129
130 test_that("listProjects delegates operation to RESTService class", {
131
132     arv <- Arvados$new("token", "hostName")
133     fakeREST <- FakeRESTService$new()
134     arv$setRESTService(fakeREST)
135
136     arv$listCollections()
137
138     expect_that(fakeREST$listResourcesCallCount, equals(1))
139 }) 
140
141 test_that("listAllProjects delegates operation to RESTService class", {
142
143     arv <- Arvados$new("token", "hostName")
144     fakeREST <- FakeRESTService$new()
145     arv$setRESTService(fakeREST)
146
147     arv$listAllProjects()
148
149     expect_that(fakeREST$fetchAllItemsCallCount, equals(1))
150 }) 
151
152 test_that("deleteProject delegates operation to RESTService class", {
153
154     arv <- Arvados$new("token", "hostName")
155     fakeREST <- FakeRESTService$new()
156     arv$setRESTService(fakeREST)
157     projectUUID <- "aaaaa-j7d0g-ccccccccccccccc"
158
159     arv$deleteCollection(projectUUID)
160
161     expect_that(fakeREST$deleteResourceCallCount, equals(1))
162 }) 
163
164 test_that("updateProject delegates operation to RESTService class", {
165
166     arv <- Arvados$new("token", "hostName")
167     fakeREST <- FakeRESTService$new()
168     arv$setRESTService(fakeREST)
169     newProjectContent <- list(newName = "Brand new shiny name")
170     projectUUID <- "aaaaa-j7d0g-ccccccccccccccc"
171
172     arv$updateCollection(projectUUID, newProjectContent)
173
174     expect_that(fakeREST$updateResourceCallCount, equals(1))
175 }) 
176
177 test_that("createProject delegates operation to RESTService class", {
178
179     arv <- Arvados$new("token", "hostName")
180     fakeREST <- FakeRESTService$new()
181     arv$setRESTService(fakeREST)
182     projectContent <- list(newName = "Brand new shiny name")
183
184     arv$createCollection(projectContent)
185
186     expect_that(fakeREST$createResourceCallCount, equals(1))
187 })