Skip to content

Commit

Permalink
test: clean up epix_fill_through_version
Browse files Browse the repository at this point in the history
  • Loading branch information
dshemetov committed Apr 13, 2024
1 parent d479882 commit e09fd7f
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions tests/testthat/test-epix_fill_through_version.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ test_that("epix_fill_through_version mirrors input when it is sufficiently up to
ea_trivial_fill_na1 <- epix_fill_through_version(ea_orig, some_earlier_observed_version, "na")
ea_trivial_fill_na2 <- epix_fill_through_version(ea_orig, ea_orig$versions_end, "na")
ea_trivial_fill_locf <- epix_fill_through_version(ea_orig, some_earlier_observed_version, "locf")
# Below, we want R6 objects to be compared based on contents rather than
# addresses. We appear to get this with `expect_identical` in `testthat`
# edition 3, which is based on `waldo::compare` rather than `base::identical`;
# `waldo::compare` in waldo >=0.3.1 appears (as of 0.4.0) to compare R6
# objects by contents rather than address (in a way that is tested but maybe
# not guaranteed via user docs). Use `testthat::local_edition` to ensure we
# use testthat edition 3 here (use `testthat::` to prevent ambiguity with
# `readr`).
testthat::local_edition(3)

expect_identical(ea_orig, ea_trivial_fill_na1)
expect_identical(ea_orig, ea_trivial_fill_na2)
expect_identical(ea_orig, ea_trivial_fill_locf)
Expand All @@ -33,9 +25,6 @@ test_that("epix_fill_through_version can extend observed versions, gives expecte
ea_fill_na <- epix_fill_through_version(ea_orig, later_unobserved_version, "na")
ea_fill_locf <- epix_fill_through_version(ea_orig, later_unobserved_version, "locf")

# We use testthat edition 3 features here, passing `ignore_attr` to
# `waldo::compare`. Ensure we are using edition 3:
testthat::local_edition(3)
withCallingHandlers(
{
expect_identical(ea_fill_na$versions_end, later_unobserved_version)
Expand Down Expand Up @@ -69,23 +58,16 @@ test_that("epix_fill_through_version does not mutate x", {
# doesn't seem sufficient to trigger)
as_epi_archive(tibble::tibble(geo_value = 1L, time_value = 1L, version = 1L, value = 10L))
)) {
# We want to perform a strict comparison of the contents of `ea_orig` before
# and `ea_orig` after. `copy` + `expect_identical` based on waldo would
# sort of work, but we might want something stricter. `as.list` +
# `identical` plus a check of the DT seems to do the trick.
ea_orig_before_as_list <- as.list(ea_orig)
ea_orig_before <- clone(ea_orig)
ea_orig_dt_before_copy <- data.table::copy(ea_orig$DT)
some_unobserved_version <- 8L
#

ea_fill_na <- epix_fill_through_version(ea_orig, some_unobserved_version, "na")
ea_orig_after_as_list <- as.list(ea_orig)
# use identical, not expect_identical, for the R6-as-list test; latter isn't as strict
expect_true(identical(ea_orig_before_as_list, ea_orig_after_as_list))
expect_identical(ea_orig_before, ea_orig)
expect_identical(ea_orig_dt_before_copy, ea_orig$DT)
#

ea_fill_locf <- epix_fill_through_version(ea_orig, some_unobserved_version, "locf")
ea_orig_after_as_list <- as.list(ea_orig)
expect_true(identical(ea_orig_before_as_list, ea_orig_after_as_list))
expect_identical(ea_orig_before, ea_orig)
expect_identical(ea_orig_dt_before_copy, ea_orig$DT)
}
})
Expand All @@ -101,8 +83,7 @@ test_that("epix_fill_through_version return with expected visibility", {

test_that("epix_fill_through_version returns same key & doesn't mutate old DT or its key", {
ea <- as_epi_archive(tibble::tibble(geo_value = 1L, time_value = 1L, version = 1L, value = 10L))
old_dt <- ea$DT
old_dt_copy <- data.table::copy(old_dt)
old_dt_copy <- data.table::copy(ea$DT)
old_key <- data.table::key(ea$DT)
expect_identical(data.table::key(epix_fill_through_version(ea, 5L, "na")$DT), old_key)
expect_identical(data.table::key(epix_fill_through_version(ea, 5L, "locf")$DT), old_key)
Expand Down

0 comments on commit e09fd7f

Please sign in to comment.