Skip to content

Commit

Permalink
add unit tests for protected names (#266)
Browse files Browse the repository at this point in the history
**What changes are proposed in this pull request?**
- Added unit tests for protected names for `ard_continuous` and
`ard_categorical`

closes #265 



--------------------------------------------------------------------------------

Pre-review Checklist (if item does not apply, mark is as complete)
- [ ] **All** GitHub Action workflows pass with a ✅
- [ ] PR branch has pulled the most recent updates from master branch:
`usethis::pr_merge_main()`
- [ ] If a bug was fixed, a unit test was added.
- [ ] Code coverage is suitable for any new functions/features
(generally, 100% coverage for new code): `devtools::test_coverage()`
- [ ] Request a reviewer

Reviewer Checklist (if item does not apply, mark is as complete)

- [ ] If a bug was fixed, a unit test was added.
- [ ] Run `pkgdown::build_site()`. Check the R console for errors, and
review the rendered website.
- [ ] Code coverage is suitable for any new functions/features:
`devtools::test_coverage()`

When the branch is ready to be merged:
- [ ] Update `NEWS.md` with the changes from this pull request under the
heading "`# cards (development version)`". If there is an issue
associated with the pull request, reference it in parentheses at the end
update (see `NEWS.md` for examples).
- [ ] **All** GitHub Action workflows pass with a ✅
- [ ] Approve Pull Request
- [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".

---------

Co-authored-by: Daniel Sjoberg <danield.sjoberg@gmail.com>
  • Loading branch information
ayogasekaram and ddsjoberg authored Jul 19, 2024
1 parent 90e6dba commit 0586777
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/ard_categorical.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,11 @@
Error in `ard_categorical()`:
! The `by` argument cannot include variables named "variable" and "variable_level".

---

Code
ard_categorical(mtcars2, by = variable, variables = by)
Condition
Error in `ard_categorical()`:
! The `by` argument cannot include variables named "variable" and "variable_level".

109 changes: 109 additions & 0 deletions tests/testthat/test-ard_categorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,112 @@ test_that("ard_categorical(by) messages about protected names", {
'The `by` argument cannot include variables named "variable" and "variable_level".'
)
})

# - test if function parameters can be used as variable names without error
test_that("ard_categorical() works when using generic names ", {
# rename some variables
mtcars2 <- mtcars %>%
dplyr::rename("variable" = am, "variable_level" = cyl, "by" = disp, "group1_level" = gear)

expect_equal(
ard_categorical(mtcars, variables = c(am, cyl), by = disp, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(variable, variable_level), by = by, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(cyl, am), by = gear, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(variable_level, variable), by = group1_level, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(gear, am), by = disp, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(group1_level, variable), by = by, denominator = "row") |> dplyr::select(stat)
)

# rename vars
mtcars2 <- mtcars %>%
dplyr::rename("N" = am, "p" = cyl, "name" = disp, "group1_level" = gear)

expect_equal(
ard_categorical(mtcars, variables = c(am, cyl), by = disp, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(N, p), by = name, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(disp, gear), by = am, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(name, group1_level), by = N, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(am, disp), by = gear, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(N, name), by = group1_level, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(am, disp), by = cyl, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(N, name), by = p, denominator = "row") |> dplyr::select(stat)
)

# rename vars
mtcars2 <- mtcars %>%
dplyr::rename("n" = am, "mean" = cyl, "p.std.error" = disp, "n_unweighted" = gear)

expect_equal(
ard_categorical(mtcars, variables = c(gear, cyl), by = disp, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(n_unweighted, mean), by = p.std.error, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(gear, cyl), by = am, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(n_unweighted, mean), by = n, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(am, disp), by = cyl, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(n, p.std.error), by = mean, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(am, disp), by = gear, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(n, p.std.error), by = n_unweighted, denominator = "row") |> dplyr::select(stat)
)

# rename vars
mtcars2 <- mtcars %>%
dplyr::rename("N_unweighted" = am, "p_unweighted" = cyl, "column" = disp, "row" = gear)

expect_equal(
ard_categorical(mtcars, variables = c(am, cyl), by = disp, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(N_unweighted, p_unweighted), by = column, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(disp, gear), by = am, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(column, row), by = N_unweighted, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(am, disp), by = cyl, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(N_unweighted, column), by = p_unweighted, denominator = "row") |> dplyr::select(stat)
)

expect_equal(
ard_categorical(mtcars, variables = c(am, disp), by = gear, denominator = "row") |> dplyr::select(stat),
ard_categorical(mtcars2, variables = c(N_unweighted, column), by = row, denominator = "row") |> dplyr::select(stat)
)
})

test_that("ard_categorical(by) messages about protected names", {
mtcars2 <- mtcars %>%
dplyr::rename("variable" = am, "variable_level" = cyl, "by" = disp, "group1_level" = gear)

expect_snapshot(
error = TRUE,
ard_categorical(mtcars2, by = variable, variables = by)
)

expect_error(
ard_categorical(mtcars2, by = variable_level, variables = by),
'The `by` argument cannot include variables named "variable" and "variable_level".'
)
})
114 changes: 114 additions & 0 deletions tests/testthat/test-ard_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,117 @@ test_that("ard_continuous() works with non-syntactic names", {
) |>
as.data.frame())
})

# - test if function parameters can be used as variable names without error
test_that("ard_continuous() works when using generic names ", {
mtcars2 <- mtcars %>%
dplyr::rename("variable_level" = mpg, "variable" = cyl, "median" = disp, "p25" = gear)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, cyl), by = disp) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(variable_level, variable), by = median) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(disp, gear), by = mpg) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(median, p25), by = variable_level) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(disp, gear), by = cyl) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(median, p25), by = variable) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(disp, mpg), by = gear) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(median, variable_level), by = p25) |> dplyr::select(stat)
)

# rename vars

mtcars2 <- mtcars %>%
dplyr::rename("by" = mpg, "statistic" = cyl, "weights" = disp, "p75" = gear)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, cyl), by = disp) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(by, statistic), by = weights) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(cyl, disp), by = mpg) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(statistic, weights), by = by) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, gear), by = cyl) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(by, p75), by = statistic) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, cyl), by = gear) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(by, statistic), by = p75) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, gear), by = cyl) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(by, p75), by = statistic) |> dplyr::select(stat)
)

# rename vars
mtcars2 <- mtcars %>%
dplyr::rename("mean" = mpg, "sd" = cyl, "var" = disp, "sum" = gear)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, cyl), by = disp) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(mean, sd), by = var) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(cyl, disp), by = mpg) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(sd, var), by = mean) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, disp), by = cyl) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(mean, var), by = sd) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, cyl), by = gear) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(mean, sd), by = sum) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, gear), by = cyl) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(mean, sum), by = sd) |> dplyr::select(stat)
)

# rename vars
mtcars2 <- mtcars %>%
dplyr::rename("deff" = mpg, "min" = cyl, "max" = disp, "mean.std.error" = gear)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, cyl), by = disp) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(deff, min), by = max) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(cyl, disp), by = mpg) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(min, max), by = deff) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, disp), by = cyl) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(deff, max), by = min) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, cyl), by = gear) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(deff, min), by = mean.std.error) |> dplyr::select(stat)
)

expect_equal(
ard_continuous(mtcars, variables = c(mpg, gear), by = cyl) |> dplyr::select(stat),
ard_continuous(mtcars2, variables = c(deff, mean.std.error), by = min) |> dplyr::select(stat)
)
})

0 comments on commit 0586777

Please sign in to comment.