Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Dec 11, 2024
1 parent d8c5983 commit ec3b4ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ target_post_dataset <- function(req, res) {
logger::log_info("Parsing multipart form request")
parsed <- mime::parse_multipart(req)
xcol <- get_xcol(parsed)
series_type <- parsed$type
series_type <- parsed$series_type
name <- get_dataset_name(parsed)
if (is.null(parsed$file$type) || parsed$file$type != "text/csv") {
return(invalid_file_type(res))
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local_add_dataset <- function(dat, name, session = session_id, env = parent.fram
write.csv(dat, file.path(filepath, "data"), row.names = FALSE)
write("day", file.path(filepath, "xcol"))
write("number", file.path(filepath, "xtype"))
write("surveillance", file.path(filepath, "series_type"))
withr::defer({
if (dir.exists(filepath)) {
fs::dir_delete(filepath)
Expand All @@ -33,6 +34,7 @@ local_add_dataset <- function(dat, name, session = session_id, env = parent.fram

local_POST_dataset_request <- function(dat, filename,
xcol = "day",
series_type = "surveillance",
env = parent.frame(),
session = session_id,
cookie = "") {
Expand All @@ -44,6 +46,9 @@ local_POST_dataset_request <- function(dat, filename,
"Content-Type: text/csv", EOL, EOL,
readr::format_csv(dat, eol = EOL), EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
series_type, EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"xcol\"", EOL, EOL,
xcol, EOL,
boundary, "--")
Expand All @@ -70,6 +75,9 @@ local_POST_dataset_request_no_xcol <- function(dat, filename,
EOL,
"Content-Type: text/csv", EOL, EOL,
readr::format_csv(dat, eol = EOL), EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
"surveillance", EOL,
boundary, "--")
filepath <- file.path("uploads", session_id, filename)
withr::defer({
Expand All @@ -88,6 +96,7 @@ local_POST_dataset_request_with_name <- function(dat,
filename,
name,
xcol = "day",
series_type = "surveillance",
env = parent.frame(),
cookie = cookie) {
EOL <- "\r\n"
Expand All @@ -101,6 +110,9 @@ local_POST_dataset_request_with_name <- function(dat,
"Content-Disposition: form-data; name=\"xcol\"", EOL, EOL,
xcol, EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
series_type, EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"name\"", EOL, EOL,
name, EOL,
boundary, "--")
Expand Down Expand Up @@ -128,6 +140,9 @@ local_POST_dataset_request_bad_file <- function(env = parent.frame()) {
"Content-Type: image/png", EOL, EOL,
"1234", EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
"surveillance", EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"xcol\"", EOL, EOL,
"day", EOL,
boundary, "--")
Expand Down
12 changes: 9 additions & 3 deletions tests/testthat/test-upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ test_that("uploading wrong file type returns 400", {
"Invalid file type; please upload file of type text/csv.")
})

test_that("saves file and xcol", {
test_that("saves file, xcol and series_type", {
router <- build_routes(cookie_key)
request <- local_POST_dataset_request(data.frame(biomarker = "ab",
time = 1:10,
value = 1),
filename = "testdataset",
xcol = "time",
series_type = "surveillance",
cookie = cookie)
res <- router$call(request)
expect_equal(res$status, 200)
Expand All @@ -115,6 +116,8 @@ test_that("saves file and xcol", {
expect_equal(nrow(dat), 10)
xcol <- readLines(file.path("uploads", session_id, "/testdataset/xcol"))
expect_equal(xcol, "time")
xcol <- readLines(file.path("uploads", session_id, "/testdataset/series_type"))
expect_equal(xcol, "surveillance")
})

test_that("can get uploaded dataset metadata with default xcol", {
Expand All @@ -140,14 +143,16 @@ test_that("can get uploaded dataset metadata with default xcol", {
expect_equal(body$data$xcol, "day")
})

test_that("can get uploaded dataset metadata with xcol", {
test_that("can get uploaded dataset metadata with xcol and series_type", {
request <- local_POST_dataset_request(data.frame(biomarker = c("ab", "ba"),
value = 1,
time = 1:10,
age = "0-5",
sex = c("M", "F")),
"testdata",
xcol = "time", cookie = cookie)
xcol = "time",
series_type = "post-exposure",
cookie = cookie)
router <- build_routes(cookie_key)
res <- router$call(request)
expect_equal(res$status, 200)
Expand All @@ -160,6 +165,7 @@ test_that("can get uploaded dataset metadata with xcol", {
expect_equal(body$data$variables$levels, list(c("0-5"), c("M", "F")))
expect_equal(body$data$biomarkers, c("ab", "ba"))
expect_equal(body$data$xcol, "time")
expect_equal(body$data$type, "post-exposure")
})

test_that("can get uploaded dataset without covariates", {
Expand Down

0 comments on commit ec3b4ec

Please sign in to comment.