tests/testthat/test_utils.R

library(testthat)
library(riskscorer)

context("Checking logical_to_numeric_or_null")
test_that("logical_to_numeric_or_null works corretly", {
  expect_null(logical_to_numeric_or_null(NULL))
  expect_null(logical_to_numeric_or_null(NA))
  expect_equal(1, logical_to_numeric_or_null(TRUE))
  expect_equal(0, logical_to_numeric_or_null(FALSE))
  expect_error(logical_to_numeric_or_null(1))
  expect_error(logical_to_numeric_or_null("1"))
})

context("Checking NYHA parsing")
test_that("NYHA gets parsed correctly", {
  expect_equal(1 ,parse_nyha(1.3))
  expect_equal(2 ,parse_nyha(1.5))
  expect_error(parse_nyha(0.5))
  expect_error(parse_nyha(4.2))
  expect_equal(1 ,parse_nyha("1.3"))
  expect_equal(2 ,parse_nyha("1.5"))
  expect_error(parse_nyha("0.5"))
  expect_error(parse_nyha("NYHA 4.2"))
  expect_null(parse_nyha(NA))
  expect_null(parse_nyha(NULL))
})

context("Checking eGFR calculation")
test_that("eGFR gets correctly calculated", {
  expect_equal(90, cc_eGFR(1, weight = 80, age = 45, sex = "Female"))
  expect_equal(47, cc_eGFR(1, weight = 80, age = 90, sex = "f"))
  expect_equal(47, cc_eGFR(1, weight = 80, age = 90, sex = "Female"))
  expect_equal(56, cc_eGFR(1, weight = 80, age = 90, sex = "m"))
  expect_equal(38, cc_eGFR(1, weight = 55, age = 90, sex = "m"))

  expect_error(cc_eGFR(-1, weight = 55, age = 90, sex = "m"))
  expect_error(cc_eGFR(21, weight = 55, age = 90, sex = "m"))
  expect_error(cc_eGFR(1, weight = 55, age = 100, sex = "m"))
  expect_error(cc_eGFR(5, weight = 55, age = 100, sex = "MALE_"))
  expect_error(cc_eGFR(5, weight = 5, age = 100, sex = "MALE"))
  expect_error(cc_eGFR(5, weight = 5, age = 100, sex = "MALE"))
  expect_error(cc_eGFR(5, weight = "F", age = 100, sex = "MALE"))
  expect_error(cc_eGFR(5, weight = "F", age = 100, sex = "MALE", digits_round = 4))
  expect_error(cc_eGFR(1, weight = 50, age = 50, sex = "MALE", digits_round = 4.1))
})

context("Checking str utility functions")
test_that("str_starts_with works correctly", {
  expect_true(str_starts_with("preopertive", "pre"))
  expect_false(str_starts_with("preopertive", "intra"))

  expect_true(str_starts_with("intraop", "intra"))
  expect_false(str_starts_with("intraop", "post"))
  expect_false(str_starts_with("preopertive", "intra"))

  expect_true(str_starts_with("post", "post"))
  expect_false(str_starts_with("post", "postop"))
})

context("Checking regurg parsing")
test_that("Regurgitation classification gets correctly translated into STS nomenclature", {
  expect_null(parse_vd_regurg(""))
  expect_null(parse_vd_regurg(NA))
  expect_equal("None", parse_vd_regurg("0"))
  expect_equal("None", parse_vd_regurg(FALSE))
  expect_equal("None", parse_vd_regurg(0))
  expect_equal("None", parse_vd_regurg("Keine"))
  expect_equal("None", parse_vd_regurg("None"))
  expect_equal("None", parse_vd_regurg("no"))
  expect_equal("None", parse_vd_regurg("n"))
  expect_equal("Trivial/Trace", parse_vd_regurg("trivial"))
  expect_equal("Trivial/Trace", parse_vd_regurg("Trace"))
  expect_equal("Trivial/Trace", parse_vd_regurg("spur"))
  expect_equal("Mild", parse_vd_regurg("1"))
  expect_equal("Mild", parse_vd_regurg(1))
  expect_equal("Mild", parse_vd_regurg("AI 1+"))
  expect_equal("Mild", parse_vd_regurg("MI 1+"))
  expect_equal("Mild", parse_vd_regurg("mild"))
  expect_equal("Moderate", parse_vd_regurg("MITTELGRADIG"))
  expect_equal("Moderate", parse_vd_regurg("MITTELschwer"))
  expect_equal("Moderate", parse_vd_regurg("2"))
  expect_equal("Moderate", parse_vd_regurg(2))
  expect_equal("Moderate", parse_vd_regurg("MI mittelschwer"))
  expect_null(parse_vd_regurg("mittegradig"))
  expect_equal("Severe", parse_vd_regurg("severe"))
  expect_equal("Severe", parse_vd_regurg("MI 3+"))
  expect_equal("Severe", parse_vd_regurg("MI 3"))
  expect_equal("Severe", parse_vd_regurg("Severe"))
  expect_equal("Severe", parse_vd_regurg("schwergradig"))
  expect_equal("Severe", parse_vd_regurg("schwergradige MI"))
  expect_equal("Severe", parse_vd_regurg(3))
})

context("Checking CHD parsing")
test_that("CHD gets correctly translated into STS nomenclature", {
  expect_equal("One", parseVesselsDisease("KHK-1"))
  expect_equal("One", parseVesselsDisease("KHK1"))
  expect_equal("One", parseVesselsDisease("1KHK"))
  expect_equal("One", parseVesselsDisease("1khK"))
  expect_equal("One", parseVesselsDisease("1-khK"))
  expect_equal("Three", parseVesselsDisease("KHK-3"))
  expect_equal("Two", parseVesselsDisease("ChdII"))
  expect_equal("Three", parseVesselsDisease("3CHD"))
  expect_equal("Three", parseVesselsDisease("3chd"))
  expect_equal("Three", parseVesselsDisease("III-CHD"))
  expect_equal("One", parseVesselsDisease("KHK-I"))
  expect_equal("One", parseVesselsDisease("KHK 1"))
  expect_equal("Two", parseVesselsDisease("II KHK"))
  expect_equal("One", parseVesselsDisease("1 khK"))
  expect_equal("One", parseVesselsDisease("1 khK"))
  expect_equal("Three", parseVesselsDisease("vessel 3"))
  expect_equal("Three", parseVesselsDisease("Vessels III"))
  expect_equal("Three", parseVesselsDisease("3vessel"))

  expect_equal("None", parseVesselsDisease(0))
  expect_equal("None", parseVesselsDisease(FALSE))
  expect_equal("None", parseVesselsDisease(F))
  expect_equal("None", parseVesselsDisease("Nein"))
  expect_equal("None", parseVesselsDisease("n"))
  expect_equal("None", parseVesselsDisease("no"))
  expect_equal("None", parseVesselsDisease("NOne"))

  expect_null(parseVesselsDisease("3 CHHD"))
  expect_null(parseVesselsDisease("4 CHD"))
  expect_null(parseVesselsDisease("EinsKHK"))
  expect_null(parseVesselsDisease(""))
  expect_null(parseVesselsDisease(NA))
  expect_null(parseVesselsDisease(NULL))
})

context("Checking boolean parsing")

test_that("Boolean strings return TRUE", {
  expect_true(parse_bool("y"))
  expect_true(parse_bool("Y"))
  expect_true(parse_bool("Yes"))
  expect_true(parse_bool("yEs"))
  expect_true(parse_bool("YES"))
  expect_true(parse_bool("t"))
  expect_true(parse_bool("T"))
  expect_true(parse_bool("True"))
  expect_true(parse_bool("TRUE"))
  expect_true(parse_bool("TRUE"))
  expect_true(parse_bool("1"))
})

test_that("Boolean strings return FALSE", {
  expect_false(parse_bool("n"))
  expect_false(parse_bool("N"))
  expect_false(parse_bool("No"))
  expect_false(parse_bool("no"))
  expect_false(parse_bool("nO"))
  expect_false(parse_bool("NO"))
  expect_false(parse_bool("f"))
  expect_false(parse_bool("F"))
  expect_false(parse_bool("FALSE"))
  expect_false(parse_bool("false"))
  expect_false(parse_bool("faLSe"))
  expect_false(parse_bool("0"))
})

test_that("parse_bool_and_add works", {
  expect_equal("No", parse_bool_and_add("FaLSE"))
  expect_equal("No", parse_bool_and_add("F"))
  expect_false(parse_bool_and_add("0", return_val_false = FALSE))
  expect_true(parse_bool_and_add("T", return_val_false = FALSE, return_val_true = TRUE))
  expect_equal("Unknown", parse_bool_and_add("Unknown",
                                             additionals = "Unknown",
                                             return_val_false = FALSE,
                                             return_val_true = TRUE))
  expect_true(parse_bool_and_add("1",
                                 additionals = "Unknown",
                                 return_val_false = FALSE,
                                 return_val_true = TRUE))

  expect_false(parse_bool_and_add("0",
                                  additionals = c("Unknown", "Undisclosed"),
                                  return_val_false = FALSE,
                                  return_val_true = TRUE))

  expect_equal("Undisclosed", parse_bool_and_add("Undisclosed",
                                                 additionals = c("Unknown", "Undisclosed")))
})

context("Checking Sex parsing")
test_that("parse_sex works without arguments", {
  expect_equal("Male", parse_sex("male"))
  expect_equal("Male", parse_sex("m"))
  expect_equal("Male", parse_sex("mALE"))
  expect_equal("Male", parse_sex("M"))
  expect_equal("Female", parse_sex("Female"))
  expect_equal("Female", parse_sex("FEMalE"))
  expect_equal("Female", parse_sex("F"))
  expect_equal("Female", parse_sex("f"))
  expect_true(is.null(parse_sex(NULL)))
  expect_true(is.null(parse_sex(NA)))
})

test_that("parse_sex works with special arguments", {
  expect_equal("Male", parse_sex("male", male_numeric_code = 1, female_numeric_code = 0))
  expect_equal("Male", parse_sex("1", male_numeric_code = 1, female_numeric_code = 9))
  expect_equal("Male", parse_sex("1", male_numeric_code = 1, female_numeric_code = 0))
  expect_equal("Male", parse_sex("0", male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Female", parse_sex("1", male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Female", parse_sex("F"))
  expect_equal("Female", parse_sex("f"))
  expect_equal("Female", parse_sex("FAlse"))
  expect_equal("Male", parse_sex(TRUE))
  expect_equal("Female", parse_sex("No"))
  expect_equal("Male", parse_sex("Y"))
  expect_equal("Female", parse_sex("N"))
  expect_equal("Female", parse_sex("n"))
  expect_equal("Female", parse_sex("n", male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Female", parse_sex("FALSE", male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Female", parse_sex(F, male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Female", parse_sex("NO", male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Male", parse_sex("TRUE", male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
  expect_error(parse_sex("T", male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Male", parse_sex("Y", male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
  expect_equal("Male", parse_sex("Yes", male_bool_code = TRUE, male_numeric_code = 0, female_numeric_code = 1))
})
meyera/riskscorer documentation built on May 22, 2019, 7:54 p.m.