-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest-cal_min_max_date.R
More file actions
162 lines (139 loc) · 4.68 KB
/
Copy pathtest-cal_min_max_date.R
File metadata and controls
162 lines (139 loc) · 4.68 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
test_that("Warn if date variable parameter is NULL", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT,
"001", "26-04-2022"
)
warning_msg <- "Date variable NA or Time variable NA not present in source data"
expect_warning(
observed <- cal_min_max_date(EX,
date_variable = NA,
time_variable = NA,
val_type = "max",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
),
regexp = warning_msg
)
expected <- stats::setNames(
data.frame(matrix(ncol = 2L, nrow = 0L)),
c("patient_number", "datetime")
)
expect_identical(observed, expected)
})
test_that("cal_min_max_date works with date and time strings", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT, ~EX_ST_TM,
"001", "26-04-2022", "10:20",
"001", "25-04-2022", "10:15",
"001", "25-04-2022", "10:19",
"002", "26-05-2022", "06:23",
"002", "26-05-2022", "04:59",
"002", "26-05-2022", "05:59"
)
expected_min <- tibble::tribble(
~patient_number, ~datetime,
"001", "2022-04-25T10:15",
"002", "2022-05-26T04:59"
)
expected_max <- tibble::tribble(
~patient_number, ~datetime,
"002", "2022-05-26T06:23",
"001", "2022-04-26T10:20"
)
observed_min <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "min",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
)
expect_identical(observed_min, expected_min)
observed_max <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "max",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
)
expect_identical(observed_max, expected_max)
})
test_that("Warning is displayed if date or time variables parameters passed are not present", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT,
"001", "26-04-2022"
)
warning_msg <- "Date variable EX_ST_DT or Time variable EX_ST_TM not present in source data"
expect_warning(
observed <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "max",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
),
regexp = warning_msg
)
expected <- stats::setNames(
data.frame(matrix(ncol = 2L, nrow = 0L)),
c("patient_number", "datetime")
)
expect_identical(observed, expected)
})
test_that("cal_min_max_date works with Date and hms (time) objects", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT, ~EX_ST_TM,
"001", as.Date("2022-04-26"), hms::hms(seconds = NULL, minutes = 20L, hours = 10L),
"001", as.Date("2022-04-25"), hms::hms(seconds = NULL, minutes = 15L, hours = 10L),
"001", as.Date("2022-04-25"), hms::hms(seconds = NULL, minutes = 19L, hours = 10L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 23L, hours = 6L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 59L, hours = 4L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 59L, hours = 5L)
)
expected_min <- tibble::tribble(
~patient_number, ~datetime,
"001", "2022-04-25T10:15:00",
"002", "2022-05-26T04:59:00"
)
expected_max <- tibble::tribble(
~patient_number, ~datetime,
"002", "2022-05-26T06:23:00",
"001", "2022-04-26T10:20:00"
)
observed_min <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "min",
date_format = "y-m-d",
time_format = "H:M:S"
)
expect_identical(observed_min, expected_min)
observed_max <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "max",
date_format = "y-m-d",
time_format = "H:M:S"
)
expect_identical(observed_max, expected_max)
})
test_that("cal_min_max_date works with Date objects (no time)", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT,
"001", as.Date("04-25-22", "%m-%d-%y"),
"001", as.Date("04-25-22", "%m-%d-%y"),
"002", as.Date("05-26-22", "%m-%d-%y")
)
expected_min <- tibble::tribble(
~patient_number, ~datetime,
"001", create_iso8601("04-25-22", .format = "m-d-y"),
"002", create_iso8601("05-26-22", .format = "m-d-y")
)
observed_min <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = NA,
val_type = "min",
date_format = "y-m-d",
time_format = NA
)
expect_identical(observed_min, expected_min)
})