From cf8bff20dcd23401acc61e2389c60fdbdb9dd490 Mon Sep 17 00:00:00 2001 From: Lukas Jung Date: Fri, 1 Dec 2023 15:46:17 +0100 Subject: [PATCH] debug and test `median_df()` --- R/median-table.R | 2 +- tests/testthat/test-median-table.R | 41 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-median-table.R diff --git a/R/median-table.R b/R/median-table.R index f67a99b..ef1c2b6 100644 --- a/R/median-table.R +++ b/R/median-table.R @@ -124,6 +124,6 @@ median_df <- function(x, even = c("mean", "low", "high"), ...) { # stores the names of `x` if there are any: tibble::tibble( term = names(x), estimate, certainty, na_ignored, na_total, rate_ignored_na, - sum_total, rate_ignored_sum, stringsAsFactors = FALSE + sum_total, rate_ignored_sum ) } diff --git a/tests/testthat/test-median-table.R b/tests/testthat/test-median-table.R new file mode 100644 index 0000000..6183902 --- /dev/null +++ b/tests/testthat/test-median-table.R @@ -0,0 +1,41 @@ + +# Example vectors --------------------------------------------------------- + +vec1 <- list( + a = 1:15, + b = c(1, 1, NA), + c = c(4, 4, NA, NA, NA, NA), + d = c(96, 24, 3, NA) +) + +vec2 <- iris[1:4] + +vec1_exp <- tibble::tibble( + term = c("a", "b", "c", "d"), + estimate = c(8, 1, 4, 24), + certainty = c(TRUE, FALSE, FALSE, FALSE), + na_ignored = c(0L, 1L, 3L, 1L), + na_total = c(0L, 1L, 4L, 1L), + rate_ignored_na = c(NaN, 1, 0.75, 1), + sum_total = c(15L, 3L, 6L, 4L), + rate_ignored_sum = c(0, 0.3333333333333333, 0.5, 0.25), +) + +vec2_exp <- tibble::tibble( + term = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), + estimate = c(5.8, 3, 4.35, 1.3), + certainty = rep(TRUE, 4L), + na_ignored = integer(4), + na_total = integer(4), + rate_ignored_na = rep(NaN, 4L), + sum_total = rep(150L, 4L), + rate_ignored_sum = numeric(4), +) + + +# Testing ----------------------------------------------------------------- + +test_that("`median_df()` works correctly", { + expect_equal(median_df(vec1), vec1_exp) + expect_equal(median_df(vec2), vec2_exp) +})