Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #2495 adds unit test for restricted_imputed_dtc_dt() #2528

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<details>
<summary>Developer Notes</summary>

- Created unit tests for developer internal function `restricted_imputed_dtc_dt()` (#2495)
- Adopted `data-raw/data` R Package Convention (#2427)

</details>
Expand Down
112 changes: 89 additions & 23 deletions tests/testthat/test-derive_vars_dt.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,72 @@ test_that("compute_dtf Test 13: compute DTF", {
)
})

# restrict_imputed_dtc_dt ----
## Test 14: restrict_imputed_dtc_dt works as expected ----
test_that("restrict_imputed_dtc_dt Test 14: restrict_imputed_dtc_dt works as expected", {
imputed_dtc <- impute_dtc_dt(
input,
min_dates = list(
c(
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06")
),
c(
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06")
)
),
highest_imputation = "Y",
date_imputation = "first"
)
restricted <- restrict_imputed_dtc_dt(
input,
imputed_dtc = imputed_dtc,
min_dates = list(
c(
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06")
),
c(
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06")
)
),
max_dates = list(
c(
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06"),
ymd("2019-07-06")
),
c(
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06"),
ymd("2019-06-06")
)
)
)
expect_equal(
restricted,
c("2019-07-18", "2019-06-06", "2019-06-06", "2019-07-06", "2019-06-06")
)
})

# derive_vars_dt ----

date <- tibble::tribble(
Expand All @@ -303,8 +369,8 @@ date <- tibble::tribble(
"2019---07"
)

## Test 14: default behavior ----
test_that("derive_vars_dt Test 14: default behavior", {
## Test 15: default behavior ----
test_that("derive_vars_dt Test 15: default behavior", {
expected_output <- tibble::tribble(
~XXSTDTC, ~ASTDT,
"2019-07-18T15:25:40", as.Date("2019-07-18"),
Expand All @@ -327,8 +393,8 @@ test_that("derive_vars_dt Test 14: default behavior", {
)
})

## Test 15: no date imputation, add DTF ----
test_that("derive_vars_dt Test 15: no date imputation, add DTF", {
## Test 16: no date imputation, add DTF ----
test_that("derive_vars_dt Test 16: no date imputation, add DTF", {
expected_output <- tibble::tribble(
~XXSTDTC, ~ASTDT, ~ASTDTF,
"2019-07-18T15:25:40", as.Date("2019-07-18"), NA_character_,
Expand All @@ -352,8 +418,8 @@ test_that("derive_vars_dt Test 15: no date imputation, add DTF", {
)
})

## Test 16: date imputed to first, auto DTF ----
test_that("derive_vars_dt Test 16: date imputed to first, auto DTF", {
## Test 17: date imputed to first, auto DTF ----
test_that("derive_vars_dt Test 17: date imputed to first, auto DTF", {
expected_output <- tibble::tribble(
~XXSTDTC, ~ASTDT, ~ASTDTF,
"2019-07-18T15:25:40", as.Date("2019-07-18"), NA_character_,
Expand All @@ -378,8 +444,8 @@ test_that("derive_vars_dt Test 16: date imputed to first, auto DTF", {
)
})

## Test 17: date imputed to last, no DTF ----
test_that("derive_vars_dt Test 17: date imputed to last, no DTF", {
## Test 18: date imputed to last, no DTF ----
test_that("derive_vars_dt Test 18: date imputed to last, no DTF", {
expected_output <- tibble::tribble(
~XXSTDTC, ~AENDT,
"2019-07-18T15:25:40", as.Date("2019-07-18"),
Expand All @@ -405,8 +471,8 @@ test_that("derive_vars_dt Test 17: date imputed to last, no DTF", {
)
})

## Test 18: NA imputation for highest_imputation = Y & max_dates ----
test_that("derive_vars_dt Test 18: NA imputation for highest_imputation = Y & max_dates", {
## Test 19: NA imputation for highest_imputation = Y & max_dates ----
test_that("derive_vars_dt Test 19: NA imputation for highest_imputation = Y & max_dates", {
actual <- data.frame(
AESTDTC = c(NA_character_, NA_character_),
TRTSDT = c(ymd("2022-01-01"), NA)
Expand All @@ -431,8 +497,8 @@ test_that("derive_vars_dt Test 18: NA imputation for highest_imputation = Y & ma
expect_dfs_equal(actual, expected, keys = c("ASTDT", "ASTDTF"))
})

## Test 19: NA imputation for highest_imputation = Y & max_dates but date_imputation = first ----
test_that("derive_vars_dt Test 19: NA imputation for highest_imputation = Y & max_dates but date_imputation = first", { # nolint
## Test 20: NA imputation for highest_imputation = Y & max_dates but date_imputation = first ----
test_that("derive_vars_dt Test 20: NA imputation for highest_imputation = Y & max_dates but date_imputation = first", { # nolint
expect_snapshot(
data.frame(
AESTDTC = c(NA_character_, NA_character_),
Expand All @@ -450,8 +516,8 @@ test_that("derive_vars_dt Test 19: NA imputation for highest_imputation = Y & ma
)
})

## Test 20: NA imputation for highest_imputation = Y & min_dates ----
test_that("derive_vars_dt Test 20: NA imputation for highest_imputation = Y & min_dates", {
## Test 21: NA imputation for highest_imputation = Y & min_dates ----
test_that("derive_vars_dt Test 21: NA imputation for highest_imputation = Y & min_dates", {
actual <- data.frame(
AESTDTC = c(NA_character_, NA_character_),
TRTSDT = c(ymd("2022-01-01"), NA)
Expand All @@ -476,8 +542,8 @@ test_that("derive_vars_dt Test 20: NA imputation for highest_imputation = Y & mi
expect_dfs_equal(actual, expected, keys = c("ASTDT", "ASTDTF"))
})

## Test 21: NA imputation for highest_imputation = Y & min_dates but date_imputation = last ----
test_that("derive_vars_dt Test 21: NA imputation for highest_imputation = Y & min_dates but date_imputation = last", { # nolint
## Test 22: NA imputation for highest_imputation = Y & min_dates but date_imputation = last ----
test_that("derive_vars_dt Test 22: NA imputation for highest_imputation = Y & min_dates but date_imputation = last", { # nolint
expect_snapshot(
data.frame(
AESTDTC = c(NA_character_, NA_character_),
Expand All @@ -495,8 +561,8 @@ test_that("derive_vars_dt Test 21: NA imputation for highest_imputation = Y & mi
)
})

## Test 22: NA imputation for highest_imputation = Y but null min/max dates fails ----
test_that("derive_vars_dt Test 22: NA imputation for highest_imputation = Y but null min/max dates fails", { # nolint
## Test 23: NA imputation for highest_imputation = Y but null min/max dates fails ----
test_that("derive_vars_dt Test 23: NA imputation for highest_imputation = Y but null min/max dates fails", { # nolint
expect_snapshot(
data.frame(
AESTDTC = c(NA_character_, NA_character_),
Expand All @@ -514,8 +580,8 @@ test_that("derive_vars_dt Test 22: NA imputation for highest_imputation = Y but
)
})

## Test 23: Supplying both min/max dates for highest_imputation = Y works ----
test_that("derive_vars_dt Test 23: Supplying both min/max dates for highest_imputation = Y works", { # nolint
## Test 24: Supplying both min/max dates for highest_imputation = Y works ----
test_that("derive_vars_dt Test 24: Supplying both min/max dates for highest_imputation = Y works", { # nolint
actual <- data.frame(
AESTDTC = c(NA_character_, NA_character_),
TRTSDT = c(ymd("2022-01-01"), NA),
Expand All @@ -541,8 +607,8 @@ test_that("derive_vars_dt Test 23: Supplying both min/max dates for highest_impu
expect_dfs_equal(actual, expected, keys = c("ASTDT", "ASTDTF"))
})

## Test 24: Supplying both min/max dates for highest_imputation = Y works ----
test_that("derive_vars_dt Test 24: Supplying both min/max dates for highest_imputation = Y works", { # nolint
## Test 25: Supplying both min/max dates for highest_imputation = Y works ----
test_that("derive_vars_dt Test 25: Supplying both min/max dates for highest_imputation = Y works", { # nolint
actual <- data.frame(
AESTDTC = c(NA_character_, NA_character_),
TRTSDT = c(ymd("2022-01-01"), NA),
Expand All @@ -569,4 +635,4 @@ test_that("derive_vars_dt Test 24: Supplying both min/max dates for highest_impu
expect_dfs_equal(actual, expected, keys = c("ASTDT", "ASTDTF"))
})

rm(input, input_warnings)
rm(input, input_warnings, inputdt, inputdtc, date)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add issue in our Discussion to look at a way to adopt this in all our testing scripts? maybe it is already there!

Loading