Skip to content

Commit

Permalink
Add grouped epi_df names<- test, spruce up related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brookslogan committed Jan 13, 2024
1 parent 9cc3437 commit 113121f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 5 additions & 6 deletions tests/testthat/test-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,20 @@ epi_tib <- epiprocess::new_epi_df(tib)
test_that("grouped epi_df maintains type for select", {
grouped_epi <- epi_tib %>% group_by(geo_value)
selected_df <- grouped_epi %>% select(-y)
expect_true("epi_df" %in% class(selected_df))
expect_true(inherits(selected_df, "epi_df"))
# make sure that the attributes are right
epi_attr <- attributes(selected_df)
expect_identical(epi_attr$names, c("geo_value", "time_value", "x"))
expect_identical(epi_attr$row.names, seq(1, 10))
expect_identical(epi_attr$groups, attributes(grouped_epi)$groups)
expect_identical(epi_attr$metadata, attributes(epi_tib)$metadata)
expect_identical(selected_df, epi_tib %>% select(-y) %>% group_by(geo_value))
})

test_that("grouped epi_df drops type when dropping keys", {
grouped_epi <- epi_tib %>% group_by(geo_value)
selected_df <- grouped_epi %>% select(geo_value)
expect_true(!("epi_df" %in% class(selected_df)))
expect_true(!inherits(selected_df, "epi_df"))
})

test_that("grouped epi_df handles extra keys correctly", {
Expand All @@ -91,17 +92,15 @@ test_that("grouped epi_df handles extra keys correctly", {
attributes(epi_tib)
grouped_epi <- epi_tib %>% group_by(geo_value)
selected_df <- grouped_epi %>% select(-extra_key)
selected_df
expect_true("epi_df" %in% class(selected_df))
expect_true(inherits(selected_df, "epi_df"))
# make sure that the attributes are right
old_attr <- attributes(epi_tib)
epi_attr <- attributes(selected_df)
expect_identical(epi_attr$names, c("geo_value", "time_value", "x", "y"))
expect_identical(epi_attr$row.names, seq(1, 10))
expect_identical(epi_attr$groups, attributes(grouped_epi)$groups)
expect_identical(epi_attr$metadata, list(
geo_type = "state", time_type =
"day",
geo_type = "state", time_type = "day",
as_of = old_attr$metadata$as_of,
other_keys = character(0)
))
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-methods-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,20 @@ test_that("Renaming columns gives appropriate colnames and metadata", {
select(geo_value, time_value, age_group = age, value)
expect_identical(renamed_edf1, renamed_edf2)
})

test_that("Renaming columns while grouped gives appropriate colnames and metadata", {
gedf <- tibble::tibble(geo_value = 1, time_value = 1, age = 1, value = 1) %>%
as_epi_df(additional_metadata = list(other_keys = "age")) %>%
group_by(geo_value)
renamed_gedf1 <- gedf %>%
`[`(c("geo_value", "time_value", "age", "value")) %>%
`names<-`(c("geo_value", "time_value", "age_group", "value"))
expect_true(inherits(renamed_gedf1, "epi_df"))
expect_true(inherits(renamed_gedf1, "grouped_df"))
expect_identical(names(renamed_gedf1), c("geo_value", "time_value", "age_group", "value"))
expect_identical(attr(renamed_gedf1, "metadata")$other_keys, c("age_group"))
renamed_gedf2 <- gedf %>%
as_epi_df(additional_metadata = list(other_keys = "age")) %>%
select(geo_value, time_value, age_group = age, value)
expect_identical(renamed_gedf1, renamed_gedf2)
})

0 comments on commit 113121f

Please sign in to comment.