-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-endpoints-validate-survey-and-programme.R
260 lines (234 loc) · 9.81 KB
/
test-endpoints-validate-survey-and-programme.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
test_that("endpoint_validate_survey_programme supports programme file", {
input <- validate_programme_survey_input(
file.path("testdata", "programme.csv"),
"programme",
file.path("testdata", "malawi.geojson"))
response <- validate_survey_programme(input)
expect_equal(response$filename, scalar("original"))
expect_equal(response$hash, scalar("12345"))
expect_equal(response$fromADR, scalar(FALSE))
## Sanity check that data has been returned
expect_true(nrow(response$data) >= 200)
expect_type(response$data[, "art_current"], "double")
expect_length(response$warnings, 0)
})
test_that("endpoint_validate_survey_programme returns error on invalid programme data", {
input <- validate_programme_survey_input(
file.path("testdata", "malformed_programme.csv"),
"programme",
file.path("testdata", "malawi.geojson"))
error <- expect_error(validate_survey_programme(input))
expect_equal(error$data[[1]]$error, scalar("INVALID_FILE"))
expect_equal(
error$data[[1]]$detail,
scalar("Data missing column calendar_quarter."))
expect_equal(error$status_code, 400)
})
test_that("endpoint_validate_survey_programme supports ANC file", {
input <- validate_programme_survey_input(
file.path("testdata", "anc.csv"),
"anc",
file.path("testdata", "malawi.geojson"))
response <- validate_survey_programme(input)
expect_equal(response$filename, scalar("original"))
expect_equal(response$hash, scalar("12345"))
expect_equal(response$fromADR, scalar(FALSE))
## Sanity check that data has been returned
expect_true(nrow(response$data) >= 200)
expect_type(response$data[, "anc_prevalence"], "double")
expect_type(response$data[, "anc_art_coverage"], "double")
expect_length(response$warnings, 0)
})
test_that("endpoint_validate_survey_programme returns error on invalid ANC data", {
input <- validate_programme_survey_input(
file.path("testdata", "malformed_anc.csv"),
"anc",
file.path("testdata", "malawi.geojson"))
error <- expect_error(validate_survey_programme(input))
expect_equal(error$data[[1]]$error, scalar("INVALID_FILE"))
expect_equal(
error$data[[1]]$detail,
scalar(paste0("Anc file contains regions for more than one country. ",
"Got countries MWI, AGO.")))
expect_equal(error$status_code, 400)
})
test_that("endpoint_validate_survey_programme supports survey file", {
survey <- file.path("testdata", "survey.csv")
input <- validate_programme_survey_input(
file.path("testdata", "survey.csv"),
"survey",
file.path("testdata", "malawi.geojson"))
response <- validate_survey_programme(input)
expect_equal(response$filename, scalar("original"))
expect_equal(response$hash, scalar("12345"))
expect_equal(response$fromADR, scalar(FALSE))
## Sanity check that data has been returned
expect_true(nrow(response$data) >= 20000)
expect_type(response$data[, "estimate"], "double")
expect_length(response$warnings, 0)
})
test_that("endpoint_validate_survey_programme returns error on invalid survey data", {
input <- validate_programme_survey_input(
file.path("testdata", "malformed_survey.csv"),
"survey",
file.path("testdata", "malawi.geojson"))
error <- expect_error(validate_survey_programme(input))
expect_equal(error$data[[1]]$error, scalar("INVALID_FILE"))
expect_equal(
error$data[[1]]$detail,
scalar("Data missing column survey_id."))
expect_equal(error$status_code, 400)
})
test_that("endpoint_validate_survey_programme programme", {
endpoint <- endpoint_validate_survey_programme()
response <- endpoint$run(
system_file("payload", "validate_programme_payload.json"))
expect_equal(response$status_code, 200)
expect_null(response$error)
expect_equal(response$data$filename, scalar("original.csv"))
expect_equal(response$data$hash, scalar("12345"))
expect_equal(response$data$fromADR, scalar(FALSE))
## Sanity check that data has been returned
expect_true(nrow(response$data$data) >= 200)
expect_type(response$data$data[, "art_current"], "double")
expect_length(response$data$warnings, 0)
})
test_that("endpoint_validate_survey_programme works with programme data", {
test_redis_available()
queue <- test_queue(workers = 0)
api <- api_build(queue)
res <- api$request(
"POST", "/validate/survey-and-programme",
body = system_file("payload", "validate_programme_payload.json"))
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$status, "success")
expect_null(body$errors)
expect_equal(body$data$filename, "original.csv")
expect_equal(body$data$hash, "12345")
expect_equal(body$data$fromADR, FALSE)
## Sanity check that data has been returned
expect_true(nrow(body$data$data) >= 200)
expect_type(body$data$data[, "art_current"], "integer")
expect_length(body$data$warnings, 0)
})
test_that("endpoint_validate_survey_programme anc", {
endpoint <- endpoint_validate_survey_programme()
response <- endpoint$run(system_file("payload", "validate_anc_payload.json"))
expect_equal(response$status_code, 200)
expect_null(response$error)
expect_equal(response$data$filename, scalar("original.csv"))
expect_equal(response$data$hash, scalar("12345"))
expect_equal(response$data$fromADR, scalar(FALSE))
## Sanity check that data has been returned
expect_true(nrow(response$data$data) >= 200)
expect_type(response$data$data[, "anc_prevalence"], "double")
expect_type(response$data$data[, "anc_art_coverage"], "double")
expect_length(response$data$warnings, 0)
})
test_that("endpoint_validate_survey_programme works with anc data", {
test_redis_available()
queue <- test_queue(workers = 0)
api <- api_build(queue)
res <- api$request(
"POST", "/validate/survey-and-programme",
body = system_file("payload", "validate_anc_payload.json"))
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$status, "success")
expect_null(body$errors)
expect_equal(body$data$filename, "original.csv")
expect_equal(body$data$hash, "12345")
expect_equal(body$data$fromADR, FALSE)
## Sanity check that data has been returned
expect_true(nrow(body$data$data) >= 200)
expect_type(body$data$data[, "anc_prevalence"], "double")
expect_type(body$data$data[, "anc_art_coverage"], "double")
expect_length(body$data$warnings, 0)
})
test_that("endpoint_validate_survey_programme survey", {
endpoint <- endpoint_validate_survey_programme()
response <- endpoint$run(
system_file("payload", "validate_survey_payload.json"))
expect_equal(response$status_code, 200)
expect_null(response$error)
expect_equal(response$data$filename, scalar("original.csv"))
expect_equal(response$data$hash, scalar("12345"))
expect_equal(response$data$fromADR, scalar(FALSE))
## Sanity check that data has been returned
expect_true(nrow(response$data$data) >= 20000)
expect_type(response$data$data[, "estimate"], "double")
})
test_that("endpoint_validate_survey_programme works with survey data", {
test_redis_available()
queue <- test_queue(workers = 0)
api <- api_build(queue)
res <- api$request(
"POST", "/validate/survey-and-programme",
body = system_file("payload", "validate_survey_payload.json"))
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$status, "success")
expect_null(body$errors)
expect_equal(body$data$filename, "original.csv")
expect_equal(body$data$fromADR, FALSE)
expect_equal(body$data$hash, "12345")
## Sanity check that data has been returned
expect_true(nrow(body$data$data) >= 20000)
expect_type(body$data$data[, "estimate"], "double")
})
test_that("endpoint_validate_survey_programme vmmc", {
endpoint <- endpoint_validate_survey_programme()
response <- endpoint$run(
system_file("payload", "validate_vmmc_payload.json"))
expect_equal(response$status_code, 200)
expect_null(response$error)
expect_equal(response$data$filename, scalar("original.xlsx"))
expect_equal(response$data$hash, scalar("12345"))
expect_equal(response$data$fromADR, scalar(FALSE))
## No data returned from this endpoint
expect_equal(response$data$data, json_verbatim("null"))
expect_equal(response$data$warnings, list())
})
test_that("anc data can be validated can be run with relaxed validation", {
testthat::skip("skipping as erroring after adding quick fix for Zim")
test_redis_available()
## Create some data which will fail when validation is strict but
## not otherwise
anc <- read.csv("testdata/anc.csv")
anc[1, "anc_tested_pos"] <- anc[1, "anc_tested"] + 5
t <- tempfile(fileext = ".csv")
write.csv(anc, t, row.names = FALSE)
input <- validate_programme_survey_input(
normalizePath(t, winslash = "/"),
"anc",
file.path("testdata", "malawi.geojson"))
queue <- test_queue(workers = 0)
api <- api_build(queue)
## In strict mode
res <- api$request("POST", "/validate/survey-and-programme",
body = input)
expect_equal(res$status, 400)
body <- jsonlite::fromJSON(res$body, simplifyVector = FALSE)
expect_equal(body$status, "failure")
expect_length(body$errors, 1)
expect_equal(body$errors[[1]]$error, "INVALID_FILE")
expect_equal(body$errors[[1]]$detail,
paste0("The number of people who tested positive is greater than the ",
"number of people tested"))
## With strict = FALSE
res <- api$request("POST", "/validate/survey-and-programme",
query = list(strict = "false"),
body = input)
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$status, "success")
expect_null(body$errors)
expect_equal(body$data$hash, "12345")
expect_equal(body$data$fromADR, FALSE)
## Sanity check that data has been returned
expect_true(nrow(body$data$data) >= 200)
expect_type(body$data$data[, "anc_prevalence"], "double")
expect_type(body$data$data[, "anc_art_coverage"], "double")
expect_length(body$data$warnings, 0)
})