-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_test.R
289 lines (271 loc) · 8.87 KB
/
create_test.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#' Create Test Function
#'
#' This function creates a test function to perform various data validation
#' checks. The returned function can be applied to a dataset to perform the
#' specified tests.
#'
#' @param dimension_test A vector of two integers specifying the expected number
#' of rows and columns.
#' @param combinations_test A list with the elements `variables` (character
#' vector of variable names) and `expectation` (integer specifying the expected
#' number of unique combinations for each column).
#' @param row_duplicates Logical. If TRUE, checks for duplicate rows.
#' @param col_duplicates Logical. If TRUE, checks for duplicate columns.
#' @param min_threshold_test Named list of minimum threshold values for
#' specified columns.
#' @param max_threshold_test Named list of maximum threshold values for
#' specified columns.
#' @return A function to be applied to the dataset.
#' @examples
#'
#' # get path
#' path <- system.file(
#' "extdata",
#' "fake_epi_df_togo.rds",
#' package = "epiCleanr")
#'
# # get example data
#' fake_epi_df_togo <- import(path)
#'
#' # Set up unit-test function
#' my_tests <- create_test(
#' # For checking the dimension of the data
#' dimension_test = c(900, 9),
#' # For expected number of combinations in data
#' combinations_test = list(
#' variables = c("month", "year", "district"),
#' expectation = 12 * 5 * 15),
#' # Check repeated cols, rows and max and min thresholds
#' row_duplicates = TRUE, col_duplicates = TRUE,
#' max_threshold_test = list(malaria_tests = 1000, cholera_tests = 1000),
#' min_threshold_test = list(cholera_cases = 0, cholera_cases = 0)
#' )
#'
# Apply your unit-test on your data
#' result <- my_tests(fake_epi_df_togo)
#'
#' @importFrom glue glue
#' @importFrom crayon red green blue
#' @export
create_test <- function(dimension_test = NULL, combinations_test = NULL,
row_duplicates = FALSE, col_duplicates = FALSE,
min_threshold_test = NULL, max_threshold_test = NULL) {
# praise emoji function
praise <- function() {
emoji <- c(
"\U0001f600", # smile
"\U0001f973", # party face
"\U0001f638", # cat grin
"\U0001f308", # rainbow
"\U0001f947", # gold medal
"\U0001f389", # party popper
"\U0001f38a" # confetti ball
)
sample(emoji, 1)
}
# Function to format big numbers
big_mark <- function(value) {
return(formatC(value, format = "d", big.mark = ","))
}
return(function(data) {
result <- list()
total_tests <- 0
tests_passed <- 0
# Check Dimensions --------------------------------------------------------
if (!is.null(dimension_test)) {
total_tests <- total_tests + 1
actual_dims <- dim(data)
expected_dims <- as.integer(dimension_test)
if (identical(actual_dims, expected_dims)) {
tests_passed <- tests_passed + 1
message(
glue::glue(
crayon::green(
"Test passed! You have the correct number of dimensions!"
)
)
)
} else {
message(
glue::glue(
crayon::red(
"Warning! Test failed. Expected {big_mark(expected_dims[1])}",
"rows and {big_mark(expected_dims[2])} columns,",
"but got {big_mark(actual_dims[1])} rows and",
"{big_mark(actual_dims[2])} columns."
)
)
)
}
}
# Check Row Duplicates -----------------------------------------------------
if (row_duplicates) {
total_tests <- total_tests + 1
duplicate_rows <- data[duplicated(data) | duplicated(data,
fromLast = TRUE
), ]
if (nrow(duplicate_rows) > 0) {
result$duplicate_rows <- duplicate_rows
message(
glue::glue(
crayon::red(
"Warning! Test failed. Duplicate rows found.",
"See output$duplicate_rows."
)
)
)
} else {
tests_passed <- tests_passed + 1
message(
glue::glue(crayon::green("Test passed! No duplicate rows found!"))
)
}
}
# Check Column Duplicates --------------------------------------------------
if (col_duplicates) {
total_tests <- total_tests + 1
repeated_columns <- which(duplicated(t(data)))
if (length(repeated_columns) > 0) {
result$duplicate_columns <- repeated_columns
message(
glue::glue(
crayon::red(
"Warning! Test failed. Repeated columns found.",
"See output$duplicate_columns."
)
)
)
} else {
tests_passed <- tests_passed + 1
message(
glue::glue(
crayon::green("Test passed! No repeated columns found!")
)
)
}
}
# Check Combinations ------------------------------------------------------
if (!is.null(combinations_test)) {
total_tests <- total_tests + 1
variables <- combinations_test$variables
# Check if the specified variables exist in the data
missing_variables <- setdiff(variables, colnames(data))
if (length(missing_variables) > 0) {
stop(glue::glue(
"Error! The following variables do not exist in the ",
"dataset: {paste(missing_variables, collapse = ', ')}"
))
}
expectation <- combinations_test$expectation
actual_combinations <- nrow(unique(data[, variables]))
if (actual_combinations == expectation) {
tests_passed <- tests_passed + 1
message(
glue::glue(
crayon::green(
"Test passed! You have the correct number of combinations",
"for {paste(variables, collapse = ', ')}!"
)
)
)
} else {
message(
glue::glue(
crayon::red(
"Warning! Test failed. Expected {big_mark(expectation)}",
"combinations but found {big_mark(actual_combinations)}",
"for {paste(variables, collapse = ', ')}."
)
)
)
}
}
# Threshold Tests --------------------------------------------------------
for (test_type in c("min", "max")) {
threshold_test <- if (test_type == "min") {
min_threshold_test
} else {
max_threshold_test
}
if (!is.null(threshold_test)) {
for (column_name in names(threshold_test)) {
# Check if the column exists in the data
if (!column_name %in% colnames(data)) {
stop(
glue::glue(
"Error! Column {column_name} does not exist in the dataset."
)
)
}
total_tests <- total_tests + 1
threshold <- threshold_test[[column_name]]
failed_condition <- if (test_type == "max") {
data[[column_name]] > threshold
} else {
data[[column_name]] < threshold
}
# Exclude NA values from the failed condition
failed_indices <- which(
failed_condition & !is.na(data[[column_name]])
)
if (length(failed_indices) > 0) {
key <- paste0(test_type, "_thresh_", column_name)
result[[key]] <- data.frame(
row_number = failed_indices,
value = data[
failed_indices,
column_name
]
)
message_type <- if (test_type == "max") "above" else "below"
message(
glue::glue(
crayon::red(
"Warning! Test failed. Values in column {column_name}",
"are {message_type} the threshold. See output${key}."
)
)
)
} else {
tests_passed <- tests_passed + 1
message_type <- if (test_type == "max") "below" else "above"
message(
glue::glue(
crayon::green(
"Test passed! Values in column {column_name}",
"are {message_type} the threshold."
)
)
)
}
}
}
}
# Calculate and return the percentage of tests passed
result$percentage_passed <- if (
total_tests > 0) {
tests_passed / total_tests * 100
} else {
stop("No tests have been carried out!")
}
if (tests_passed == total_tests) {
message(
crayon::blue(
glue::glue(
"Congratulations! All tests passed: {tests_passed}/{total_tests}",
" ({round(result$percentage_passed)}%) {praise()}"
)
)
)
} else {
message(
crayon::blue(
glue::glue(
"Total tests passed: {tests_passed}/{total_tests}",
" ({round(result$percentage_passed)}%)"
)
)
)
}
})
}