diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 21b8a93..4e0ec42 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -13,6 +13,7 @@ jobs: runs-on: ubuntu-latest env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} steps: - uses: actions/checkout@v4 diff --git a/DESCRIPTION b/DESCRIPTION index 0deb298..7d8b973 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/R/cod_recode_sex.R b/R/cod_recode_sex.R index 467d4b5..ee99517 100644 --- a/R/cod_recode_sex.R +++ b/R/cod_recode_sex.R @@ -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 { diff --git a/README.Rmd b/README.Rmd index 80ca068..2dbece5 100644 --- a/README.Rmd +++ b/README.Rmd @@ -20,6 +20,7 @@ knitr::opts_chunk$set( [![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) [![R-CMD-check](https://github.com/OxfordIHTM/codeditr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/OxfordIHTM/codeditr/actions/workflows/R-CMD-check.yaml) [![Codecov test coverage](https://codecov.io/gh/OxfordIHTM/codeditr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/OxfordIHTM/codeditr?branch=main) +[![CodeFactor](https://www.codefactor.io/repository/github/OxfordIHTM/codeditr/badge)](https://www.codefactor.io/repository/github/OxfordIHTM/codeditr) The [World Health Organization](https://www.who.int/)'s [CoDEdit electronic tool](https://www.who.int/standards/classifications/classification-of-diseases/services/codedit-tool) is intended to help producers of cause-of-death statistics in strengthening their capacity to perform routine checks on their data. This package ports the original tool built using Microsoft Access into R so as to leverage the utility and function of the original tool into a usable application program interface that can be used for building more universal tools or for creating programmatic scientific workflows aimed at routine, automated, and large-scale monitoring of cause-of-death data. diff --git a/README.md b/README.md index 7b729f8..b7778ec 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](h [![R-CMD-check](https://github.com/OxfordIHTM/codeditr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/OxfordIHTM/codeditr/actions/workflows/R-CMD-check.yaml) [![Codecov test coverage](https://codecov.io/gh/OxfordIHTM/codeditr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/OxfordIHTM/codeditr?branch=main) +[![CodeFactor](https://www.codefactor.io/repository/github/OxfordIHTM/codeditr/badge)](https://www.codefactor.io/repository/github/OxfordIHTM/codeditr) The [World Health Organization](https://www.who.int/)’s [CoDEdit @@ -69,17 +70,17 @@ follows: ``` r citation("codeditr") -#> To cite zscorer in publications use: +#> To cite codeditr in publications use: #> -#> Anita Makori and Ernest Guevarra (2024). codeditr: Implementing Basic -#> Checks on Cause-of-Death Data Based on World Health Organization's -#> CoDEdit Tool. R package version 0.0.9000. URL -#> https://oxford-ihtm.io/codeditr/ +#> Anita Makori, Ernest Guevarra (2024). _codeditr: Implementing +#> Cause-of-Death Data Checks Based on World Health Organization's +#> CoDEdit Tool_. R package version 0.0.9000, +#> . #> #> A BibTeX entry for LaTeX users is #> #> @Manual{, -#> title = {codeditr: Implementing Basic Checks on Cause-of-Death Data Based on World Health Organization's CoDEdit Tool}, +#> title = {codeditr: Implementing Cause-of-Death Data Checks Based on World Health Organization's CoDEdit Tool}, #> author = {{Anita Makori} and {Ernest Guevarra}}, #> year = {2024}, #> note = {R package version 0.0.9000}, diff --git a/man/cod_recode_sex.Rd b/man/cod_recode_sex.Rd index 65db2e5..426e8f9 100644 --- a/man/cod_recode_sex.Rd +++ b/man/cod_recode_sex.Rd @@ -4,7 +4,7 @@ \alias{cod_recode_sex} \title{Recode sex value of cause of death data based on CoDEdit rules} \usage{ -cod_recode_sex(sex_value, sex_code = c(1, 2), codedit = TRUE) +cod_recode_sex(sex_value, sex_code = c(1L, 2L), codedit = TRUE) } \arguments{ \item{sex_value}{A character or integer value or vector of values signifying diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..0af9989 --- /dev/null +++ b/tests/testthat.R @@ -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") diff --git a/tests/testthat/test-cod_recode_age_type.R b/tests/testthat/test-cod_recode_age_type.R new file mode 100644 index 0000000..443f5d0 --- /dev/null +++ b/tests/testthat/test-cod_recode_age_type.R @@ -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 + ) + } +) diff --git a/tests/testthat/test-cod_recode_sex.R b/tests/testthat/test-cod_recode_sex.R new file mode 100644 index 0000000..d1e5a53 --- /dev/null +++ b/tests/testthat/test-cod_recode_sex.R @@ -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 + ) + } +)