sales <- data.frame(
sale_id = 1:5,
date = c("20200101", "20200101", "20200102", "20200103", "2020003"),
sale_price = c(10, 20, 30, 40, -1),
book_title = c(
"Phenomenology of Spirit",
NA,
"Critique of Practical Reason",
"Spirit of Trust",
"Empiricism and the Philosophy of Mind"
),
stringsAsFactors = FALSE
)
# Create a custom expectation
expect_prop_length <- function(var, len, prop, data) {
expect_prop_gte(
var = {{var}}, # Notice the use of the embracing operator
func = chk_max_length,
prop = prop,
data = data,
args = list(len = len),
func_desc = "length_check"
)
}
# Use it to check that dates are mostly <= 8 char wide
expect_prop_length(date, 8, 0.9, sales)
# Check price values mostly between 0 and 100
try(expect_prop_values(sale_price, 0.9, 1:100, data = sales))
Run the code above in your browser using DataLab