1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-25 06:06:12 +01:00

fortify code with more tests

This commit is contained in:
dr. M.S. (Matthijs) Berends 2018-04-03 11:08:31 +02:00
parent 972e923484
commit 4a47e59e6f
4 changed files with 89 additions and 28 deletions

5
NEWS
View File

@ -12,8 +12,9 @@
- For parameters of functions `first_isolate`, `EUCAST_rules` the column names are now case-insensitive - For parameters of functions `first_isolate`, `EUCAST_rules` the column names are now case-insensitive
- Functions `as.rsi` and `as.mic` now add the package name and version as attribute - Functions `as.rsi` and `as.mic` now add the package name and version as attribute
- Expanded README.md - Expanded README.md
- Added unit testing with Travis CI (https://travis-ci.org/msberends/AMR) - Added unit testing with the `testthat` package
- Added code coverage checking with Codecov (https://codecov.io/gh/msberends/AMR/tree/master/R) - Added build tests for Linux and macOS using Travis CI (https://travis-ci.org/msberends/AMR)
- Added Line coverage checking using CodeCov (https://codecov.io/gh/msberends/AMR/tree/master/R)
## 0.1.1 ## 0.1.1
- `EUCAST_rules` applies for amoxicillin even if ampicillin is missing - `EUCAST_rules` applies for amoxicillin even if ampicillin is missing

View File

@ -14,18 +14,18 @@ test_that("guess_bactid works", {
test_that("first isolates work", { test_that("first isolates work", {
# septic_patients contains 1960 out of 2000 first isolates # septic_patients contains 1960 out of 2000 first isolates
septic_ptns <- septic_patients #septic_ptns <- septic_patients
expect_equal(sum(first_isolate(tbl = septic_ptns, expect_equal(sum(first_isolate(tbl = septic_patients,
col_date = "date", col_date = "date",
col_patient_id = "patient_id", col_patient_id = "patient_id",
col_bactid = "bactid", col_bactid = "bactid",
info = FALSE)), 1960) info = FALSE)), 1960)
# septic_patients contains 1962 out of 2000 first weighted isolates # septic_patients contains 1962 out of 2000 first *weighted* isolates
septic_ptns$keyab <- suppressWarnings(key_antibiotics(septic_ptns)) #septic_ptns$keyab <- suppressWarnings(key_antibiotics(septic_ptns))
expect_equal( expect_equal(
suppressWarnings(sum( suppressWarnings(sum(
first_isolate(tbl = septic_ptns, first_isolate(tbl = septic_patients %>% mutate(keyab = key_antibiotics(.)),
col_date = "date", col_date = "date",
col_patient_id = "patient_id", col_patient_id = "patient_id",
col_bactid = "bactid", col_bactid = "bactid",
@ -33,4 +33,20 @@ test_that("first isolates work", {
type = "keyantibiotics", type = "keyantibiotics",
info = TRUE))), info = TRUE))),
1962) 1962)
# set 1500 random observations to be of specimen type 'Urine'
random_rows <- sample(x = 1:2000, size = 1500, replace = FALSE)
expect_lt(sum(
first_isolate(tbl = mutate(septic_patients,
specimen = if_else(row_number() %in% random_rows,
"Urine",
"Unknown")),
col_date = "date",
col_patient_id = "patient_id",
col_bactid = "bactid",
col_specimen = "specimen",
filter_specimen = "Urine",
info = TRUE)),
1501)
}) })

View File

@ -4,6 +4,8 @@ test_that("joins work", {
unjoined <- septic_patients unjoined <- septic_patients
inner <- septic_patients %>% inner_join_microorganisms() inner <- septic_patients %>% inner_join_microorganisms()
left <- septic_patients %>% left_join_microorganisms() left <- septic_patients %>% left_join_microorganisms()
semi <- septic_patients %>% semi_join_microorganisms()
anti <- septic_patients %>% anti_join_microorganisms()
suppressWarnings(right <- septic_patients %>% right_join_microorganisms()) suppressWarnings(right <- septic_patients %>% right_join_microorganisms())
suppressWarnings(full <- septic_patients %>% full_join_microorganisms()) suppressWarnings(full <- septic_patients %>% full_join_microorganisms())
@ -13,6 +15,13 @@ test_that("joins work", {
expect_true(ncol(unjoined) < ncol(left)) expect_true(ncol(unjoined) < ncol(left))
expect_true(nrow(unjoined) == nrow(left)) expect_true(nrow(unjoined) == nrow(left))
expect_true(ncol(semi) == ncol(semi))
expect_true(nrow(semi) == nrow(semi))
expect_true(nrow(anti) == 0)
expect_true(nrow(unjoined) < nrow(right)) expect_true(nrow(unjoined) < nrow(right))
expect_true(nrow(unjoined) < nrow(full)) expect_true(nrow(unjoined) < nrow(full))
expect_equal(nrow(left_join_microorganisms("ESCCOL")), 1)
}) })

View File

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