-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest-model.R
115 lines (105 loc) · 3.84 KB
/
test-model.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
test_that("`parse_timeset_input` on valid inputs", {
# Date-like: we can get out Date vector, or character or integerish YYYYmmdd
# format only
expect_identical(
parse_timeset_input(as.Date("2018-01-01")),
as.Date("2018-01-01")
)
expect_identical(
parse_timeset_input(as.Date(c("2018-01-01", "2018-01-02"))),
as.Date(c("2018-01-01", "2018-01-02"))
)
expect_identical(parse_timeset_input("2018-01-01"), "20180101")
expect_identical(
parse_timeset_input(c("2018-01-01", "2018-01-02")),
c("20180101", "20180102")
)
expect_identical(parse_timeset_input("20180101"), "20180101")
expect_identical(
parse_timeset_input(c("20180101", "20180102")),
c("20180101", "20180102")
)
expect_identical(parse_timeset_input(20180101), 20180101)
expect_identical(
parse_timeset_input(c(20180101, 20180102)),
c(20180101, 20180102)
)
# Epiweeks: we can get out character or integerish
expect_identical(parse_timeset_input("201801"), "201801")
expect_identical(
parse_timeset_input(c("201801", "201802")),
c("201801", "201802")
)
expect_identical(parse_timeset_input(201801), 201801)
expect_identical(
parse_timeset_input(c(201801, 201802)),
c(201801, 201802)
)
# EpiRanges: aren't changed
expect_identical(
epirange(as.Date("2018-01-01"), as.Date("2018-01-05")),
epirange(as.Date("2018-01-01"), as.Date("2018-01-05"))
)
expect_identical(epirange(201801, 201805), epirange(201801, 201805))
# Wildcard:
expect_identical(parse_timeset_input("*"), "*")
# NULL: allow this as a missing argument marker
expect_identical(parse_timeset_input(NULL), NULL)
})
test_that("null parsing", {
# parse_data_frame (df[[info$name]] = NULL)-> parse_value
epidata_call <- pub_flusurv(
locations = "ca",
epiweeks = 202001,
fetch_args = fetch_args_list(dry_run = TRUE)
)
# see generate_test_data.R
mock_df <- as.data.frame(readr::read_rds(testthat::test_path("data/flusurv-epiweeks.rds")))
metadata <- epidata_call$meta
mock_df[[metadata[[1]]$name]][1] <- NA
mock_df[[metadata[[2]]$name]] <- c(TRUE)
epidata_call$meta[[2]]$type <- "bool"
expect_no_error(res <- parse_data_frame(epidata_call, mock_df) %>% as_tibble())
expect_true(res$location)
expect_identical(res$release_date, as.Date(NA))
# if the call has no metadata, return the whole frame as is
epidata_call$meta <- NULL
expect_identical(parse_data_frame(epidata_call, mock_df), mock_df)
})
test_that("parse invalid time", {
value <- list(3)
value$class <- "my nonexistant class"
expect_error(parse_timeset_input(value))
})
test_that("parse_data_frame warns when df contains fields not listed in meta", {
epidata_call <- pub_flusurv(
locations = "ca",
epiweeks = 202001,
fetch_args = fetch_args_list(dry_run = TRUE)
)
# see generate_test_data.R
mock_df <- as.data.frame(readr::read_rds(testthat::test_path("data/flusurv-epiweeks.rds")))
# Success when meta and df fields match exactly
expect_no_warning(parse_data_frame(epidata_call, mock_df))
# Warning when df contains extra fields
mock_df$extra <- 5
expect_warning(
parse_data_frame(epidata_call, mock_df),
class = "epidatr__missing_meta_fields"
)
mock_df$extra <- NULL
# Success when meta contains extra fields
mock_df$rate_age_0 <- NULL
expect_no_warning(parse_data_frame(epidata_call, mock_df))
})
test_that("parse_api_date accepts str and int input", {
expect_identical(parse_api_date("20200101"), as.Date("2020-01-01"))
expect_identical(parse_api_date(20200101), as.Date("2020-01-01"))
})
test_that("parse_api_date accepts YYYYMMDD and YYYY-MM-DD", {
expect_identical(parse_api_date(20200101), as.Date("2020-01-01"))
expect_identical(parse_api_date("2020-01-01"), as.Date("2020-01-01"))
})
test_that("parse_api_date handles missing values appropriately", {
expect_identical(parse_api_date(NA), as.Date(NA))
})