Skip to content

Commit

Permalink
#51 Issue: get randomization list + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salatak committed Feb 21, 2024
1 parent e94c746 commit 567d911
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
37 changes: 37 additions & 0 deletions R/api_get_randomization_list.R
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions inst/plumber/unbiased_api/study.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 /<study_id:int>/randomization_list
#* @serializer unboxedJSON
#*

sentryR::with_captured_calls(function(study_id, req, res) {
return(
unbiased:::api_get_rand_list(study_id, req, res)
)
})
50 changes: 49 additions & 1 deletion tests/testthat/test-E2E-get-study.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") |>
Expand Down Expand Up @@ -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)
})

0 comments on commit 567d911

Please sign in to comment.