From 7911fefc41fe278a07ceb8d3bd820c2cae9a4bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz?= Date: Tue, 26 Mar 2024 13:53:22 +0000 Subject: [PATCH 1/2] Fix too aggressive current state validation in randomization endpoint --- R/api_randomize.R | 2 +- .../test-E2E-study-minimisation-pocock.R | 78 ++++++++++++++++++- tests/testthat/test-api-audit-log.R | 4 +- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/R/api_randomize.R b/R/api_randomize.R index 18db0f3..d2314c4 100644 --- a/R/api_randomize.R +++ b/R/api_randomize.R @@ -59,7 +59,7 @@ api__randomize_patient <- function(study_id, current_state, req, res) { checkmate::assert( checkmate::check_data_frame(current_state, any.missing = TRUE, - all.missing = FALSE, nrows = 2, ncols = 3 + all.missing = FALSE, min.rows = 1 ), .var.name = "current_state", add = collection diff --git a/tests/testthat/test-E2E-study-minimisation-pocock.R b/tests/testthat/test-E2E-study-minimisation-pocock.R index 65244ab..517c0bf 100644 --- a/tests/testthat/test-E2E-study-minimisation-pocock.R +++ b/tests/testthat/test-E2E-study-minimisation-pocock.R @@ -59,7 +59,7 @@ test_that("correct request with the structure of the returned result", { response_patient |> resp_body_json() - testthat::expect_equal(response$status_code, 200) + testthat::expect_equal(response_patient$status_code, 200) checkmate::expect_number(response_patient_body$patient_id, lower = 1) # Endpoint Response Structure Test @@ -502,3 +502,79 @@ test_that("request with incorrect ratio", { testthat::expect_equal(response_ratio$status, 400) }) + +test_that("randomization works for 1 patient", { + with_db_fixtures("fixtures/example_db.yml") + response_patient <- request(api_url) |> + req_url_path("study", "1", "patient") |> + req_method("POST") |> + req_error(is_error = \(x) FALSE) |> + req_body_json( + data = list( + current_state = + tibble::tibble( + "gender" = c("F"), + "arm" = c("") + ) + ) + ) |> + req_perform() + + response_patient_body <- + response_patient |> + resp_body_json() + + print("######################") + print(response_patient_body) + print("######################") + testthat::expect_equal(response_patient$status_code, 200) + checkmate::expect_number(response_patient_body$patient_id, lower = 1) +}) +test_that("randomization works for 2 patients", { + with_db_fixtures("fixtures/example_db.yml") + response_patient <- request(api_url) |> + req_url_path("study", "1", "patient") |> + req_method("POST") |> + req_error(is_error = \(x) FALSE) |> + req_body_json( + data = list( + current_state = + tibble::tibble( + "gender" = c("F", "M"), + "arm" = c("placebo", "") + ) + ) + ) |> + req_perform() + + response_patient_body <- + response_patient |> + resp_body_json() + + testthat::expect_equal(response_patient$status_code, 200) + checkmate::expect_number(response_patient_body$patient_id, lower = 1) +}) +test_that("randomization works for 3 patients", { + with_db_fixtures("fixtures/example_db.yml") + response_patient <- request(api_url) |> + req_url_path("study", "1", "patient") |> + req_method("POST") |> + req_error(is_error = \(x) FALSE) |> + req_body_json( + data = list( + current_state = + tibble::tibble( + "gender" = c("F", "M", "F"), + "arm" = c("placebo", "active", "") + ) + ) + ) |> + req_perform() + + response_patient_body <- + response_patient |> + resp_body_json() + + testthat::expect_equal(response_patient$status_code, 200) + checkmate::expect_number(response_patient_body$patient_id, lower = 1) +}) diff --git a/tests/testthat/test-api-audit-log.R b/tests/testthat/test-api-audit-log.R index 2ae767f..9d8e2de 100644 --- a/tests/testthat/test-api-audit-log.R +++ b/tests/testthat/test-api-audit-log.R @@ -21,7 +21,9 @@ testthat::test_that("audit logs for study are returned correctly from the databa testthat::expect_identical(response$status_code, 200L) testthat::expect_identical(length(response_body), count) - created_at <- response_body |> dplyr::bind_rows() |> dplyr::pull("created_at") + created_at <- response_body |> + dplyr::bind_rows() |> + dplyr::pull("created_at") testthat::expect_equal( created_at, created_at |> sort() From 1bbcce347cbd4084d53aecf58ce4e64065c81782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz?= Date: Tue, 26 Mar 2024 13:55:41 +0000 Subject: [PATCH 2/2] Remove unnecessary print statements in test-E2E-study-minimisation-pocock.R --- tests/testthat/test-E2E-study-minimisation-pocock.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/testthat/test-E2E-study-minimisation-pocock.R b/tests/testthat/test-E2E-study-minimisation-pocock.R index 517c0bf..08e72ab 100644 --- a/tests/testthat/test-E2E-study-minimisation-pocock.R +++ b/tests/testthat/test-E2E-study-minimisation-pocock.R @@ -524,9 +524,6 @@ test_that("randomization works for 1 patient", { response_patient |> resp_body_json() - print("######################") - print(response_patient_body) - print("######################") testthat::expect_equal(response_patient$status_code, 200) checkmate::expect_number(response_patient_body$patient_id, lower = 1) })