@@ -128,3 +128,103 @@ test_that("group_vars works as expected", {
128128 " geo_value"
129129 )
130130})
131+
132+ test_that(" filter.epi_archive works as expected" , {
133+
134+ ea2 <- ea2_data %> %
135+ as_epi_archive()
136+
137+ # Some basic output value checks:
138+
139+ expect_equal(
140+ ea2 %> % filter(geo_value == " tn" ),
141+ new_epi_archive(
142+ ea2 $ DT [FALSE ],
143+ ea2 $ geo_type , ea2 $ time_type , ea2 $ other_keys ,
144+ ea2 $ clobberable_versions_start , ea2 $ versions_end
145+ )
146+ )
147+
148+ expect_equal(
149+ ea2 %> % filter(geo_value == " ca" , time_value == as.Date(" 2020-06-02" )),
150+ new_epi_archive(
151+ data.table :: data.table(geo_value = " ca" , time_value = as.Date(" 2020-06-02" ),
152+ version = as.Date(" 2020-06-02" ) + 0 : 2 , cases = 0 : 2 ),
153+ ea2 $ geo_type , ea2 $ time_type , ea2 $ other_keys ,
154+ ea2 $ clobberable_versions_start , ea2 $ versions_end
155+ )
156+ )
157+
158+ # Output geo_type and time_type behavior:
159+
160+ hrr_day_ea <- tibble(
161+ geo_value = c(rep(1 , 14 ), 100 ),
162+ time_value = as.Date(" 2020-01-01" ) - 1 + c(1 : 14 , 14 ),
163+ version = time_value + 3 ,
164+ value = 1 : 15
165+ ) %> %
166+ as_epi_archive()
167+
168+ expect_equal(hrr_day_ea $ geo_type , " hrr" )
169+ expect_equal(hrr_day_ea $ time_type , " day" )
170+
171+ hrr_week_ea <- hrr_day_ea %> %
172+ filter(geo_value == 1 , as.POSIXlt(time_value )$ wday == 6L )
173+
174+ expect_equal(hrr_week_ea $ geo_type , " hrr" )
175+ expect_equal(hrr_week_ea $ time_type , " week" )
176+
177+ hrr_one_week_ea <- hrr_week_ea %> %
178+ filter(time_value == time_value [[1 ]])
179+
180+ expect_equal(hrr_one_week_ea $ time_type , " week" )
181+
182+ intcustom_day_ea <- hrr_day_ea
183+ intcustom_day_ea $ geo_type <- " custom"
184+
185+ intcustom_week_ea <- intcustom_day_ea %> %
186+ filter(geo_value == 1 , as.POSIXlt(time_value )$ wday == 6L )
187+
188+ expect_equal(intcustom_week_ea $ geo_type , " custom" )
189+ expect_equal(intcustom_week_ea $ time_type , " week" )
190+
191+ # Error-raising:
192+ expect_error(
193+ ea2 %> % filter(version == as.Date(" 2020-06-02" )),
194+ class = " epiprocess__filter_archive__used_version"
195+ )
196+ expect_error(
197+ ea2 %> % filter(version < = as.Date(" 2020-06-02" )),
198+ class = " epiprocess__filter_archive__used_version"
199+ )
200+ expect_snapshot(
201+ ea2 %> % filter(version < = as.Date(" 2020-06-02" )),
202+ error = TRUE , cnd_class = TRUE
203+ )
204+ expect_error(
205+ ea2 %> % filter(time_value > = as.Date(" 2020-06-02" ), cases > = 2L ),
206+ class = " epiprocess__filter_archive__used_measurement"
207+ )
208+ expect_snapshot(
209+ ea2 %> % filter(time_value > = as.Date(" 2020-06-02" ), cases > = 2L ),
210+ error = TRUE , cnd_class = TRUE
211+ )
212+ expect_error(
213+ ea2 %> % filter(time_value > = as.Date(" 2020-06-02" ), cases > = 2L ),
214+ class = " epiprocess__filter_archive__used_measurement"
215+ )
216+ expect_error(
217+ ea2 %> % filter(cases > = median(cases ), .by = geo_value ),
218+ class = " epiprocess__filter_archive__used_measurement"
219+ )
220+
221+ # Escape hatch:
222+ expect_equal(
223+ ea2 %> %
224+ filter(version < = time_value + as.difftime(1 , units = " days" ),
225+ .format_aware = TRUE ) %> %
226+ . $ DT ,
227+ ea2 $ DT [version < = time_value + as.difftime(1 , units = " days" ), ]
228+ )
229+
230+ })
0 commit comments