diff --git a/R/alert_functions.R b/R/alert_functions.R index ebce96f..0f6d60e 100644 --- a/R/alert_functions.R +++ b/R/alert_functions.R @@ -349,6 +349,14 @@ pipe_infodengue <- function(cities, cid10="A90", datarelatorio, finalday = NULL, .as_legacy_alert_objects <- function(result) { data <- result$data + diseases <- unique(c(as.character(data$CID10), + as.character(result$parameters$cid10))) + if (!"CID10" %in% names(data) || + !"cid10" %in% names(result$parameters) || + length(diseases) != 1L || anyNA(diseases) || any(!nzchar(diseases))) { + stop("The legacy adapter requires exactly one disease in data and parameters.", + call. = FALSE) + } groups <- split(seq_len(nrow(data)), factor(data$cidade, levels = unique(data$cidade))) legacy <- lapply(names(groups), function(city) { diff --git a/tests/testthat/test-legacy-disease-guard.R b/tests/testthat/test-legacy-disease-guard.R new file mode 100644 index 0000000..77cd0e8 --- /dev/null +++ b/tests/testthat/test-legacy-disease-guard.R @@ -0,0 +1,10 @@ +test_that("legacy adapter rejects mixed or inconsistent disease results", { + result <- review_result() + mixed <- result + mixed$data$CID10[1] <- "A92.0" + expect_error(AlertTools:::.as_legacy_alert_objects(mixed), "exactly one disease") + inconsistent <- result + inconsistent$parameters$cid10 <- "A92.0" + expect_error(AlertTools:::.as_legacy_alert_objects(inconsistent), "exactly one disease") + expect_s3_class(AlertTools:::.as_legacy_alert_objects(result)[[1]], "alerta") +})