From 3106d921fb14dc55d7b2ff7f61bb59e9d296b9d6 Mon Sep 17 00:00:00 2001 From: Ernest Guevarra Date: Wed, 21 Feb 2024 10:21:58 +0000 Subject: [PATCH] create tests for muac check functions --- R/muac_checks.R | 33 +++---------------------- tests/testthat/test-muac-check.R | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 tests/testthat/test-muac-check.R diff --git a/R/muac_checks.R b/R/muac_checks.R index fec7119..f7e3604 100644 --- a/R/muac_checks.R +++ b/R/muac_checks.R @@ -90,34 +90,7 @@ ipc_muac_check <- function(df, df <- df |> dplyr::mutate(muac = muac * 10) } -# if (.summary) { - summarise_muac_check(df, .summary = .summary, .list = .list) - # } else { - # df |> - # dplyr::mutate( - # age_ratio = nipnTK::ageRatioTest(as.integer(!is.na(age)))$observedR, - # age_ratio_p = nipnTK::ageRatioTest(as.integer(!is.na(age)))$p, - # sex_ratio = nipnTK::sexRatioTest(sex, codes = c(1, 2))$pM, - # sex_ratio_p = nipnTK::sexRatioTest(sex, codes = c(1, 2))$p, - # digit_preference = nipnTK::digitPreference(muac, digits = 0)$dps, - # digit_preference_class = nipnTK::digitPreference(muac, digits = 0)$dpsClass, - # std_dev = stats::sd(muac, na.rm = TRUE), - # age_ratio_class = classify_age_ratio(.data$age_ratio_p), - # sex_ratio_class = classify_sex_ratio(.data$sex_ratio_p), - # std_dev_class = classify_sd(.data$std_dev), - # quality_score = classify_quality( - # .data$age_ratio_class, .data$sex_ratio_class, - # .data$std_dev_class, .data$digit_preference_class - # )$q_score, - # quality_class = classify_quality( - # .data$age_ratio_class, .data$sex_ratio_class, - # .data$std_dev_class, .data$digit_preference_class - # )$q_class - # ) |> - # dplyr::relocate(.data$age_ratio_class, .after = "age_ratio_p") |> - # dplyr::relocate(.data$sex_ratio_class, .after = "sex_ratio_p") |> - # dplyr::relocate(.data$std_dev_class, .after = "std_dev") - # } + summarise_muac_check(df, .summary = .summary, .list = .list) } @@ -146,7 +119,9 @@ summarise_muac_check <- function(df, .summary = TRUE, .list = TRUE) { quality_score = classify_quality( .data$age_ratio_class, .data$sex_ratio_class, .data$std_dev_class, .data$digit_preference_class - )$q_score, + )$q_score + ) |> + dplyr::mutate( quality_class = classify_quality( .data$age_ratio_class, .data$sex_ratio_class, .data$std_dev_class, .data$digit_preference_class diff --git a/tests/testthat/test-muac-check.R b/tests/testthat/test-muac-check.R new file mode 100644 index 0000000..b1d201c --- /dev/null +++ b/tests/testthat/test-muac-check.R @@ -0,0 +1,42 @@ +# Tests for MUAC check functions ----------------------------------------------- + +## Test check function - list output ---- +muac_check <- ipc_muac_check( + df = muac_data, muac_units = "cm", oedema_recode = c(1, 2) +) + +testthat::expect_type(muac_check, "list") +testthat::expect_vector(muac_check) +testthat::expect_named( + muac_check, + c("Age Ratio", "Sex Ratio", "Digit Preference", + "Standard Deviation", "Data Quality") +) + + +## Test check function - non-list output ---- +muac_check <- ipc_muac_check( + df = muac_data, muac_units = "cm", oedema_recode = c(1, 2), .list = FALSE +) + +testthat::expect_type(muac_check, "list") +testthat::expect_s3_class(muac_check, "tbl_df") +testthat::expect_vector(muac_check) +testthat::expect_equal(ncol(muac_check), 12) +testthat::expect_equal(nrow(muac_check), 1) + + +## Test check function - non-summary output ---- +muac_check <- ipc_muac_check( + df = muac_data, muac_units = "cm", oedema_recode = c(1, 2), .summary = FALSE +) + +testthat::expect_type(muac_check, "list") +testthat::expect_s3_class(muac_check, "tbl_df") +testthat::expect_vector(muac_check) +testthat::expect_true( + all(c("age_ratio", "age_ratio_p", "age_ratio_class", "sex_ratio", "sex_ratio_p", + "sex_ratio_class", "digit_preference", "digit_preference_class", "std_dev", + "std_dev_class", "quality_score", "quality_class") %in% names(muac_check)) +) +testthat::expect_equal(nrow(muac_check), nrow(muac_data))