Added a few unit test for Arvados class
[arvados.git] / sdk / R / tests / testthat / test-Arvados.R
1 context("Arvados API")
2
3 source("fakes/FakeHttpRequest.R")
4 source("fakes/FakeHttpParser.R")
5
6 test_that("Constructor will use environment variables if no parameters are passed to it", {
7
8     Sys.setenv(ARVADOS_API_HOST  = "environment_api_host")
9     Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
10
11     arv <- Arvados$new()
12
13     Sys.unsetenv("ARVADOS_API_HOST")
14     Sys.unsetenv("ARVADOS_API_TOKEN")
15
16     expect_that("https://environment_api_host/arvados/v1/",
17                 equals(arv$getHostName())) 
18
19     expect_that("environment_api_token",
20                 equals(arv$getToken())) 
21 }) 
22
23 test_that("Constructor preferes constructor fields over environment variables", {
24
25     Sys.setenv(ARVADOS_API_HOST  = "environment_api_host")
26     Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
27
28     arv <- Arvados$new("constructor_api_token", "constructor_api_host")
29
30     Sys.unsetenv("ARVADOS_API_HOST")
31     Sys.unsetenv("ARVADOS_API_TOKEN")
32
33     expect_that("https://constructor_api_host/arvados/v1/",
34                 equals(arv$getHostName())) 
35
36     expect_that("constructor_api_token",
37                 equals(arv$getToken())) 
38 }) 
39
40 test_that("Constructor raises exception if fields and environment variables are not provided", {
41
42     expect_that(Arvados$new(),
43                 throws_error(paste0("Please provide host name and authentification token",
44                                     " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
45                                     " environmental variables.")))
46 }) 
47
48 test_that("getWebDavHostName calls REST service properly", {
49
50     hostName <- "hostName"
51     token    <- "token"
52     arv      <- Arvados$new(token, hostName)
53
54     serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
55     expectedURL    <- paste0("https://", hostName,
56                              "/discovery/v1/apis/arvados/v1/rest")
57
58     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
59     arv$setHttpClient(httpRequest)
60     arv$setHttpParser(FakeHttpParser$new())
61
62     webDAVHostName <- arv$getWebDavHostName()
63
64     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
65     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
66     expect_that(httpRequest$numberOfGETRequests, equals(1))
67 }) 
68
69 test_that("getWebDavHostName returns webDAV host name properly", {
70
71     arv <- Arvados$new("token", "hostName")
72
73     serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
74
75     httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
76     arv$setHttpClient(httpRequest)
77     arv$setHttpParser(FakeHttpParser$new())
78
79     expect_that("https://myWebDavServer.com", equals(arv$getWebDavHostName())) 
80 }) 
81
82 test_that("getCollection calls REST service properly", {
83
84     arv <- Arvados$new("token", "hostName")
85
86     serverResponse <- NULL
87     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
88     expectedURL    <- paste0(arv$getHostName(), "collections/", collectionUUID)
89
90     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
91     arv$setHttpClient(httpRequest)
92     arv$setHttpParser(FakeHttpParser$new())
93
94     arv$getCollection(collectionUUID)
95
96     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
97     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
98     expect_that(httpRequest$numberOfGETRequests, equals(1))
99 }) 
100
101 test_that("getCollection parses server response", {
102
103     arv <- Arvados$new("token", "hostName")
104
105     httpParser <- FakeHttpParser$new()
106     arv$setHttpParser(httpParser)
107     arv$setHttpClient(FakeHttpRequest$new())
108
109     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
110     arv$getCollection(collectionUUID)
111
112     expect_that(httpParser$parserCallCount, equals(1))
113 }) 
114
115 test_that("getCollection raises exception if response contains errors field", {
116
117     arv <- Arvados$new("token", "hostName")
118     
119     serverResponse <- list(errors = 404)
120     arv$setHttpClient(FakeHttpRequest$new(NULL, serverResponse))
121     arv$setHttpParser(FakeHttpParser$new())
122
123     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
124     
125     expect_that(arv$getCollection(collectionUUID), 
126                 throws_error(404))
127 }) 
128
129 test_that("listCollections calls REST service properly", {
130
131     arv <- Arvados$new("token", "hostName")
132
133     serverResponse <- NULL
134     expectedURL    <- paste0(arv$getHostName(), "collections")
135
136     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
137     arv$setHttpClient(httpRequest)
138     arv$setHttpParser(FakeHttpParser$new())
139
140     arv$listCollections()
141
142     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
143     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
144     expect_that(httpRequest$numberOfGETRequests, equals(1))
145 }) 
146
147 test_that("listCollections parses server response", {
148
149     arv <- Arvados$new("token", "hostName")
150
151     httpParser <- FakeHttpParser$new()
152     arv$setHttpParser(httpParser)
153     arv$setHttpClient(FakeHttpRequest$new())
154
155     arv$listCollections()
156
157     expect_that(httpParser$parserCallCount, equals(1))
158 }) 
159
160 test_that("listCollections raises exception if response contains errors field", {
161
162     arv <- Arvados$new("token", "hostName")
163     
164     serverResponse <- list(errors = 404)
165     expectedURL <- NULL
166     arv$setHttpClient(FakeHttpRequest$new(expectedURL, serverResponse))
167     arv$setHttpParser(FakeHttpParser$new())
168
169     expect_that(arv$listCollections(), 
170                 throws_error(404))
171 }) 
172
173 test_that("deleteCollection calls REST service properly", {
174
175     arv <- Arvados$new("token", "hostName")
176
177     serverResponse <- NULL
178     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
179     expectedURL    <- paste0(arv$getHostName(), "collections/", collectionUUID)
180
181     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
182     arv$setHttpClient(httpRequest)
183     arv$setHttpParser(FakeHttpParser$new())
184
185     arv$deleteCollection(collectionUUID)
186
187     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
188     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
189     expect_that(httpRequest$numberOfDELETERequests, equals(1))
190 }) 
191
192 test_that("deleteCollection parses server response", {
193
194     arv <- Arvados$new("token", "hostName")
195
196     httpParser <- FakeHttpParser$new()
197     arv$setHttpParser(httpParser)
198     arv$setHttpClient(FakeHttpRequest$new())
199
200     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
201     arv$deleteCollection(collectionUUID)
202
203     expect_that(httpParser$parserCallCount, equals(1))
204 }) 
205
206 test_that("getCollection raises exception if response contains errors field", {
207
208     arv <- Arvados$new("token", "hostName")
209     
210     serverResponse <- list(errors = 404)
211     arv$setHttpClient(FakeHttpRequest$new(NULL, serverResponse))
212     arv$setHttpParser(FakeHttpParser$new())
213
214     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
215     
216     expect_that(arv$deleteCollection(collectionUUID), 
217                 throws_error(404))
218 })