1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-08 16:42:10 +02:00

add tests using testthat

This commit is contained in:
2018-03-27 17:43:42 +02:00
parent c26839b08e
commit 6f7730dcaa
19 changed files with 231 additions and 17 deletions

4
tests/testthat.R Normal file
View File

@ -0,0 +1,4 @@
library(testthat)
library(AMR)
test_check("AMR")

BIN
tests/testthat/Rplots.pdf Normal file

Binary file not shown.

15
tests/testthat/test-atc.R Normal file
View File

@ -0,0 +1,15 @@
context("atc.R")
test_that("atc_property works", {
expect_equal(tolower(atc_property("J01CA04", property = "Name")), "amoxicillin")
expect_equivalent(atc_property("J01CA04", "DDD"), 1)
})
test_that("abname works", {
expect_equal(abname("AMOX"), "Amoxicillin")
expect_equal(abname(c("AMOX", "GENT")), c("Amoxicillin", "Gentamicin"))
expect_equal(abname("AMOX", from = 'umcg'), "Amoxicillin")
expect_equal(abname("amox", from = 'molis'), "Amoxicillin")
expect_equal(abname("J01CA04", from = 'atc'), "Amoxicillin")
})

View File

@ -0,0 +1,37 @@
context("classes.R")
test_that("rsi works", {
expect_true(as.rsi("S") < as.rsi("I"))
expect_true(as.rsi("I") < as.rsi("R"))
expect_true(as.rsi("R") > as.rsi("S"))
expect_true(is.rsi(as.rsi("S")))
expect_equal(suppressWarnings(as.logical(as.rsi("INVALID VALUE"))), NA)
expect_equal(class(barplot(as.rsi(c("S", "I", "R")))), "numeric")
expect_equal(summary(as.rsi(c("S", "R"))), c("Mode" = 'rsi',
"<NA>" = "0",
"Sum S" = "1",
"Sum IR" = "1",
"Sum R" = "1",
"Sum I" = "0"))
})
test_that("mic works", {
expect_true(as.mic(8) == as.mic("8"))
expect_true(as.mic("1") > as.mic("<=0.0625"))
expect_true(as.mic("1") < as.mic(">=32"))
expect_true(is.mic(as.mic(8)))
expect_equal(as.double(as.mic(">=32")), 32)
expect_equal(as.integer(as.mic(">=32")), 32)
expect_equal(suppressWarnings(as.logical(as.mic("INVALID VALUE"))), NA)
expect_equal(class(plot(as.mic(c(1, 2, 4, 8)))), "numeric")
expect_equal(summary(as.mic(c(2, 8))), c("Mode" = 'mic',
"<NA>" = "0",
"Min." = "2",
"Max." = "8"))
})

View File

@ -0,0 +1,9 @@
context("clipboard.R")
test_that("clipboard works", {
# why is the <<- needed? Won't work without it...
t1 <<- AMR::antibiotics
clipboard_export(t1, info = FALSE)
t2 <- clipboard_import()
expect_equal(t1, t2)
})

View File

@ -0,0 +1,31 @@
context("EUCAST.R")
test_that("EUCAST rules work", {
a <- data.frame(bactid = c("KLEPNE", # Klebsiella pneumoniae
"PSEAER", # Pseudomonas aeruginosa
"ENTAER"), # Enterobacter aerogenes
amox = "-", # Amoxicillin
stringsAsFactors = FALSE)
b <- data.frame(bactid = c("KLEPNE", # Klebsiella pneumoniae
"PSEAER", # Pseudomonas aeruginosa
"ENTAER"), # Enterobacter aerogenes
amox = "R", # Amoxicillin
stringsAsFactors = FALSE)
expect_equal(EUCAST_rules(a, info = FALSE), b)
expect_equal(interpretive_reading(a, info = FALSE), b)
a <- data.frame(bactid = c("STAAUR", # Staphylococcus aureus
"STCGRA"), # Streptococcus pyognenes (Lancefield Group A)
coli = "-", # Colistin
stringsAsFactors = FALSE)
b <- data.frame(bactid = c("STAAUR", # Staphylococcus aureus
"STCGRA"), # Streptococcus pyognenes (Lancefield Group A)
coli = "R", # Colistin
stringsAsFactors = FALSE)
expect_equal(EUCAST_rules(a, info = FALSE), b)
})
test_that("MO properties work", {
expect_equal(mo_property("ESCCOL"), "Escherichia coli")
expect_equal(mo_property("STAAUR"), "Staphylococcus aureus")
})

View File

@ -0,0 +1,23 @@
context("first_isolates.R")
test_that("keyantibiotics work", {
expect_equal(length(key_antibiotics(septic_patients, info = FALSE)), nrow(septic_patients))
expect_true(key_antibiotics_equal("SSS", "SSS"))
expect_true(key_antibiotics_equal("SSS", "SIS", ignore_I = TRUE))
expect_false(key_antibiotics_equal("SSS", "SIS", ignore_I = FALSE))
})
test_that("guess_bactid works", {
expect_equal(guess_bactid("E. coli"), "ESCCOL")
expect_equal(guess_bactid("Escherichia coli"), "ESCCOL")
})
test_that("first isolates work", {
# septic_patients contains 1960 out of 2000 first isolates
expect_equal(sum(first_isolate(septic_patients,
"date",
"patient_id",
"bactid",
info = FALSE)), 1960)
})

View File

@ -0,0 +1,18 @@
context("joins.R")
test_that("joins work", {
unjoined <- septic_patients
inner <- septic_patients %>% inner_join_microorganisms()
left <- septic_patients %>% left_join_microorganisms()
suppressWarnings(right <- septic_patients %>% right_join_microorganisms())
suppressWarnings(full <- septic_patients %>% full_join_microorganisms())
expect_true(ncol(unjoined) < ncol(inner))
expect_true(nrow(unjoined) == nrow(inner))
expect_true(ncol(unjoined) < ncol(left))
expect_true(nrow(unjoined) == nrow(left))
expect_true(nrow(unjoined) < nrow(right))
expect_true(nrow(unjoined) < nrow(full))
})

View File

@ -0,0 +1,20 @@
context("misc.R")
test_that("`like` works", {
expect_true("test" %like% "^t")
expect_true("test" %like% "test")
expect_true("test" %like% "TEST")
expect_true(as.factor("test") %like% "TEST")
})
test_that("percentages works", {
expect_equal(percent(0.25), "25%")
expect_equal(percent(0.5), "50%")
expect_equal(percent(0.1234), "12.3%")
})
test_that("quasiquotation works", {
expect_equal(quasiquotate(deparse(substitute("test")), "test"), "test")
expect_equal(quasiquotate(deparse(substitute('test')), "'test'"), "test")
expect_equal(quasiquotate(deparse(substitute(test)), test), "test")
})

View File

@ -0,0 +1,38 @@
context("rsi_analysis.R")
test_that("rsi works", {
# amox resistance in `septic_patients` should be around 53.86%
amox_R <- septic_patients %>% summarise(amox = rsi(amox)) %>% pull(amox)
expect_equal(amox_R, 0.5386, tolerance = 0.0001)
expect_equal(rsi_df(septic_patients,
ab = "amox",
info = FALSE), 0.5386, tolerance = 0.0001)
# and pita+genta susceptibility around 98.09%
expect_equal(rsi_df(septic_patients,
ab = c("pita", "gent"),
interpretation = "S",
info = FALSE), 0.9809, tolerance = 0.0001)
})
test_that("prediction of rsi works", {
amox_R <- rsi_predict(tbl = septic_patients[which(septic_patients$bactid == "ESCCOL"),],
col_ab = "amox",
col_date = "date",
info = FALSE)
amox_R <- amox_R %>% pull("probR")
# amox resistance will decrease according to `septic_patients`
expect_true(amox_R[2] > amox_R[20])
expect_error(rsi_predict(tbl = septic_patients[which(septic_patients$bactid == "ESCCOL"),],
model = "INVALID MODEL",
col_ab = "amox",
col_date = "date",
info = FALSE))
expect_error(rsi_predict(tbl = septic_patients[which(septic_patients$bactid == "ESCCOL"),],
col_ab = "NOT EXISTING COLUMN",
col_date = "date",
info = FALSE))
expect_error(rsi_predict(tbl = septic_patients[which(septic_patients$bactid == "ESCCOL"),],
col_ab = "amox",
col_date = "NOT EXISTING COLUMN",
info = FALSE))
})