From 113121fdf5ac4119bf387db63948496bbb2378c7 Mon Sep 17 00:00:00 2001 From: "Logan C. Brooks" Date: Fri, 12 Jan 2024 19:32:15 -0800 Subject: [PATCH] Add grouped epi_df names<- test, spruce up related tests --- tests/testthat/test-epi_df.R | 11 +++++------ tests/testthat/test-methods-epi_df.R | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-epi_df.R b/tests/testthat/test-epi_df.R index fbd31f7b..decd6fd7 100644 --- a/tests/testthat/test-epi_df.R +++ b/tests/testthat/test-epi_df.R @@ -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", { @@ -91,8 +92,7 @@ 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) @@ -100,8 +100,7 @@ test_that("grouped epi_df handles extra keys correctly", { 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) )) diff --git a/tests/testthat/test-methods-epi_df.R b/tests/testthat/test-methods-epi_df.R index c9e38fff..6be7e89b 100644 --- a/tests/testthat/test-methods-epi_df.R +++ b/tests/testthat/test-methods-epi_df.R @@ -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) +})