From 567d911217bfa43927c50e26fb3852f736340b0d Mon Sep 17 00:00:00 2001 From: Kinga Date: Wed, 21 Feb 2024 14:08:12 +0000 Subject: [PATCH] #51 Issue: get randomization list + tests --- R/api_get_randomization_list.R | 37 +++++++++++++++++++++ inst/plumber/unbiased_api/study.R | 15 +++++++++ tests/testthat/test-E2E-get-study.R | 50 ++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 R/api_get_randomization_list.R diff --git a/R/api_get_randomization_list.R b/R/api_get_randomization_list.R new file mode 100644 index 0000000..2babe95 --- /dev/null +++ b/R/api_get_randomization_list.R @@ -0,0 +1,37 @@ +api_get_rand_list <- function(study_id, req, res) { + db_connection_pool <- get("db_connection_pool") + + study_id <- req$args$study_id + + is_study <- + checkmate::test_true( + dplyr::tbl(db_connection_pool, "study") |> + dplyr::filter(id == study_id) |> + dplyr::collect() |> + nrow() > 0 + ) + + if (!is_study) { + res$status <- 404 + return(list( + error = "Study not found" + )) + } + + patients <- + dplyr::tbl(db_connection_pool, "patient") |> + dplyr::filter(study_id == !!study_id) |> + dplyr::left_join( + dplyr::tbl(db_connection_pool, "arm") |> + dplyr::select(arm_id = id, arm = name), + by = "arm_id" + ) |> + dplyr::select( + patient_id = id, arm, used, sys_period + ) |> + dplyr::collect() |> + dplyr::mutate(sys_period = as.character(gsub("\\[\"|\\+00\",\\)", "", sys_period))) |> + dplyr::mutate(sys_period = as.POSIXct(sys_period)) + + return(patients) +} diff --git a/inst/plumber/unbiased_api/study.R b/inst/plumber/unbiased_api/study.R index 93fa34e..dad162e 100644 --- a/inst/plumber/unbiased_api/study.R +++ b/inst/plumber/unbiased_api/study.R @@ -74,3 +74,18 @@ sentryR::with_captured_calls(function(study_id, req, res) { unbiased:::api_get_study_records(study_id, req, res) ) }) + +#* Get randomization list +#* +#* @param study_id:int Study identifier +#* +#* @tag read +#* @get //randomization_list +#* @serializer unboxedJSON +#* + +sentryR::with_captured_calls(function(study_id, req, res) { + return( + unbiased:::api_get_rand_list(study_id, req, res) + ) +}) diff --git a/tests/testthat/test-E2E-get-study.R b/tests/testthat/test-E2E-get-study.R index 038c111..191dc3f 100644 --- a/tests/testthat/test-E2E-get-study.R +++ b/tests/testthat/test-E2E-get-study.R @@ -40,7 +40,7 @@ test_that("correct request to reads studies with the structure of the returned r testthat::expect_equal(length(response_body), n_studies) }) -test_that("correct request to reads records for chosen study_id with the structure of the returned result", { +test_that("requests to reads records for chosen study_id with the structure of the returned result", { response <- request(api_url) |> req_url_path("study", "minimisation_pocock") |> req_method("POST") |> @@ -110,3 +110,51 @@ test_that("correct request to reads records for chosen study_id with the structu testthat::expect_equal(response_study_id$status, 404) }) + +test_that("correct request to reads randomization list with the structure of the returned result", { + source("./test-helpers.R") + + conn <- pool::localCheckout( + get("db_connection_pool", envir = globalenv()) + ) + with_db_fixtures("fixtures/example_study.yml") + + response <- + request(api_url) |> + req_url_path("/study/1/randomization_list") |> + req_method("GET") |> + req_perform() + + response_body <- + response |> + resp_body_json() + + testthat::expect_equal(response$status_code, 200) + + checkmate::expect_names( + names(response_body[[1]]), + identical.to = c("patient_id", "arm", "used", "sys_period") + ) +}) + +test_that("incorrect input study_id to reads randomization list", { + source("./test-helpers.R") + + conn <- pool::localCheckout( + get("db_connection_pool", envir = globalenv()) + ) + with_db_fixtures("fixtures/example_study.yml") + + response <- + tryCatch( + { + request(api_url) |> + req_url_path("study/100/randomization_list") |> + req_method("GET") |> + req_perform() + }, + error = function(e) e + ) + + testthat::expect_equal(response$status, 404) +})