Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Aug 9, 2024
1 parent 789eea6 commit a4b1c66
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
6 changes: 3 additions & 3 deletions R/ard_stats_poisson_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param na.rm (scalar `logical`)\cr
#' whether missing values should be removed before summing the numerator and the denominator. Default
#' is `TRUE`.
#' whether missing values should be removed before summing the numerator and the denominator. Default is `TRUE`.
#' @param ... arguments passed to [poisson.test()].
#' @return an ARD data frame of class 'card'
#' @name ard_stats_poisson_test
Expand Down Expand Up @@ -124,7 +123,8 @@ ard_stats_poisson_test <- function(data, numerator, denominator, na.rm = TRUE, b
formals = formals(asNamespace("stats")[["poisson.test"]]),
passed_args = dots_list(...),
lst_ard_columns = list(context = "stats_poisson_test")
)
) |>
dplyr::distinct()

# rename "r" statistic to "mu"
ret$stat_name[ret$stat_name == "r"] <- "mu"
Expand Down
3 changes: 1 addition & 2 deletions man/ard_stats_poisson_test.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/ard_stats_poisson_test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ard_stats_poisson_test() errors are handled correctly

Code
ard_stats_poisson_test(cards::ADTTE, by = TRTA, numerator = CNSR, denominator = AVAL)
Condition
Error in `ard_stats_poisson_test()`:
! The `by` argument must have a maximum of two levels.

81 changes: 81 additions & 0 deletions tests/testthat/test-ard_stats_poisson_test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
skip_if_not(is_pkg_installed("broom", reference_pkg = "cardx"))

test_that("ard_stats_poisson_test() works for one sample tests", {
# Default values work
expect_silent(
ard_stats_poisson_test(cards::ADTTE, numerator = CNSR, denominator = AVAL)
)

# Custom values work
expect_silent(
ard_single <- ard_stats_poisson_test(
cards::ADTTE,
numerator = "CNSR",
denominator = "AVAL",
conf.level = 0.90,
r = 0.8,
alternative = "greater"
)
)

# Statistics calculated correctly
expect_equal(
ard_single |>
cards::get_ard_statistics(
stat_name %in%
c("estimate", "statistic", "p.value", "parameter", "conf.low", "conf.high", "method", "alternative")
),
poisson.test(
x = sum(cards::ADTTE$CNSR),
T = sum(cards::ADTTE$AVAL),
r = 0.8,
conf.level = 0.9,
alternative = "greater"
) |>
broom::tidy() |>
unclass(),
ignore_attr = TRUE
)
})

test_that("ard_stats_poisson_test() works for two sample tests", {
expect_silent(
ard_compare <-
cards::ADTTE |>
dplyr::filter(TRTA %in% c("Placebo", "Xanomeline High Dose")) |>
ard_stats_poisson_test(by = TRTA, numerator = CNSR, denominator = AVAL)
)

# Statistics calculated correctly
expect_equal(
ard_compare |>
cards::get_ard_statistics(
stat_name %in%
c("estimate", "statistic", "p.value", "parameter", "conf.low", "conf.high", "method", "alternative")
),
poisson.test(
x = cards::ADTTE |>
dplyr::filter(TRTA %in% c("Placebo", "Xanomeline High Dose")) |>
dplyr::group_by(TRTA) |>
dplyr::summarise(sum = sum(CNSR)) |>
dplyr::pull(sum),
T = cards::ADTTE |>
dplyr::filter(TRTA %in% c("Placebo", "Xanomeline High Dose")) |>
dplyr::group_by(TRTA) |>
dplyr::summarise(sum = sum(AVAL)) |>
dplyr::pull(sum)
) |>
broom::tidy() |>
unclass(),
ignore_attr = TRUE
)

})

test_that("ard_stats_poisson_test() errors are handled correctly", {
expect_snapshot(
cards::ADTTE |>
ard_stats_poisson_test(by = TRTA, numerator = CNSR, denominator = AVAL),
error = TRUE
)
})

0 comments on commit a4b1c66

Please sign in to comment.