From 4a47e59e6f0abec2e662ed87a1936ffaf6d2eae4 Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Tue, 3 Apr 2018 11:08:31 +0200 Subject: [PATCH] fortify code with more tests --- NEWS | 5 +- tests/testthat/test-first_isolates.R | 26 ++++++++-- tests/testthat/test-joins.R | 15 ++++-- tests/testthat/test-rsi_analysis.R | 71 +++++++++++++++++++++------- 4 files changed, 89 insertions(+), 28 deletions(-) diff --git a/NEWS b/NEWS index 2e2c2f13..ca90d540 100644 --- a/NEWS +++ b/NEWS @@ -12,8 +12,9 @@ - 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 - Expanded README.md -- Added unit testing with Travis CI (https://travis-ci.org/msberends/AMR) -- Added code coverage checking with Codecov (https://codecov.io/gh/msberends/AMR/tree/master/R) +- Added unit testing with the `testthat` package +- 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 - `EUCAST_rules` applies for amoxicillin even if ampicillin is missing diff --git a/tests/testthat/test-first_isolates.R b/tests/testthat/test-first_isolates.R index 8ef6026e..ce85cd6b 100644 --- a/tests/testthat/test-first_isolates.R +++ b/tests/testthat/test-first_isolates.R @@ -14,18 +14,18 @@ test_that("guess_bactid works", { test_that("first isolates work", { # septic_patients contains 1960 out of 2000 first isolates - septic_ptns <- septic_patients - expect_equal(sum(first_isolate(tbl = septic_ptns, + #septic_ptns <- septic_patients + expect_equal(sum(first_isolate(tbl = septic_patients, col_date = "date", col_patient_id = "patient_id", col_bactid = "bactid", info = FALSE)), 1960) - # septic_patients contains 1962 out of 2000 first weighted isolates - septic_ptns$keyab <- suppressWarnings(key_antibiotics(septic_ptns)) + # septic_patients contains 1962 out of 2000 first *weighted* isolates + #septic_ptns$keyab <- suppressWarnings(key_antibiotics(septic_ptns)) expect_equal( suppressWarnings(sum( - first_isolate(tbl = septic_ptns, + first_isolate(tbl = septic_patients %>% mutate(keyab = key_antibiotics(.)), col_date = "date", col_patient_id = "patient_id", col_bactid = "bactid", @@ -33,4 +33,20 @@ test_that("first isolates work", { type = "keyantibiotics", info = TRUE))), 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) + }) diff --git a/tests/testthat/test-joins.R b/tests/testthat/test-joins.R index cd23b49a..9009705e 100644 --- a/tests/testthat/test-joins.R +++ b/tests/testthat/test-joins.R @@ -4,15 +4,24 @@ test_that("joins work", { unjoined <- septic_patients inner <- septic_patients %>% inner_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(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(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(full)) + + expect_equal(nrow(left_join_microorganisms("ESCCOL")), 1) }) diff --git a/tests/testthat/test-rsi_analysis.R b/tests/testthat/test-rsi_analysis.R index fb5623f8..1d5e48b3 100644 --- a/tests/testthat/test-rsi_analysis.R +++ b/tests/testthat/test-rsi_analysis.R @@ -2,36 +2,71 @@ 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(septic_patients$amox), 0.5386, tolerance = 0.0001) + expect_equal(rsi(septic_patients$amox), 0.5386, tolerance = 0.0001) expect_equal(rsi_df(septic_patients, - ab = c("pita", "gent"), - interpretation = "S", - info = FALSE), 0.9809, tolerance = 0.0001) + ab = "amox", + info = FALSE), + 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, + ab = c("pita", "gent"), + interpretation = "S", + 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", { - 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` + 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_error(rsi_predict(tbl = septic_patients[which(septic_patients$bactid == "ESCCOL"),], + + expect_output(rsi_predict(tbl = filter(septic_patients, bactid == "ESCCOL"), + model = "binomial", + col_ab = "amox", + col_date = "date", + info = TRUE)) + expect_output(rsi_predict(tbl = filter(septic_patients, bactid == "ESCCOL"), + model = "loglin", + col_ab = "amox", + 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", col_ab = "amox", col_date = "date", 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_date = "date", 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_date = "NOT EXISTING COLUMN", info = FALSE))