From 694affea16aea2bc5dba155f0f04f5505d4f1328 Mon Sep 17 00:00:00 2001 From: Kinga Date: Wed, 28 Feb 2024 07:35:07 +0000 Subject: [PATCH] Issue #70: Status code changed from 500 to 400. Added current_status tests. --- R/api_randomize.R | 38 +++++++++++++ .../test-E2E-study-minimisation-pocock.R | 55 ++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/R/api_randomize.R b/R/api_randomize.R index b4884ef..9b75817 100644 --- a/R/api_randomize.R +++ b/R/api_randomize.R @@ -61,6 +61,44 @@ api__randomize_patient <- function(study_id, current_state, req, res) { add = collection ) + browser() + + checkmate::assert( + checkmate::check_data_frame(current_state, + any.missing = TRUE, + all.missing = FALSE, nrows = 2, ncols = 3 + ), + .var.name = "current_state", + add = collection + ) + + checkmate::assert( + checkmate::check_names( + colnames(current_state), + must.include = "arm" + ), + .var.name = "current_state", + add = collection + ) + + + check_arm <- function(x) { + res <- checkmate::check_character( + current_state$arm[nrow(current_state)], + max.chars = 0 + ) + if (!isTRUE(res)) { + res <- ("Last value should be empty") + } + return(res) + } + + checkmate::assert( + check_arm(), + .var.name = "current_state[arm]", + add = collection + ) + if (length(collection$getMessages()) > 0) { res$status <- 400 return(list( diff --git a/tests/testthat/test-E2E-study-minimisation-pocock.R b/tests/testthat/test-E2E-study-minimisation-pocock.R index a821a7a..ef22b59 100644 --- a/tests/testthat/test-E2E-study-minimisation-pocock.R +++ b/tests/testthat/test-E2E-study-minimisation-pocock.R @@ -198,7 +198,7 @@ test_that("request with patient that is assigned an arm at entry", { response |> resp_body_json() - response_current_state <- + response_cs_arm <- tryCatch( { request(api_url) |> @@ -220,7 +220,58 @@ test_that("request with patient that is assigned an arm at entry", { ) testthat::expect_equal( - response_current_state$status, 500, + response_cs_arm$status, 400, + label = "HTTP status code" + ) + + response_cs_records <- + tryCatch( + { + request(api_url) |> + req_url_path("study", response_body$study$id, "patient") |> + req_method("POST") |> + req_body_json( + data = list( + current_state = + tibble::tibble( + "sex" = c("female"), + "weight" = c("61-80 kg"), + "arm" = c("placebo") + ) + ) + ) |> + req_perform() + }, + error = function(e) e + ) + + testthat::expect_equal( + response_cs_records$status, 400, + label = "HTTP status code" + ) + + response_current_state <- + tryCatch( + { + request(api_url) |> + req_url_path("study", response_body$study$id, "patient") |> + req_method("POST") |> + req_body_json( + data = list( + current_state = + tibble::tibble( + "sex" = c("female", "male"), + "weight" = c("61-80 kg", "81 kg or more") + ) + ) + ) |> + req_perform() + }, + error = function(e) e + ) + + testthat::expect_equal( + response_current_state$status, 400, label = "HTTP status code" ) })