@@ -7,12 +7,14 @@ toy_epi_df <- tibble::tibble(
7
7
length.out = 5
8
8
), times = 2 ),
9
9
geo_value = rep(c(" ca" , " hi" ), each = 5 ),
10
- indicator_var = as.factor(rep(1 : 2 , times = 5 )),
11
- ) %> % as_epi_df(additional_metadata = list (other_keys = " indicator_var" ))
10
+ indic_var1 = as.factor(rep(1 : 2 , times = 5 )),
11
+ indic_var2 = as.factor(rep(letters [1 : 5 ], times = 2 ))
12
+ ) %> % as_epi_df(additional_metadata =
13
+ list (other_keys = c(" indic_var1" , " indic_var2" )))
12
14
13
15
att_toy = attr(toy_epi_df , " metadata" )
14
16
15
- test_that(" head and tail do not drop the epi_df class" , {
17
+ test_that(" Head and tail do not drop the epi_df class" , {
16
18
att_head = attr(head(toy_epi_df ), " metadata" )
17
19
att_tail = attr(tail(toy_epi_df ), " metadata" )
18
20
@@ -29,35 +31,43 @@ test_that("head and tail do not drop the epi_df class", {
29
31
})
30
32
31
33
32
- test_that(" subsetting drops or does not drop the epi_df class appropriately" , {
34
+ test_that(" Subsetting drops & does not drop the epi_df class appropriately" , {
33
35
34
36
# Row subset - should be epi_df
35
37
row_subset = toy_epi_df [1 : 2 , ]
36
38
att_row_subset = attr(row_subset , " metadata" )
37
39
38
40
expect_true(is_epi_df(row_subset ))
39
41
expect_equal(nrow(row_subset ), 2L )
40
- expect_equal(ncol(row_subset ), 5L )
42
+ expect_equal(ncol(row_subset ), 6L )
41
43
expect_identical(att_row_subset $ geo_type , att_toy $ geo_type )
42
44
expect_identical(att_row_subset $ time_type , att_toy $ time_type )
43
45
expect_identical(att_row_subset $ as_of , att_toy $ as_of )
44
46
expect_identical(att_row_subset $ other_keys , att_toy $ other_keys )
45
47
46
- # Col subset - shouldn't be an epi_df
47
- col_subset = toy_epi_df [, 2 : 3 ]
48
-
49
- expect_false(is_epi_df(col_subset ))
50
- expect_true(tibble :: is_tibble(col_subset ))
51
- expect_equal(nrow(col_subset ), 10L )
52
- expect_equal(ncol(col_subset ), 2L )
53
-
54
48
# Row and col single value - shouldn't be an epi_df
55
49
row_col_subset1 = toy_epi_df [1 ,2 ]
56
50
expect_false(is_epi_df(row_col_subset1 ))
57
51
expect_true(tibble :: is_tibble(row_col_subset1 ))
58
52
expect_equal(nrow(row_col_subset1 ), 1L )
59
53
expect_equal(ncol(row_col_subset1 ), 1L )
60
54
55
+ # Col subset with no time_value - shouldn't be an epi_df
56
+ col_subset1 = toy_epi_df [, c(1 ,3 )]
57
+
58
+ expect_false(is_epi_df(col_subset1 ))
59
+ expect_true(tibble :: is_tibble(col_subset1 ))
60
+ expect_equal(nrow(col_subset1 ), 10L )
61
+ expect_equal(ncol(col_subset1 ), 2L )
62
+
63
+ # Col subset with no geo_value - shouldn't be an epi_df
64
+ col_subset2 = toy_epi_df [, 2 : 3 ]
65
+
66
+ expect_false(is_epi_df(col_subset2 ))
67
+ expect_true(tibble :: is_tibble(col_subset2 ))
68
+ expect_equal(nrow(col_subset2 ), 10L )
69
+ expect_equal(ncol(col_subset2 ), 2L )
70
+
61
71
# Row and col subset that contains geo_value and time_value - should be epi_df
62
72
row_col_subset2 = toy_epi_df [2 : 3 ,1 : 3 ]
63
73
att_row_col_subset2 = attr(row_col_subset2 , " metadata" )
@@ -68,6 +78,41 @@ test_that("subsetting drops or does not drop the epi_df class appropriately", {
68
78
expect_identical(att_row_col_subset2 $ geo_type , att_toy $ geo_type )
69
79
expect_identical(att_row_col_subset2 $ time_type , att_toy $ time_type )
70
80
expect_identical(att_row_col_subset2 $ as_of , att_toy $ as_of )
71
- expect_identical(att_row_col_subset2 $ other_keys , att_toy $ other_keys )
81
+ expect_identical(att_row_col_subset2 $ other_keys , character (0 ))
82
+ })
83
+
84
+ test_that(" When duplicate cols in subset should abort" , {
85
+ expect_error(toy_epi_df [, c(2 ,2 : 3 ,4 ,4 ,4 )],
86
+ " Column name(s) time_value, y must not be duplicated." , fixed = T )
87
+ expect_error(toy_epi_df [1 : 4 , c(1 ,2 : 4 ,1 )],
88
+ " Column name(s) geo_value must not be duplicated." , fixed = T )
89
+ })
90
+
91
+ test_that(" Correct metadata when subset includes some of other_keys" , {
92
+ # Only include other_var of indic_var1
93
+ only_indic_var1 = toy_epi_df [, 1 : 5 ]
94
+ att_only_indic_var1 = attr(only_indic_var1 , " metadata" )
95
+
96
+ expect_true(is_epi_df(only_indic_var1 ))
97
+ expect_equal(nrow(only_indic_var1 ), 10L )
98
+ expect_equal(ncol(only_indic_var1 ), 5L )
99
+ expect_identical(att_only_indic_var1 $ geo_type , att_toy $ geo_type )
100
+ expect_identical(att_only_indic_var1 $ time_type , att_toy $ time_type )
101
+ expect_identical(att_only_indic_var1 $ as_of , att_toy $ as_of )
102
+ expect_identical(att_only_indic_var1 $ other_keys , att_toy $ other_keys [- 2 ])
103
+
104
+ # Only include other_var of indic_var2
105
+ only_indic_var2 = toy_epi_df [, c(1 : 4 ,6 )]
106
+ att_only_indic_var2 = attr(only_indic_var2 , " metadata" )
72
107
73
- })
108
+ expect_true(is_epi_df(only_indic_var2 ))
109
+ expect_equal(nrow(only_indic_var2 ), 10L )
110
+ expect_equal(ncol(only_indic_var2 ), 5L )
111
+ expect_identical(att_only_indic_var2 $ geo_type , att_toy $ geo_type )
112
+ expect_identical(att_only_indic_var2 $ time_type , att_toy $ time_type )
113
+ expect_identical(att_only_indic_var2 $ as_of , att_toy $ as_of )
114
+ expect_identical(att_only_indic_var2 $ other_keys , att_toy $ other_keys [- 1 ])
115
+
116
+ # Including both original other_keys was already tested above
117
+ })
118
+
0 commit comments