(v3.0.1.9135) Fix get_mo_source() removing the mo_source file at the default location, returning the wrong mo_source, and throwing an error after the original file was moved, and run the unit tests in CI also in a real interactive session and with a user setup, a Dutch locale, and CLSI as default guideline, with tests that no longer assume an English locale or EUCAST

This commit is contained in:
dr. M.S. (Matthijs) Berends committed 2026-10-14 23:34:45 +02:00
1 parent 4be7352843
commit 8b01bcb23c
16 files changed
+360 -38

No files matched your search

+3
View File
@@ -44,10 +44,13 @@ test_that("test-ab_from_text.R", {
ab_from_text("28/03/2020 amoxicilliin 500mg po tds", thorough_search = FALSE)[[1]],
as.ab("Amoxicillin")
)
# translated names are in the language of the system, and are tested here in English
old_opts <- options(AMR_locale = "en")
expect_identical(
ab_from_text("28/03/2020 amoxicilliin 500mg po tds", translate_ab = TRUE)[[1]],
"Amoxicillin"
)
options(old_opts)
expect_identical(
ab_from_text("administered amoxi/clav and cipro", collapse = ", ")[[1]],
"AMC, CIP"
+2 -2
View File
@@ -75,7 +75,7 @@ test_that("test-ab_property.R", {
expect_true(ab_url("AMX") %like% "fhi[.]no")
expect_identical(
colnames(set_ab_names(example_isolates[, 17:22])),
colnames(set_ab_names(example_isolates[, 17:22], language = "en")),
c("cefoxitin", "cefotaxime", "ceftazidime", "ceftriaxone", "gentamicin", "tobramycin")
)
expect_identical(
@@ -97,7 +97,7 @@ test_that("test-ab_property.R", {
"moxifloxacin", "vancomycin", "TEC"
) %in%
(example_isolates %>%
set_ab_names(NIT:VAN) %>%
set_ab_names(NIT:VAN, language = "en") %>%
colnames())))
}
})
+16 -8
View File
@@ -35,21 +35,25 @@ test_that("test-antibiogram.R", {
ab0 <- antibiogram(example_isolates)
# the column names of ab1 to ab6 and ab8 to ab9 are tested in English, which must also hold on systems in another language
ab1 <- antibiogram(example_isolates,
antimicrobials = c(aminoglycosides(), carbapenems())
antimicrobials = c(aminoglycosides(), carbapenems()),
language = "en"
)
ab2 <- antibiogram(example_isolates,
antimicrobials = aminoglycosides(),
ab_transform = "atc",
mo_transform = "gramstain"
mo_transform = "gramstain",
language = "en"
)
ab3 <- antibiogram(example_isolates,
antimicrobials = carbapenems(),
ab_transform = "ab",
mo_transform = "name",
formatting_type = 1
formatting_type = 1,
language = "en"
)
expect_inherits(ab1, "antibiogram")
@@ -65,7 +69,8 @@ test_that("test-antibiogram.R", {
# combined antibiogram yield higher empiric coverage
ab4 <- antibiogram(example_isolates,
antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN"),
mo_transform = "gramstain"
mo_transform = "gramstain",
language = "en"
)
ab5 <- antibiogram(example_isolates,
@@ -73,7 +78,8 @@ test_that("test-antibiogram.R", {
mo_transform = "gramstain",
ab_transform = "name",
sep = " & ",
add_total_n = FALSE
add_total_n = FALSE,
language = "en"
)
expect_inherits(ab4, "antibiogram")
@@ -87,7 +93,8 @@ test_that("test-antibiogram.R", {
ab6 <- antibiogram(example_isolates,
antimicrobials = c(aminoglycosides(), carbapenems()),
syndromic_group = "ward",
ab_transform = NULL
ab_transform = NULL,
language = "en"
)
# with a custom language, though this will be determined automatically
@@ -112,7 +119,8 @@ test_that("test-antibiogram.R", {
# the data set could contain a filter for e.g. respiratory specimens
ab8 <- suppressWarnings(antibiogram(example_isolates,
antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN"),
wisca = TRUE
wisca = TRUE,
language = "en"
))
expect_inherits(ab8, "antibiogram")
@@ -125,7 +133,7 @@ test_that("test-antibiogram.R", {
expect_warning(
ab9 <- example_isolates %>%
group_by(ward, gender) %>%
wisca(antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN"))
wisca(antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN"), language = "en")
)
expect_equal(colnames(ab9), c("ward", "gender", "Piperacillin/tazobactam", "Piperacillin/tazobactam + Gentamicin", "Piperacillin/tazobactam + Tobramycin"))
}
+12 -11
View File
@@ -30,8 +30,9 @@
test_that("test-count.R", {
skip_on_cran()
expect_equal(count_R(example_isolates$AMX), count_resistant(example_isolates$AMX))
expect_equal(count_SI(example_isolates$AMX), count_susceptible(example_isolates$AMX))
# the counts in this file are based on EUCAST, which must also hold when another guideline is set with the AMR_guideline option
expect_equal(count_R(example_isolates$AMX), count_resistant(example_isolates$AMX, guideline = "EUCAST"))
expect_equal(count_SI(example_isolates$AMX), count_susceptible(example_isolates$AMX, guideline = "EUCAST"))
expect_equal(count_IR(example_isolates$AMX), count_resistant(example_isolates$AMX, guideline = "CLSI"))
expect_equal(count_S(example_isolates$AMX), count_susceptible(example_isolates$AMX, guideline = "CLSI"))
expect_equal(count_all(example_isolates$AMX), n_sir(example_isolates$AMX))
@@ -64,9 +65,9 @@ test_that("test-count.R", {
expect_error(count_df(example_isolates[, "date", drop = TRUE]))
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
expect_equal(example_isolates %>% count_susceptible(AMC), 1433)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE), 1687)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = FALSE), 1764)
expect_equal(example_isolates %>% count_susceptible(AMC, guideline = "EUCAST"), 1433)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE, guideline = "EUCAST"), 1687)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = FALSE, guideline = "EUCAST"), 1764)
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE), 1798)
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = FALSE), 1936)
expect_identical(
@@ -80,20 +81,20 @@ test_that("test-count.R", {
example_isolates %>%
group_by(ward) %>%
summarise(
cipro = count_susceptible(CIP),
genta = count_susceptible(GEN),
combination = count_susceptible(CIP, GEN)
cipro = count_susceptible(CIP, guideline = "EUCAST"),
genta = count_susceptible(GEN, guideline = "EUCAST"),
combination = count_susceptible(CIP, GEN, guideline = "EUCAST")
) %>%
pull(combination),
c(946, 428, 94)
)
# count_df
# count_df, which always combines S and I, as with EUCAST
expect_equal(
example_isolates %>% select(AMX) %>% count_df() %>% pull(value),
c(
example_isolates$AMX %>% count_susceptible(),
example_isolates$AMX %>% count_resistant()
example_isolates$AMX %>% count_susceptible(guideline = "EUCAST"),
example_isolates$AMX %>% count_resistant(guideline = "EUCAST")
)
)
expect_equal(
+3
View File
@@ -29,6 +29,9 @@
test_that("test-interpretive_rules.R", {
skip_on_cran()
# these tests are based on EUCAST rules, which must also hold when another guideline is set with the AMR_guideline option
old_opts <- options(AMR_guideline = "EUCAST")
on.exit(options(old_opts), add = TRUE)
# thoroughly check input table
expect_equal(
+88
View File
@@ -0,0 +1,88 @@
# ==================================================================== #
# TITLE: #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
# https://github.com/msberends/AMR #
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
# https://doi.org/10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen and the University Medical #
# Center Groningen in the Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://amr-for-r.org #
# ==================================================================== #
test_that("test-mo_source.R", {
# set_mo_source() and the interactive branches of get_mo_source() only work in interactive sessions, see .github/scripts/run_tests.sh
skip_if_not(interactive())
skip_on_cran()
dir <- tempfile("mo_source_test")
dir.create(dir)
src_a <- file.path(dir, "source_a.csv")
src_b <- file.path(dir, "source_b.csv")
dest_a <- file.path(dir, "dest_a.rds")
dest_b <- file.path(dir, "dest_b.rds")
write.csv(data.frame(lab_code = "TESTCODE_A", mo = "B_ESCHR_COLI"), src_a, row.names = FALSE)
write.csv(data.frame(lab_code = "TESTCODE_B", mo = "B_STPHY_AURS"), src_b, row.names = FALSE)
# existing destination files are updated without asking for permission, so these tests never prompt
saveRDS(NULL, dest_a)
saveRDS(NULL, dest_b)
amr_env <- AMR:::AMR_env
mo_source_bak <- amr_env$mo_source
on.exit(amr_env$mo_source <- mo_source_bak, add = TRUE)
suppressMessages(set_mo_source(src_b, destination = dest_b))
suppressMessages(set_mo_source(src_a, destination = dest_a))
expect_identical(get_mo_source(destination = dest_a)$lab_code, "TESTCODE_A")
# another destination returns its own mo_source, not the one already loaded
expect_identical(get_mo_source(destination = dest_b)$lab_code, "TESTCODE_B")
expect_identical(as.character(as.mo("TESTCODE_B", reference_df = get_mo_source(destination = dest_b), info = FALSE)), "B_STPHY_AURS")
# a destination that does not exist removes no other mo_source file, nor the loaded mo_source or the cache
suppressMessages(get_mo_source(destination = dest_a))
suppressWarnings(as.mo("Klebsiela pneumonia", info = FALSE))
# as.mo() uses the mo_source at the default destination, which can exist on the system that runs these tests
loaded_destination <- attributes(amr_env$mo_source)$mo_source_destination
n_cached <- NROW(amr_env$mo_previously_coerced)
expect_null(get_mo_source(destination = file.path(dir, "does_not_exist.rds")))
expect_true(file.exists(dest_a))
expect_true(file.exists(dest_b))
expect_identical(attributes(amr_env$mo_source)$mo_source_destination, loaded_destination)
expect_identical(NROW(amr_env$mo_previously_coerced), n_cached)
# an updated original file updates the mo_source file at the same destination
write.csv(data.frame(lab_code = "TESTCODE_A2", mo = "B_ESCHR_COLI"), src_a, row.names = FALSE)
Sys.setFileTime(src_a, Sys.time() + 60)
expect_identical(suppressMessages(get_mo_source(destination = dest_a))$lab_code, "TESTCODE_A2")
expect_true(file.exists(dest_b))
# a deleted mo_source file removes the reference to it
unlink(dest_a)
expect_null(get_mo_source(destination = dest_a))
expect_null(amr_env$mo_source)
expect_true(file.exists(dest_b))
# a moved or deleted original file removes the mo_source file, as documented, without an error
suppressMessages(get_mo_source(destination = dest_b))
unlink(src_b)
expect_null(suppressMessages(get_mo_source(destination = dest_b)))
expect_false(file.exists(dest_b))
unlink(dir, recursive = TRUE)
})
+3 -2
View File
@@ -30,8 +30,9 @@
test_that("test-proportion.R", {
skip_on_cran()
expect_equal(proportion_R(example_isolates$AMX), resistance(example_isolates$AMX))
expect_equal(proportion_SI(example_isolates$AMX), susceptibility(example_isolates$AMX))
# with EUCAST, which must also hold when another guideline is set with the AMR_guideline option
expect_equal(proportion_R(example_isolates$AMX), resistance(example_isolates$AMX, guideline = "EUCAST"))
expect_equal(proportion_SI(example_isolates$AMX), susceptibility(example_isolates$AMX, guideline = "EUCAST"))
expect_equal(proportion_IR(example_isolates$AMX), resistance(example_isolates$AMX, guideline = "CLSI"))
expect_equal(proportion_S(example_isolates$AMX), susceptibility(example_isolates$AMX, guideline = "CLSI"))
# AMX resistance in `example_isolates`