Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from nutriverse/dev
Browse files Browse the repository at this point in the history
create tests for muac check functions
  • Loading branch information
ernestguevarra authored Feb 21, 2024
2 parents b867d86 + 3106d92 commit 9b70117
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
33 changes: 4 additions & 29 deletions R/muac_checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions tests/testthat/test-muac-check.R
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 9b70117

Please sign in to comment.