stRoke/tests/testthat/test-age_calc.R

66 lines
1.9 KiB
R
Raw Normal View History

2022-09-22 14:20:46 +02:00
test_that("age_calc works for vectors of length 1 (scalars)", {
2023-01-12 13:44:29 +01:00
result <- age_calc(as.Date("1945-10-23"), as.Date("2018-09-30"))
2022-09-22 14:20:46 +02:00
expect_equal(round(result), 73)
})
2022-09-22 14:47:33 +02:00
################################################################################
# Unit Test - gpttools
test_that("age_calc works correctly for years", {
2023-01-12 13:44:29 +01:00
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", {
2023-01-12 13:44:29 +01:00
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 works correctly for months", {
2023-01-12 13:44:29 +01:00
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", {
2023-01-12 13:44:29 +01:00
expect_equal(round(age_calc(
as.Date("2000-07-07"), as.Date("2020-01-01"), units = "months"
)), 234)
2023-01-11 12:54:08 +01:00
})
test_that("age_calc works correctly for days", {
2023-01-12 13:44:29 +01:00
expect_equal(age_calc(as.Date("2000-01-01"), as.Date("2020-01-01"),
units = "days"), 7305)
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", {
2023-01-12 13:44:29 +01:00
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", {
2023-01-12 13:44:29 +01:00
expect_equal(age_calc(
as.Date("2000-01-01"),
as.Date("2014-05-11"),
precise = FALSE,
units = "years"
),
14)
})
2023-01-11 12:54:08 +01:00
test_that("age_calc throws an error when wrong unit", {
2023-01-12 13:44:29 +01:00
expect_error(age_calc(as.Date("2020-01-01"), as.Date("2000-01-01"),
units = "hours"))
})
test_that("age_calc throws an error when wrong format", {
expect_error(age_calc("2020-01-01", as.Date("2000-01-01"), units = "hours"))
2023-01-11 12:54:08 +01:00
})