mirror of
https://github.com/msberends/AMR.git
synced 2025-07-08 18:41:58 +02:00
(v1.6.0.9023) new unit test flow
This commit is contained in:
@ -26,8 +26,7 @@
|
||||
# the testthat package is in Suggests, but very old R versions will not be
|
||||
# able to install it. Yet, we want basic R CMD CHECK's in those R versions
|
||||
# as well, so only run unit tests in later R versions:
|
||||
if (require("testthat")) {
|
||||
library(testthat, warn.conflicts = FALSE)
|
||||
if (require("testthat", warn.conflicts = FALSE)) {
|
||||
library(AMR)
|
||||
test_check("AMR")
|
||||
}
|
||||
|
@ -57,9 +57,10 @@ test_that("looking up ab columns works", {
|
||||
|
||||
test_that("looking up ab columns works", {
|
||||
skip_on_cran()
|
||||
library(dplyr)
|
||||
|
||||
# we rely on "grouped_tbl" being a class of grouped tibbles, so:
|
||||
expect_true(is_null_or_grouped_tbl(example_isolates %>% group_by(hospital_id)))
|
||||
|
||||
# we rely on "grouped_tbl" being a class of grouped tibbles, so implement a test that checks for this:
|
||||
if (require("dplyr")) {
|
||||
expect_true(is_null_or_grouped_tbl(example_isolates %>% group_by(hospital_id)))
|
||||
}
|
||||
|
||||
})
|
||||
|
@ -41,42 +41,6 @@ test_that("counts work", {
|
||||
expect_equal(suppressWarnings(count_S(example_isolates$AMX)) + count_I(example_isolates$AMX),
|
||||
count_SI(example_isolates$AMX))
|
||||
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
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_all(AMC, GEN, only_all_tested = TRUE), 1798)
|
||||
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = FALSE), 1936)
|
||||
expect_identical(example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE),
|
||||
example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE) +
|
||||
example_isolates %>% count_resistant(AMC, GEN, only_all_tested = TRUE))
|
||||
|
||||
# count of cases
|
||||
expect_equal(example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
summarise(cipro = count_susceptible(CIP),
|
||||
genta = count_susceptible(GEN),
|
||||
combination = count_susceptible(CIP, GEN)) %>%
|
||||
pull(combination),
|
||||
c(253, 465, 192, 558))
|
||||
|
||||
# count_df
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% count_df() %>% pull(value),
|
||||
c(example_isolates$AMX %>% count_susceptible(),
|
||||
example_isolates$AMX %>% count_resistant())
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% count_df(combine_IR = TRUE) %>% pull(value),
|
||||
c(suppressWarnings(example_isolates$AMX %>% count_S()),
|
||||
suppressWarnings(example_isolates$AMX %>% count_IR()))
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% count_df(combine_SI = FALSE) %>% pull(value),
|
||||
c(suppressWarnings(example_isolates$AMX %>% count_S()),
|
||||
example_isolates$AMX %>% count_I(),
|
||||
example_isolates$AMX %>% count_R())
|
||||
)
|
||||
|
||||
# warning for speed loss
|
||||
reset_all_thrown_messages()
|
||||
@ -94,11 +58,49 @@ test_that("counts work", {
|
||||
expect_error(count_df(c("A", "B", "C")))
|
||||
expect_error(count_df(example_isolates[, "date"]))
|
||||
|
||||
# grouping in rsi_calc_df() (= backbone of rsi_df())
|
||||
expect_true("hospital_id" %in% (example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
select(hospital_id, AMX, CIP, gender) %>%
|
||||
rsi_df() %>%
|
||||
colnames()))
|
||||
if (require("dplyr")) {
|
||||
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_all(AMC, GEN, only_all_tested = TRUE), 1798)
|
||||
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = FALSE), 1936)
|
||||
expect_identical(example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE),
|
||||
example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE) +
|
||||
example_isolates %>% count_resistant(AMC, GEN, only_all_tested = TRUE))
|
||||
|
||||
# count of cases
|
||||
expect_equal(example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
summarise(cipro = count_susceptible(CIP),
|
||||
genta = count_susceptible(GEN),
|
||||
combination = count_susceptible(CIP, GEN)) %>%
|
||||
pull(combination),
|
||||
c(253, 465, 192, 558))
|
||||
|
||||
# count_df
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% count_df() %>% pull(value),
|
||||
c(example_isolates$AMX %>% count_susceptible(),
|
||||
example_isolates$AMX %>% count_resistant())
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% count_df(combine_IR = TRUE) %>% pull(value),
|
||||
c(suppressWarnings(example_isolates$AMX %>% count_S()),
|
||||
suppressWarnings(example_isolates$AMX %>% count_IR()))
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% count_df(combine_SI = FALSE) %>% pull(value),
|
||||
c(suppressWarnings(example_isolates$AMX %>% count_S()),
|
||||
example_isolates$AMX %>% count_I(),
|
||||
example_isolates$AMX %>% count_R())
|
||||
)
|
||||
|
||||
# grouping in rsi_calc_df() (= backbone of rsi_df())
|
||||
expect_true("hospital_id" %in% (example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
select(hospital_id, AMX, CIP, gender) %>%
|
||||
rsi_df() %>%
|
||||
colnames()))
|
||||
}
|
||||
|
||||
})
|
||||
|
@ -54,7 +54,8 @@ test_that("disk works", {
|
||||
}
|
||||
expect_output(print(as.disk(12)))
|
||||
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
expect_output(print(tibble(d = as.disk(12))))
|
||||
if (require("dplyr")) {
|
||||
expect_output(print(tibble(d = as.disk(12))))
|
||||
}
|
||||
|
||||
})
|
||||
|
@ -41,18 +41,18 @@ test_that("episodes work", {
|
||||
expect_equal(get_episode(test_df$date, 365),
|
||||
c(1, 1, 2, 2, 2, 3, 3, 4, 1, 2, 2, 2, 3))
|
||||
|
||||
library(dplyr)
|
||||
expect_identical(test_df %>% group_by(patient_id) %>% mutate(f = is_new_episode(date, 365)) %>% pull(f),
|
||||
c(TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE))
|
||||
|
||||
suppressMessages(
|
||||
x <- example_isolates %>%
|
||||
mutate(out = first_isolate(., include_unknown = TRUE, method = "episode-based", info = FALSE))
|
||||
)
|
||||
|
||||
y <- example_isolates %>%
|
||||
group_by(patient_id, mo) %>%
|
||||
mutate(out = is_new_episode(date, 365))
|
||||
|
||||
expect_identical(which(x$out), which(y$out))
|
||||
if (require("dplyr")) {
|
||||
expect_identical(test_df %>% group_by(patient_id) %>% mutate(f = is_new_episode(date, 365)) %>% pull(f),
|
||||
c(TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE))
|
||||
|
||||
suppressMessages(
|
||||
x <- example_isolates %>%
|
||||
mutate(out = first_isolate(., include_unknown = TRUE, method = "episode-based", info = FALSE))
|
||||
)
|
||||
y <- example_isolates %>%
|
||||
group_by(patient_id, mo) %>%
|
||||
mutate(out = is_new_episode(date, 365))
|
||||
|
||||
expect_identical(which(x$out), which(y$out))
|
||||
}
|
||||
})
|
||||
|
@ -28,26 +28,27 @@ context("filter_ab_class.R")
|
||||
test_that("ATC-group filtering works", {
|
||||
skip_on_cran()
|
||||
|
||||
library(dplyr)
|
||||
expect_gt(example_isolates %>% filter_ab_class("carbapenem") %>% nrow(), 0)
|
||||
expect_gt(example_isolates %>% filter_aminoglycosides() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_carbapenems() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_1st_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_2nd_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_3rd_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_4th_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_5th_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_fluoroquinolones() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_glycopeptides() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_macrolides() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_oxazolidinones() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_penicillins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_tetracyclines() %>% ncol(), 0)
|
||||
|
||||
expect_gt(example_isolates %>% filter_carbapenems("R", "all") %>% nrow(), 0)
|
||||
|
||||
expect_error(example_isolates %>% filter_carbapenems(result = "test"))
|
||||
expect_error(example_isolates %>% filter_carbapenems(scope = "test"))
|
||||
expect_message(example_isolates %>% select(1:3) %>% filter_carbapenems())
|
||||
if (require("dplyr")) {
|
||||
expect_gt(example_isolates %>% filter_ab_class("carbapenem") %>% nrow(), 0)
|
||||
expect_gt(example_isolates %>% filter_aminoglycosides() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_carbapenems() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_1st_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_2nd_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_3rd_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_4th_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_5th_cephalosporins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_fluoroquinolones() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_glycopeptides() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_macrolides() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_oxazolidinones() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_penicillins() %>% ncol(), 0)
|
||||
expect_gt(example_isolates %>% filter_tetracyclines() %>% ncol(), 0)
|
||||
|
||||
expect_gt(example_isolates %>% filter_carbapenems("R", "all") %>% nrow(), 0)
|
||||
|
||||
expect_error(example_isolates %>% filter_carbapenems(result = "test"))
|
||||
expect_error(example_isolates %>% filter_carbapenems(scope = "test"))
|
||||
expect_message(example_isolates %>% select(1:3) %>% filter_carbapenems())
|
||||
}
|
||||
})
|
||||
|
@ -26,64 +26,65 @@
|
||||
context("ggplot_rsi.R")
|
||||
|
||||
test_that("ggplot_rsi works", {
|
||||
|
||||
|
||||
skip_on_cran()
|
||||
|
||||
skip_if_not_installed("ggplot2")
|
||||
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
library(ggplot2)
|
||||
|
||||
pdf(NULL) # prevent Rplots.pdf being created
|
||||
|
||||
# data should be equal
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi())$data %>% summarise_all(resistance) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
|
||||
)
|
||||
|
||||
print(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "interpretation", facet = "antibiotic"))
|
||||
print(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))
|
||||
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "interpretation", facet = "antibiotic"))$data %>% summarise_all(resistance) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
|
||||
)
|
||||
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(resistance) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
|
||||
)
|
||||
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(count_resistant) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(count_resistant) %>% as.double()
|
||||
)
|
||||
|
||||
# support for scale_type ab and mo
|
||||
expect_equal(class((data.frame(mo = as.mo(c("e. coli", "s aureus")),
|
||||
n = c(40, 100)) %>%
|
||||
ggplot(aes(x = mo, y = n)) +
|
||||
geom_col())$data),
|
||||
"data.frame")
|
||||
expect_equal(class((data.frame(ab = as.ab(c("amx", "amc")),
|
||||
n = c(40, 100)) %>%
|
||||
ggplot(aes(x = ab, y = n)) +
|
||||
geom_col())$data),
|
||||
"data.frame")
|
||||
|
||||
expect_equal(class((data.frame(ab = as.ab(c("amx", "amc")),
|
||||
n = c(40, 100)) %>%
|
||||
ggplot(aes(x = ab, y = n)) +
|
||||
geom_col())$data),
|
||||
"data.frame")
|
||||
|
||||
# support for manual colours
|
||||
expect_equal(class((ggplot(data.frame(x = c("Value1", "Value2", "Value3"),
|
||||
y = c(1, 2, 3),
|
||||
z = c("Value4", "Value5", "Value6"))) +
|
||||
geom_col(aes(x = x, y = y, fill = z)) +
|
||||
scale_rsi_colours(Value4 = "S", Value5 = "I", Value6 = "R"))$data),
|
||||
"data.frame")
|
||||
skip_if_not_installed("dplyr")
|
||||
|
||||
if (require("dplyr") & require("ggplot2")) {
|
||||
|
||||
pdf(NULL) # prevent Rplots.pdf being created
|
||||
|
||||
# data should be equal
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi())$data %>% summarise_all(resistance) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
|
||||
)
|
||||
|
||||
print(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "interpretation", facet = "antibiotic"))
|
||||
print(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))
|
||||
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "interpretation", facet = "antibiotic"))$data %>% summarise_all(resistance) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
|
||||
)
|
||||
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(resistance) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
|
||||
)
|
||||
|
||||
expect_equal(
|
||||
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(count_resistant) %>% as.double(),
|
||||
example_isolates %>% select(AMC, CIP) %>% summarise_all(count_resistant) %>% as.double()
|
||||
)
|
||||
|
||||
# support for scale_type ab and mo
|
||||
expect_equal(class((data.frame(mo = as.mo(c("e. coli", "s aureus")),
|
||||
n = c(40, 100)) %>%
|
||||
ggplot(aes(x = mo, y = n)) +
|
||||
geom_col())$data),
|
||||
"data.frame")
|
||||
expect_equal(class((data.frame(ab = as.ab(c("amx", "amc")),
|
||||
n = c(40, 100)) %>%
|
||||
ggplot(aes(x = ab, y = n)) +
|
||||
geom_col())$data),
|
||||
"data.frame")
|
||||
|
||||
expect_equal(class((data.frame(ab = as.ab(c("amx", "amc")),
|
||||
n = c(40, 100)) %>%
|
||||
ggplot(aes(x = ab, y = n)) +
|
||||
geom_col())$data),
|
||||
"data.frame")
|
||||
|
||||
# support for manual colours
|
||||
expect_equal(class((ggplot(data.frame(x = c("Value1", "Value2", "Value3"),
|
||||
y = c(1, 2, 3),
|
||||
z = c("Value4", "Value5", "Value6"))) +
|
||||
geom_col(aes(x = x, y = y, fill = z)) +
|
||||
scale_rsi_colours(Value4 = "S", Value5 = "I", Value6 = "R"))$data),
|
||||
"data.frame")
|
||||
|
||||
}
|
||||
})
|
||||
|
@ -37,6 +37,5 @@ test_that("key_antimcrobials work", {
|
||||
expect_true(antimicrobials_equal(".SS", "SI.", ignore_I = TRUE, type = "keyantimicrobials"))
|
||||
expect_false(antimicrobials_equal(".SS", "SI.", ignore_I = FALSE, type = "keyantimicrobials"))
|
||||
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
expect_warning(key_antimicrobials(example_isolates %>% slice(rep(1, 10))))
|
||||
expect_warning(key_antimicrobials(example_isolates[rep(1, 10), ]))
|
||||
})
|
||||
|
@ -44,7 +44,6 @@ test_that("mdro works", {
|
||||
# check class
|
||||
expect_equal(class(outcome), c("ordered", "factor"))
|
||||
|
||||
library(dplyr)
|
||||
# example_isolates should have these finding using Dutch guidelines
|
||||
expect_equal(as.double(table(outcome)),
|
||||
c(1970, 24, 6)) # 1970 neg, 24 unconfirmed, 6 pos
|
||||
@ -245,8 +244,8 @@ test_that("mdro works", {
|
||||
info = FALSE))
|
||||
|
||||
# print groups
|
||||
library(dplyr)
|
||||
expect_output(x <- mdro(example_isolates %>% group_by(hospital_id), info = TRUE))
|
||||
expect_output(x <- mdro(example_isolates %>% group_by(hospital_id), guideline = custom, info = TRUE))
|
||||
|
||||
if (require("dplyr")) {
|
||||
expect_output(x <- mdro(example_isolates %>% group_by(hospital_id), info = TRUE))
|
||||
expect_output(x <- mdro(example_isolates %>% group_by(hospital_id), guideline = custom, info = TRUE))
|
||||
}
|
||||
})
|
||||
|
@ -62,8 +62,9 @@ test_that("mic works", {
|
||||
|
||||
expect_s3_class(summary(as.mic(c(2, 8))), c("summaryDefault", "table"))
|
||||
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
expect_output(print(tibble(m = as.mic(2:4))))
|
||||
if (require("dplyr")) {
|
||||
expect_output(print(tibble(m = as.mic(2:4))))
|
||||
}
|
||||
})
|
||||
|
||||
test_that("mathematical functions on mic work", {
|
||||
|
@ -28,10 +28,8 @@ context("mo.R")
|
||||
test_that("as.mo works", {
|
||||
|
||||
skip_on_cran()
|
||||
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
|
||||
MOs <- microorganisms %>% filter(!is.na(mo), nchar(mo) > 3)
|
||||
|
||||
MOs <- subset(microorganisms, !is.na(mo) & nchar(mo) > 3)
|
||||
expect_identical(as.character(MOs$mo), as.character(as.mo(MOs$mo)))
|
||||
|
||||
expect_identical(
|
||||
@ -152,37 +150,43 @@ test_that("as.mo works", {
|
||||
expect_identical(as.character(as.mo("S. salivarius", Lancefield = FALSE)), "B_STRPT_SLVR")
|
||||
expect_identical(as.character(as.mo("S. salivarius", Lancefield = TRUE)), "B_STRPT_GRPK") # group K
|
||||
|
||||
# select with one column
|
||||
expect_identical(
|
||||
example_isolates[1:10, ] %>%
|
||||
left_join_microorganisms() %>%
|
||||
select(genus) %>%
|
||||
as.mo() %>%
|
||||
as.character(),
|
||||
c("B_ESCHR", "B_ESCHR", "B_STPHY", "B_STPHY", "B_STPHY",
|
||||
"B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY"))
|
||||
|
||||
# select with two columns
|
||||
expect_identical(
|
||||
example_isolates[1:10, ] %>%
|
||||
pull(mo),
|
||||
example_isolates[1:10, ] %>%
|
||||
left_join_microorganisms() %>%
|
||||
select(genus, species) %>%
|
||||
as.mo())
|
||||
if (require("dplyr")) {
|
||||
# select with one column
|
||||
expect_identical(
|
||||
example_isolates[1:10, ] %>%
|
||||
left_join_microorganisms() %>%
|
||||
select(genus) %>%
|
||||
as.mo() %>%
|
||||
as.character(),
|
||||
c("B_ESCHR", "B_ESCHR", "B_STPHY", "B_STPHY", "B_STPHY",
|
||||
"B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY"))
|
||||
|
||||
# select with two columns
|
||||
expect_identical(
|
||||
example_isolates[1:10, ] %>%
|
||||
pull(mo),
|
||||
example_isolates[1:10, ] %>%
|
||||
left_join_microorganisms() %>%
|
||||
select(genus, species) %>%
|
||||
as.mo())
|
||||
|
||||
# too many columns
|
||||
expect_error(example_isolates %>% select(1:3) %>% as.mo())
|
||||
|
||||
# test pull
|
||||
expect_equal(nrow(example_isolates %>% mutate(mo = as.mo(mo))),
|
||||
2000)
|
||||
expect_true(example_isolates %>% pull(mo) %>% is.mo())
|
||||
}
|
||||
|
||||
# unknown results
|
||||
expect_warning(as.mo(c("INVALID", "Yeah, unknown")))
|
||||
|
||||
# too many columns
|
||||
expect_error(example_isolates %>% select(1:3) %>% as.mo())
|
||||
|
||||
|
||||
# print
|
||||
expect_output(print(as.mo(c("B_ESCHR_COLI", NA))))
|
||||
|
||||
# test pull
|
||||
expect_equal(nrow(example_isolates %>% mutate(mo = as.mo(mo))),
|
||||
2000)
|
||||
|
||||
|
||||
# test data.frame
|
||||
expect_equal(nrow(data.frame(test = as.mo("B_ESCHR_COLI"))),
|
||||
@ -265,7 +269,6 @@ test_that("as.mo works", {
|
||||
rep("UNKNOWN", 3))
|
||||
|
||||
expect_null(mo_failures())
|
||||
expect_true(example_isolates %>% pull(mo) %>% is.mo())
|
||||
|
||||
expect_error(translate_allow_uncertain(5))
|
||||
|
||||
|
@ -131,11 +131,12 @@ test_that("mo_property works", {
|
||||
expect_equal(mo_name("test", reference_df = data.frame(col1 = "test", mo = "B_ESCHR_COLI")),
|
||||
"Escherichia coli")
|
||||
|
||||
library(dplyr)
|
||||
expect_equal(example_isolates %>% filter(mo_is_gram_negative()) %>% nrow(),
|
||||
730)
|
||||
expect_equal(example_isolates %>% filter(mo_is_gram_positive()) %>% nrow(),
|
||||
1238)
|
||||
expect_equal(example_isolates %>% filter(mo_is_intrinsic_resistant(ab = "Vancomycin")) %>% nrow(),
|
||||
710)
|
||||
if (require("dplyr")) {
|
||||
expect_equal(example_isolates %>% filter(mo_is_gram_negative()) %>% nrow(),
|
||||
730)
|
||||
expect_equal(example_isolates %>% filter(mo_is_gram_positive()) %>% nrow(),
|
||||
1238)
|
||||
expect_equal(example_isolates %>% filter(mo_is_intrinsic_resistant(ab = "Vancomycin")) %>% nrow(),
|
||||
710)
|
||||
}
|
||||
})
|
||||
|
@ -52,30 +52,49 @@ test_that("proportions works", {
|
||||
0.9382647,
|
||||
tolerance = 0.0001)
|
||||
|
||||
library(dplyr)
|
||||
# percentages
|
||||
expect_equal(example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
summarise(R = proportion_R(CIP, as_percent = TRUE),
|
||||
I = proportion_I(CIP, as_percent = TRUE),
|
||||
S = proportion_S(CIP, as_percent = TRUE),
|
||||
n = n_rsi(CIP),
|
||||
total = n()) %>%
|
||||
pull(n) %>%
|
||||
sum(),
|
||||
1409)
|
||||
|
||||
# count of cases
|
||||
expect_equal(example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
summarise(cipro_p = proportion_SI(CIP, as_percent = TRUE),
|
||||
cipro_n = n_rsi(CIP),
|
||||
genta_p = proportion_SI(GEN, as_percent = TRUE),
|
||||
genta_n = n_rsi(GEN),
|
||||
combination_p = proportion_SI(CIP, GEN, as_percent = TRUE),
|
||||
combination_n = n_rsi(CIP, GEN)) %>%
|
||||
pull(combination_n),
|
||||
c(305, 617, 241, 711))
|
||||
if (require("dplyr")) {
|
||||
# percentages
|
||||
expect_equal(example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
summarise(R = proportion_R(CIP, as_percent = TRUE),
|
||||
I = proportion_I(CIP, as_percent = TRUE),
|
||||
S = proportion_S(CIP, as_percent = TRUE),
|
||||
n = n_rsi(CIP),
|
||||
total = n()) %>%
|
||||
pull(n) %>%
|
||||
sum(),
|
||||
1409)
|
||||
|
||||
# count of cases
|
||||
expect_equal(example_isolates %>%
|
||||
group_by(hospital_id) %>%
|
||||
summarise(cipro_p = proportion_SI(CIP, as_percent = TRUE),
|
||||
cipro_n = n_rsi(CIP),
|
||||
genta_p = proportion_SI(GEN, as_percent = TRUE),
|
||||
genta_n = n_rsi(GEN),
|
||||
combination_p = proportion_SI(CIP, GEN, as_percent = TRUE),
|
||||
combination_n = n_rsi(CIP, GEN)) %>%
|
||||
pull(combination_n),
|
||||
c(305, 617, 241, 711))
|
||||
|
||||
# proportion_df
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% proportion_df() %>% pull(value),
|
||||
c(example_isolates$AMX %>% proportion_SI(),
|
||||
example_isolates$AMX %>% proportion_R())
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% proportion_df(combine_IR = TRUE) %>% pull(value),
|
||||
c(example_isolates$AMX %>% proportion_S(),
|
||||
example_isolates$AMX %>% proportion_IR())
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% proportion_df(combine_SI = FALSE) %>% pull(value),
|
||||
c(example_isolates$AMX %>% proportion_S(),
|
||||
example_isolates$AMX %>% proportion_I(),
|
||||
example_isolates$AMX %>% proportion_R())
|
||||
)
|
||||
}
|
||||
|
||||
reset_all_thrown_messages()
|
||||
expect_warning(proportion_R(as.character(example_isolates$AMC)))
|
||||
@ -115,24 +134,6 @@ test_that("proportions works", {
|
||||
expect_warning(proportion_I(as.character(example_isolates$GEN)))
|
||||
reset_all_thrown_messages()
|
||||
expect_warning(proportion_S(example_isolates$AMC, as.character(example_isolates$GEN)))
|
||||
|
||||
# proportion_df
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% proportion_df() %>% pull(value),
|
||||
c(example_isolates$AMX %>% proportion_SI(),
|
||||
example_isolates$AMX %>% proportion_R())
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% proportion_df(combine_IR = TRUE) %>% pull(value),
|
||||
c(example_isolates$AMX %>% proportion_S(),
|
||||
example_isolates$AMX %>% proportion_IR())
|
||||
)
|
||||
expect_equal(
|
||||
example_isolates %>% select(AMX) %>% proportion_df(combine_SI = FALSE) %>% pull(value),
|
||||
c(example_isolates$AMX %>% proportion_S(),
|
||||
example_isolates$AMX %>% proportion_I(),
|
||||
example_isolates$AMX %>% proportion_R())
|
||||
)
|
||||
|
||||
expect_error(proportion_df(c("A", "B", "C")))
|
||||
expect_error(proportion_df(example_isolates[, "date"]))
|
||||
|
@ -28,65 +28,71 @@ context("resistance_predict.R")
|
||||
test_that("prediction of rsi works", {
|
||||
skip_on_cran()
|
||||
|
||||
library(dplyr)
|
||||
expect_output(AMX_R <- example_isolates %>%
|
||||
filter(mo == "B_ESCHR_COLI") %>%
|
||||
rsi_predict(col_ab = "AMX",
|
||||
col_date = "date",
|
||||
model = "binomial",
|
||||
minimum = 10,
|
||||
info = TRUE) %>%
|
||||
pull("value"))
|
||||
# AMX resistance will increase according to data set `example_isolates`
|
||||
expect_true(AMX_R[3] < AMX_R[20])
|
||||
|
||||
expect_output(x <- suppressMessages(resistance_predict(example_isolates, col_ab = "AMX", year_min = 2010, model = "binomial", info = TRUE)))
|
||||
if (require("dplyr")) {
|
||||
expect_output(AMX_R <- example_isolates %>%
|
||||
filter(mo == "B_ESCHR_COLI") %>%
|
||||
rsi_predict(col_ab = "AMX",
|
||||
col_date = "date",
|
||||
model = "binomial",
|
||||
minimum = 10,
|
||||
info = TRUE) %>%
|
||||
pull("value"))
|
||||
# AMX resistance will increase according to data set `example_isolates`
|
||||
expect_true(AMX_R[3] < AMX_R[20])
|
||||
}
|
||||
|
||||
expect_output(x <- suppressMessages(resistance_predict(example_isolates,
|
||||
col_ab = "AMX",
|
||||
year_min = 2010,
|
||||
model = "binomial",
|
||||
info = TRUE)))
|
||||
pdf(NULL) # prevent Rplots.pdf being created
|
||||
expect_silent(plot(x))
|
||||
expect_silent(ggplot_rsi_predict(x))
|
||||
expect_silent(ggplot(x))
|
||||
expect_error(ggplot_rsi_predict(example_isolates))
|
||||
|
||||
expect_output(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_output(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
model = "binomial",
|
||||
col_ab = "AMX",
|
||||
col_date = "date",
|
||||
info = TRUE))
|
||||
expect_output(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_output(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
model = "loglin",
|
||||
col_ab = "AMX",
|
||||
col_date = "date",
|
||||
info = TRUE))
|
||||
expect_output(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_output(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
model = "lin",
|
||||
col_ab = "AMX",
|
||||
col_date = "date",
|
||||
info = TRUE))
|
||||
|
||||
expect_error(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_error(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
model = "INVALID MODEL",
|
||||
col_ab = "AMX",
|
||||
col_date = "date",
|
||||
info = TRUE))
|
||||
expect_error(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_error(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
model = "binomial",
|
||||
col_ab = "NOT EXISTING COLUMN",
|
||||
col_date = "date",
|
||||
info = TRUE))
|
||||
expect_error(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_error(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
model = "binomial",
|
||||
col_ab = "AMX",
|
||||
col_date = "NOT EXISTING COLUMN",
|
||||
info = TRUE))
|
||||
expect_error(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_error(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
col_ab = "AMX",
|
||||
col_date = "NOT EXISTING COLUMN",
|
||||
info = TRUE))
|
||||
expect_error(rsi_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_error(rsi_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
col_ab = "AMX",
|
||||
col_date = "date",
|
||||
info = TRUE))
|
||||
# almost all E. coli are MEM S in the Netherlands :)
|
||||
expect_error(resistance_predict(x = filter(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
expect_error(resistance_predict(x = subset(example_isolates, mo == "B_ESCHR_COLI"),
|
||||
model = "binomial",
|
||||
col_ab = "MEM",
|
||||
col_date = "date",
|
||||
|
@ -59,30 +59,36 @@ test_that("rsi works", {
|
||||
expect_identical(as.logical(lapply(example_isolates, is.rsi.eligible)),
|
||||
rep(FALSE, length(example_isolates)))
|
||||
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
# 40 rsi columns
|
||||
expect_equal(example_isolates %>%
|
||||
mutate_at(vars(PEN:RIF), as.character) %>%
|
||||
lapply(is.rsi.eligible) %>%
|
||||
as.logical() %>%
|
||||
sum(),
|
||||
40)
|
||||
|
||||
expect_output(print(tibble(ab = as.rsi("S"))))
|
||||
|
||||
expect_error(as.rsi.mic(as.mic(16)))
|
||||
expect_error(as.rsi.disk(as.disk(16)))
|
||||
|
||||
expect_error(get_guideline("this one does not exist"))
|
||||
|
||||
expect_s3_class(example_isolates %>%
|
||||
mutate(m = as.mic(2),
|
||||
d = as.disk(20)) %>%
|
||||
skimr::skim(),
|
||||
"data.frame")
|
||||
expect_s3_class(skimr::skim(example_isolates),
|
||||
"data.frame")
|
||||
|
||||
if (require("dplyr")) {
|
||||
# 40 rsi columns
|
||||
expect_equal(example_isolates %>%
|
||||
mutate_at(vars(PEN:RIF), as.character) %>%
|
||||
lapply(is.rsi.eligible) %>%
|
||||
as.logical() %>%
|
||||
sum(),
|
||||
40)
|
||||
expect_equal(sum(is.rsi(example_isolates)), 40)
|
||||
|
||||
expect_output(print(tibble(ab = as.rsi("S"))))
|
||||
}
|
||||
|
||||
if (require("skimr")) {
|
||||
expect_s3_class(skim(example_isolates),
|
||||
"data.frame")
|
||||
if (require("dplyr")) {
|
||||
expect_s3_class(example_isolates %>%
|
||||
mutate(m = as.mic(2),
|
||||
d = as.disk(20)) %>%
|
||||
skim(),
|
||||
"data.frame")
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
test_that("mic2rsi works", {
|
||||
@ -110,12 +116,14 @@ test_that("mic2rsi works", {
|
||||
expect_equal(as.rsi(as.mic(32), "E. coli", "ampicillin", guideline = "EUCAST 2020"),
|
||||
as.rsi("R"))
|
||||
|
||||
expect_true(suppressWarnings(example_isolates %>%
|
||||
mutate(amox_mic = as.mic(2)) %>%
|
||||
select(mo, amox_mic) %>%
|
||||
as.rsi() %>%
|
||||
pull(amox_mic) %>%
|
||||
is.rsi()))
|
||||
if (require("dplyr")) {
|
||||
expect_true(suppressWarnings(example_isolates %>%
|
||||
mutate(amox_mic = as.mic(2)) %>%
|
||||
select(mo, amox_mic) %>%
|
||||
as.rsi() %>%
|
||||
pull(amox_mic) %>%
|
||||
is.rsi()))
|
||||
}
|
||||
})
|
||||
|
||||
test_that("disk2rsi works", {
|
||||
@ -140,13 +148,15 @@ test_that("disk2rsi works", {
|
||||
ab = "ERY",
|
||||
guideline = "CLSI")),
|
||||
"R")
|
||||
|
||||
expect_true(example_isolates %>%
|
||||
mutate(amox_disk = as.disk(15)) %>%
|
||||
select(mo, amox_disk) %>%
|
||||
as.rsi(guideline = "CLSI") %>%
|
||||
pull(amox_disk) %>%
|
||||
is.rsi())
|
||||
|
||||
if (require("dplyr")) {
|
||||
expect_true(example_isolates %>%
|
||||
mutate(amox_disk = as.disk(15)) %>%
|
||||
select(mo, amox_disk) %>%
|
||||
as.rsi(guideline = "CLSI") %>%
|
||||
pull(amox_disk) %>%
|
||||
is.rsi())
|
||||
}
|
||||
|
||||
# frequency tables
|
||||
if (require("cleaner")) {
|
||||
@ -165,20 +175,18 @@ test_that("data.frame2rsi works", {
|
||||
TOB = as.disk(16),
|
||||
ERY = "R", # note about assigning <rsi> class
|
||||
CLR = "V") # note about cleaning
|
||||
expect_s3_class(suppressWarnings(as.rsi(df)), "data.frame")
|
||||
expect_s3_class(suppressWarnings(as.rsi(df)),
|
||||
"data.frame")
|
||||
|
||||
expect_s3_class(suppressWarnings(as.rsi(data.frame(mo = "Escherichia coli",
|
||||
amoxi = c("R", "S", "I", "invalid")))$amoxi), "rsi")
|
||||
expect_warning(data.frame(mo = "E. coli",
|
||||
NIT = c("<= 2", 32)) %>%
|
||||
as.rsi())
|
||||
expect_message(data.frame(mo = "E. coli",
|
||||
NIT = c("<= 2", 32),
|
||||
uti = TRUE) %>%
|
||||
as.rsi())
|
||||
expect_message(
|
||||
data.frame(mo = "E. coli",
|
||||
NIT = c("<= 2", 32),
|
||||
specimen = c("urine", "blood")) %>%
|
||||
as.rsi())
|
||||
amoxi = c("R", "S", "I", "invalid")))$amoxi),
|
||||
"rsi")
|
||||
expect_warning(as.rsi(data.frame(mo = "E. coli",
|
||||
NIT = c("<= 2", 32))))
|
||||
expect_message(as.rsi(data.frame(mo = "E. coli",
|
||||
NIT = c("<= 2", 32),
|
||||
uti = TRUE)))
|
||||
expect_message(as.rsi(data.frame(mo = "E. coli",
|
||||
NIT = c("<= 2", 32),
|
||||
specimen = c("urine", "blood"))))
|
||||
})
|
||||
|
@ -56,7 +56,7 @@ test_that("imports work", {
|
||||
call_functions <- c(
|
||||
# cleaner
|
||||
"freq.default" = "cleaner",
|
||||
# skmir
|
||||
# skimr
|
||||
"inline_hist" = "skimr",
|
||||
"sfl" = "skimr",
|
||||
# set_mo_source
|
||||
|
Reference in New Issue
Block a user