11test_that(" new_epi_df works as intended" , {
22 # Empty tibble
3- wmsg = capture_warnings(a <- new_epi_df())
4- expect_match(wmsg [1 ],
5- " Unknown or uninitialised column: `geo_value`." )
6- expect_match(wmsg [2 ],
7- " Unknown or uninitialised column: `time_value`." )
3+ wmsg <- capture_warnings(a <- new_epi_df())
4+ expect_match(
5+ wmsg [1 ],
6+ " Unknown or uninitialised column: `geo_value`."
7+ )
8+ expect_match(
9+ wmsg [2 ],
10+ " Unknown or uninitialised column: `time_value`."
11+ )
812 expect_true(is_epi_df(a ))
913 expect_identical(attributes(a )$ metadata $ geo_type , " custom" )
1014 expect_identical(attributes(a )$ metadata $ time_type , " custom" )
1115 expect_true(lubridate :: is.POSIXt(attributes(a )$ metadata $ as_of ))
12-
16+
1317 # Simple non-empty tibble with geo_value and time_value cols
1418 tib <- tibble :: tibble(
1519 x = 1 : 10 , y = 1 : 10 ,
1620 time_value = rep(seq(as.Date(" 2020-01-01" ), by = 1 , length.out = 5 ), times = 2 ),
1721 geo_value = rep(c(" ca" , " hi" ), each = 5 )
1822 )
19-
20- epi_tib = new_epi_df(tib )
23+
24+ epi_tib <- new_epi_df(tib )
2125 expect_true(is_epi_df(epi_tib ))
2226 expect_length(epi_tib , 4L )
2327 expect_identical(attributes(epi_tib )$ metadata $ geo_type , " state" )
@@ -32,10 +36,72 @@ test_that("as_epi_df errors when additional_metadata is not a list", {
3236 dplyr :: slice_tail(n = 6 ) %> %
3337 tsibble :: as_tsibble() %> %
3438 dplyr :: mutate(
35- state = rep(" MA" ,6 ),
36- pol = rep(c(" blue" , " swing" , " swing" ), each = 2 ))
37-
39+ state = rep(" MA" , 6 ),
40+ pol = rep(c(" blue" , " swing" , " swing" ), each = 2 )
41+ )
42+
3843 expect_error(
39- as_epi_df(ex_input , additional_metadata = c(other_keys = " state" , " pol" )),
40- " `additional_metadata` must be a list type." )
41- })
44+ as_epi_df(ex_input , additional_metadata = c(other_keys = " state" , " pol" )),
45+ " `additional_metadata` must be a list type."
46+ )
47+ })
48+
49+ # select fixes
50+
51+ tib <- tibble :: tibble(
52+ x = 1 : 10 , y = 1 : 10 ,
53+ time_value = rep(seq(as.Date(" 2020-01-01" ),
54+ by = 1 , length.out = 5
55+ ), times = 2 ),
56+ geo_value = rep(c(" ca" , " hi" ), each = 5 )
57+ )
58+ epi_tib <- epiprocess :: new_epi_df(tib )
59+ test_that(" grouped epi_df maintains type for select" , {
60+ grouped_epi <- epi_tib %> % group_by(geo_value )
61+ selected_df <- grouped_epi %> % select(- y )
62+ expect_true(inherits(selected_df , " epi_df" ))
63+ # make sure that the attributes are right
64+ epi_attr <- attributes(selected_df )
65+ expect_identical(epi_attr $ names , c(" geo_value" , " time_value" , " x" ))
66+ expect_identical(epi_attr $ row.names , seq(1 , 10 ))
67+ expect_identical(epi_attr $ groups , attributes(grouped_epi )$ groups )
68+ expect_identical(epi_attr $ metadata , attributes(epi_tib )$ metadata )
69+ expect_identical(selected_df , epi_tib %> % select(- y ) %> % group_by(geo_value ))
70+ })
71+
72+ test_that(" grouped epi_df drops type when dropping keys" , {
73+ grouped_epi <- epi_tib %> % group_by(geo_value )
74+ selected_df <- grouped_epi %> % select(geo_value )
75+ expect_true(! inherits(selected_df , " epi_df" ))
76+ })
77+
78+ test_that(" grouped epi_df handles extra keys correctly" , {
79+ tib <- tibble :: tibble(
80+ x = 1 : 10 , y = 1 : 10 ,
81+ time_value = rep(seq(as.Date(" 2020-01-01" ),
82+ by = 1 , length.out = 5
83+ ), times = 2 ),
84+ geo_value = rep(c(" ca" , " hi" ), each = 5 ),
85+ extra_key = rep(seq(as.Date(" 2020-01-01" ),
86+ by = 1 , length.out = 5
87+ ), times = 2 )
88+ )
89+ epi_tib <- epiprocess :: new_epi_df(tib ,
90+ additional_metadata = list (other_keys = " extra_key" )
91+ )
92+ attributes(epi_tib )
93+ grouped_epi <- epi_tib %> % group_by(geo_value )
94+ selected_df <- grouped_epi %> % select(- extra_key )
95+ expect_true(inherits(selected_df , " epi_df" ))
96+ # make sure that the attributes are right
97+ old_attr <- attributes(epi_tib )
98+ epi_attr <- attributes(selected_df )
99+ expect_identical(epi_attr $ names , c(" geo_value" , " time_value" , " x" , " y" ))
100+ expect_identical(epi_attr $ row.names , seq(1 , 10 ))
101+ expect_identical(epi_attr $ groups , attributes(grouped_epi )$ groups )
102+ expect_identical(epi_attr $ metadata , list (
103+ geo_type = " state" , time_type = " day" ,
104+ as_of = old_attr $ metadata $ as_of ,
105+ other_keys = character (0 )
106+ ))
107+ })
0 commit comments