Skip to content

Commit

Permalink
add tests for recode sex and recode age type
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra committed Apr 28, 2024
1 parent 600707d commit f645441
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 3 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ Imports:
tibble
Suggests:
covr,
spelling
spelling,
testthat (>= 3.0.0)
Encoding: UTF-8
Language: en-GB
LazyData: true
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
2 changes: 1 addition & 1 deletion R/cod_recode_sex.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @export
#'

cod_recode_sex <- function(sex_value, sex_code = c(1, 2), codedit = TRUE) {
cod_recode_sex <- function(sex_value, sex_code = c(1L, 2L), codedit = TRUE) {
if (codedit) {
sex_value[!sex_value %in% sex_code] <- 9L
} else {
Expand Down
2 changes: 1 addition & 1 deletion man/cod_recode_sex.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(codeditr)

test_check("codeditr")
97 changes: 97 additions & 0 deletions tests/testthat/test-cod_recode_age_type.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Tests for recode age type ----------------------------------------------------

age_type <- c(rep("D", 3), rep("M", 2), rep("Y", 3))

age_type1 <- c(rep("d", 3), rep("m", 2), rep("y", 3))
age_type_code1 <- c("d", "m", "y")

age_type2 <- c(rep("days", 3), rep("months", 2), rep("years", 3))
age_type_code2 <- c("days", "months", "years")

age_type3 <- c(rep(1L, 3), rep(2L, 2), rep(3L, 3))
age_type_code3 <- c(1L, 2L, 3L)

age_type4 <- c(rep("d", 3), "m", NA_character_, rep("y", 3))
age_type_code4 <- c("d", "m", "y")

age_type5 <- c(rep(1L, 3), 2L, NA_integer_, rep(3L, 3))
age_type_code5 <- c(1L, 2L, 3L)

expected <- c(rep("D", 3), rep("M", 2), rep("Y", 3))
expected_na <- c(rep("D", 3), "M", NA_character_, rep("Y", 3))


testthat::test_that(
"output of recode age type is as expected", {
expect_vector(
cod_recode_age_type(age_type = age_type),
ptype = character(),
size = 8
)

expect_equal(
cod_recode_age_type(age_type = age_type),
expected
)

## input is different characters ----
expect_vector(
cod_recode_age_type(age_type = age_type1, age_type_code = age_type_code1),
ptype = character(),
size = 8
)

expect_equal(
cod_recode_age_type(age_type = age_type1, age_type_code = age_type_code1),
expected
)

## input is different characters ----
expect_vector(
cod_recode_age_type(age_type = age_type2, age_type_code = age_type_code2),
ptype = character(),
size = 8
)

expect_equal(
cod_recode_age_type(age_type = age_type2, age_type_code = age_type_code2),
expected
)

## input is integer ----
expect_vector(
cod_recode_age_type(age_type = age_type3, age_type_code = age_type_code3),
ptype = character(),
size = 8
)

expect_equal(
cod_recode_age_type(age_type = age_type3, age_type_code = age_type_code3),
expected
)

## input has NA ----
expect_vector(
cod_recode_age_type(age_type = age_type4, age_type_code = age_type_code4),
ptype = character(),
size = 8
)

expect_equal(
cod_recode_age_type(age_type = age_type4, age_type_code = age_type_code4),
expected_na
)

## input has NA ----
expect_vector(
cod_recode_age_type(age_type = age_type5, age_type_code = age_type_code5),
ptype = character(),
size = 8
)

expect_equal(
cod_recode_age_type(age_type = age_type5, age_type_code = age_type_code5),
expected_na
)
}
)
47 changes: 47 additions & 0 deletions tests/testthat/test-cod_recode_sex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Tests for recode sex ---------------------------------------------------------

sex_value <- c(rep(1L, 2), rep(2L, 3), NA_integer_)

sex_value1 <- c(rep("m", 2), rep("f", 3), NA_character_)
sex_code1 <- c("m", "f")

expected <- c(1L, 1L, 2L, 2L, 2L, NA_integer_)
expected_codedit <- c(1L, 1L, 2L, 2L, 2L, 9L)

testthat::test_that(
"output of recode sex is as expected", {
expect_vector(
cod_recode_sex(sex_value = sex_value), ptype = integer(), size = 6
)

expect_equal(cod_recode_sex(sex_value = sex_value), expected_codedit)

expect_vector(
cod_recode_sex(
sex_value = sex_value1, sex_code = sex_code1, codedit = FALSE
),
ptype = integer(),
size = 6
)

expect_equal(
cod_recode_sex(
sex_value = sex_value1, sex_code = sex_code1, codedit = FALSE
),
expected
)

expect_vector(
cod_recode_sex(sex_value = sex_value1, sex_code = sex_code1),
ptype = integer(),
size = 6
)

expect_equal(
cod_recode_sex(
sex_value = sex_value1, sex_code = sex_code1, codedit = TRUE
),
expected_codedit
)
}
)

0 comments on commit f645441

Please sign in to comment.