2022-09-22 14:20:46 +02:00
|
|
|
test_that("age_calc works for vectors of length 1 (scalars)", {
|
|
|
|
result<-age_calc(as.Date("1945-10-23"),as.Date("2018-09-30"))
|
|
|
|
expect_equal(round(result), 73)
|
|
|
|
})
|
2022-09-22 14:47:33 +02:00
|
|
|
|
2023-01-04 14:33:48 +01:00
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# Unit Test - gpttools
|
|
|
|
|
|
|
|
test_that("age_calc works correctly for years", {
|
|
|
|
expect_equal(age_calc(as.Date("2000-01-01"), as.Date("2020-01-01"), units = "years"), 20)
|
|
|
|
})
|
|
|
|
|
2023-01-11 12:54:08 +01:00
|
|
|
test_that("age_calc gives error if enddate < dob", {
|
|
|
|
expect_error(age_calc(as.Date("2020-01-01"), as.Date("2000-01-01"), units = "years"))
|
|
|
|
})
|
|
|
|
|
2023-01-04 14:33:48 +01:00
|
|
|
test_that("age_calc works correctly for months", {
|
|
|
|
expect_equal(age_calc(as.Date("2000-01-01"), as.Date("2020-01-01"), units = "months"), 240)
|
|
|
|
})
|
|
|
|
|
2023-01-11 12:54:08 +01:00
|
|
|
test_that("age_calc works correctly for months", {
|
|
|
|
expect_equal(round(age_calc(as.Date("2000-07-07"), as.Date("2020-01-01"), units = "months")), 234)
|
|
|
|
})
|
|
|
|
|
2023-01-04 14:33:48 +01:00
|
|
|
test_that("age_calc works correctly for days", {
|
|
|
|
testthat::expect_equal(age_calc(as.Date("2000-01-01"), as.Date("2020-01-01"), units = "days"), 7305)
|
|
|
|
testthat::expect_length(age_calc(as.Date("2000-01-01"), as.Date("2020-01-01"), units = "days"), 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test_that("age_calc works correctly with leap years and precise set to TRUE", {
|
|
|
|
expect_equal(age_calc(as.Date("2000-02-29"), as.Date("2020-02-29"), units = "years", precise = TRUE), 20)
|
|
|
|
})
|
|
|
|
|
|
|
|
test_that("age_calc throws an error when enddate is before dob", {
|
|
|
|
expect_error(age_calc(as.Date("2020-01-01"), as.Date("2000-01-01"), units = "years"))
|
|
|
|
})
|
2023-01-11 12:54:08 +01:00
|
|
|
|
|
|
|
test_that("age_calc throws an error when wrong unit", {
|
|
|
|
expect_error(age_calc(as.Date("2020-01-01"), as.Date("2000-01-01"), units = "hours"))
|
|
|
|
})
|