From 2f52bf39c353cddea5380f4f0c6a822b74ebf4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wa=C5=82ejko?= Date: Mon, 26 Feb 2024 09:26:55 +0000 Subject: [PATCH] tests and upstream integration --- R/api_get_randomization_list.R | 10 +++------- R/audit-trail.R | 10 ++++++---- inst/plumber/unbiased_api/study.R | 2 +- tests/testthat/test-E2E-study-minimisation-pocock.R | 2 +- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/R/api_get_randomization_list.R b/R/api_get_randomization_list.R index 2babe95..ab97ea8 100644 --- a/R/api_get_randomization_list.R +++ b/R/api_get_randomization_list.R @@ -1,15 +1,10 @@ api_get_rand_list <- function(study_id, req, res) { + audit_log_event_type("get_rand_list", req) 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 - ) + is_study <- check_study_exist(study_id = study_id) if (!is_study) { res$status <- 404 @@ -17,6 +12,7 @@ api_get_rand_list <- function(study_id, req, res) { error = "Study not found" )) } + audit_log_study_id(study_id, req) patients <- dplyr::tbl(db_connection_pool, "patient") |> diff --git a/R/audit-trail.R b/R/audit-trail.R index b31015c..a063ec5 100644 --- a/R/audit-trail.R +++ b/R/audit-trail.R @@ -23,9 +23,9 @@ AuditLog <- R6::R6Class( # nolint: object_name_linter. private$request_body <- request_body }, set_response_body = function(response_body) { - if (typeof(response_body) == "list") { - response_body <- jsonlite::toJSON(response_body, auto_unbox = TRUE) |> as.character() - } + checkmate::assert_false( + typeof(response_body) == "list" + ) private$response_body <- response_body }, set_event_type = function(event_type) { @@ -149,7 +149,9 @@ setup_audit_trail <- function(pr, endpoints = list()) { audit_log$set_request_body(req$body) audit_log$set_response_body(res$body) - if (audit_log$validate_log()) { + log_valid <- audit_log$validate_log() + + if (log_valid) { audit_log$persist() } }) diff --git a/inst/plumber/unbiased_api/study.R b/inst/plumber/unbiased_api/study.R index 4518030..bc5c3a3 100644 --- a/inst/plumber/unbiased_api/study.R +++ b/inst/plumber/unbiased_api/study.R @@ -103,7 +103,7 @@ unbiased:::wrap_endpoint(function(study_id, req, res) { #* @serializer unboxedJSON #* -sentryR::with_captured_calls(function(study_id, req, res) { +unbiased:::wrap_endpoint(function(study_id, req, res) { return( unbiased:::api_get_rand_list(study_id, req, res) ) diff --git a/tests/testthat/test-E2E-study-minimisation-pocock.R b/tests/testthat/test-E2E-study-minimisation-pocock.R index 13ef083..31394e3 100644 --- a/tests/testthat/test-E2E-study-minimisation-pocock.R +++ b/tests/testthat/test-E2E-study-minimisation-pocock.R @@ -1,7 +1,7 @@ test_that("correct request with the structure of the returned result", { source("./test-helpers.R") source("./audit-log-test-helpers.R") - with_db_fixtures("fixtures/example_study.yml") + with_db_fixtures("fixtures/example_db.yml") assert_audit_trail_for_test(c( "study_create", "randomize_patient"