From ec3b4ec4c204875975df86138cdb72b5cbb73fc2 Mon Sep 17 00:00:00 2001 From: "alex.hill@gmail.com" Date: Wed, 11 Dec 2024 13:21:43 +0000 Subject: [PATCH] update tests --- R/api.R | 2 +- tests/testthat/helper.R | 15 +++++++++++++++ tests/testthat/test-upload.R | 12 +++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/R/api.R b/R/api.R index 144969a..659731a 100644 --- a/R/api.R +++ b/R/api.R @@ -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)) diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 66a21a3..a66f74d 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -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) @@ -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 = "") { @@ -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, "--") @@ -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({ @@ -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" @@ -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, "--") @@ -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, "--") diff --git a/tests/testthat/test-upload.R b/tests/testthat/test-upload.R index ad9b275..4d110a8 100644 --- a/tests/testthat/test-upload.R +++ b/tests/testthat/test-upload.R @@ -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) @@ -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", { @@ -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) @@ -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", {