From e0620aec83bc9410406793cc0cf531f10b15d6fb Mon Sep 17 00:00:00 2001 From: Tina Fu Date: Fri, 5 Jan 2024 14:36:20 +0000 Subject: [PATCH 1/4] fix tests for age_from_chi --- tests/testthat/test-dob_from_chi.R | 43 +++++++++++++----------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/tests/testthat/test-dob_from_chi.R b/tests/testthat/test-dob_from_chi.R index 0645a54..5b770c3 100644 --- a/tests/testthat/test-dob_from_chi.R +++ b/tests/testthat/test-dob_from_chi.R @@ -10,18 +10,6 @@ gen_real_chi <- function(first_6) { } } -# A helper function to work out ages as time passes: Given the age at a given -# date, work out what the age would be today. "2022-04-01" was chosen as the -# default arbitrarily. -expected_age <- function( - expected_ages, - expected_at = lubridate::make_date(year = 2022, month = 4, day = 1)) { - expected_ages + floor(lubridate::time_length( - lubridate::interval(expected_at, Sys.Date()), - "years" - )) -} - test_that("Returns correct DoB - no options", { # Some standard CHIs / dates expect_equal( @@ -241,15 +229,16 @@ test_that("dob_from_chi gives messages when returning NA", { ) }) -test_that("Returns correct age - no options", { +test_that("Returns correct age - no options except fixed reference date", { # Some standard CHIs expect_equal( age_from_chi(c( "0101336489", "0101405073", "0101625707" - )), - expected_age(c(89, 82, 60)) + ), + ref_date = as.Date("2023-11-01")), + c(90, 83, 61) ) # Leap years @@ -258,18 +247,20 @@ test_that("Returns correct age - no options", { gen_real_chi(290228), gen_real_chi(290236), gen_real_chi(290296) - )), - expected_age(c(94, 86, 26)) + ), + ref_date = as.Date("2023-03-01")), + c(95, 87, 27) ) # Century leap year (hard to test as 1900 is a long time ago!) expect_equal( - age_from_chi(gen_real_chi(290200)), - expected_age(22) + age_from_chi(gen_real_chi(290200), + ref_date = as.Date("2023-03-01")), + 23 ) }) -test_that("Returns correct age - fixed age supplied", { +test_that("Returns correct age - fixed age and reference date supplied", { # Some standard CHIs # Fixed min age e.g. All patients are younger than X expect_equal( @@ -280,13 +271,14 @@ test_that("Returns correct age - fixed age supplied", { "0101625707" ), min_age = 1, - max_age = 101 + max_age = 101, + ref_date = as.Date("2023-11-01") ), - expected_age(c(89, 82, 60)) + c(90, 83, 61) ) }) -test_that("Returns correct age - unusual fixed age", { +test_that("Returns correct age - unusual fixed age with fixed reference date", { # Some standard CHIs expect_equal( suppressMessages( @@ -296,10 +288,11 @@ test_that("Returns correct age - unusual fixed age", { "0101405073", "0101625707" ), - max_age = 72 + max_age = 72, + ref_date = as.Date("2023-11-01") ) ), - expected_age(c(NA_real_, NA_real_, 60)) + c(NA_real_, NA_real_, 61) ) }) From b8c09908b1fc5859d69badca4b09b083de41c45d Mon Sep 17 00:00:00 2001 From: Tina815 Date: Fri, 5 Jan 2024 14:40:32 +0000 Subject: [PATCH 2/4] Style code --- tests/testthat/test-dob_from_chi.R | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/testthat/test-dob_from_chi.R b/tests/testthat/test-dob_from_chi.R index 5b770c3..888dd91 100644 --- a/tests/testthat/test-dob_from_chi.R +++ b/tests/testthat/test-dob_from_chi.R @@ -232,30 +232,35 @@ test_that("dob_from_chi gives messages when returning NA", { test_that("Returns correct age - no options except fixed reference date", { # Some standard CHIs expect_equal( - age_from_chi(c( - "0101336489", - "0101405073", - "0101625707" + age_from_chi( + c( + "0101336489", + "0101405073", + "0101625707" + ), + ref_date = as.Date("2023-11-01") ), - ref_date = as.Date("2023-11-01")), c(90, 83, 61) ) # Leap years expect_equal( - age_from_chi(c( - gen_real_chi(290228), - gen_real_chi(290236), - gen_real_chi(290296) + age_from_chi( + c( + gen_real_chi(290228), + gen_real_chi(290236), + gen_real_chi(290296) + ), + ref_date = as.Date("2023-03-01") ), - ref_date = as.Date("2023-03-01")), c(95, 87, 27) ) # Century leap year (hard to test as 1900 is a long time ago!) expect_equal( age_from_chi(gen_real_chi(290200), - ref_date = as.Date("2023-03-01")), + ref_date = as.Date("2023-03-01") + ), 23 ) }) From ca0bc7116850d7b90909e86a4ebd0664ad24b6d5 Mon Sep 17 00:00:00 2001 From: Tina Fu Date: Fri, 5 Jan 2024 15:08:19 +0000 Subject: [PATCH 3/4] increment release version --- DESCRIPTION | 2 +- NEWS.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8146c1a..d8dbd33 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: phsmethods Title: Standard Methods for Use in Public Health Scotland -Version: 1.0.1 +Version: 1.0.2 Authors@R: c( person("Public Health Scotland", , , "phs.datascience@phs.scot", role = "cph"), person("David", "Caldwell", , "David.Caldwell@phs.scot", role = "aut"), diff --git a/NEWS.md b/NEWS.md index facf535..ce6ca41 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# phsmethods 1.0.2 (2024-01-05) + +- Fix some tests for `age_from_chi()` due to a helper function `expected_age()` +caused the tests fail when the new year comes. It has been replaced with a fixed +reference date. + # phsmethods 1.0.1 (2023-11-27) - Fix a bug in `extract_fin_year()` to make sure financial years are displayed From f5b8be2c1078a9d49100ff92124c483937745867 Mon Sep 17 00:00:00 2001 From: Tina Fu Date: Fri, 5 Jan 2024 15:17:24 +0000 Subject: [PATCH 4/4] update NEWS --- NEWS.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index ce6ca41..8192040 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,8 @@ # phsmethods 1.0.2 (2024-01-05) -- Fix some tests for `age_from_chi()` due to a helper function `expected_age()` -caused the tests fail when the new year comes. It has been replaced with a fixed -reference date. +- No user-facing changes. Fix some tests for `age_from_chi()` due to a helper +function `expected_age()` caused the tests fail when the new year comes. It has +been replaced with a fixed reference date. # phsmethods 1.0.1 (2023-11-27)