Skip to content

Commit 887246a

Browse files
committed
Give informative errors calls in apply_to_each() and apply_to_each_col()
1 parent 9ba279c commit 887246a

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: qualifyr
22
Type: Package
33
Title: Validate Relational Data Structures
4-
Version: 0.1.0.9006
4+
Version: 0.1.0.9007
55
Authors@R: c(
66
person(
77
"Ian", "Farm",

R/qf_constraint.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,26 +255,30 @@ NULL
255255
#'
256256
#' @export
257257
apply_to_each <- function(.cstr, ..., .args = list()) {
258+
call <- sys.call()
258259
check_arg_type(.cstr, "function")
259260
check_arg_type(.args, "list")
260261
col_specs <- rlang::enquos(...)
261-
constraint_specs <- purrr::map(col_specs, \(spec)
262-
rlang::eval_tidy(rlang::expr(
263-
(.cstr)(!!spec, !!!.args)
262+
constraint_specs <- purrr::map(col_specs, \(col_spec) {
263+
cstr_spec <- rlang::eval_tidy(rlang::expr(
264+
(.cstr)(!!col_spec, !!!.args)
264265
))
265-
)
266+
environment(cstr_spec)$call <- call
267+
cstr_spec
268+
})
266269
as_qf_constraint_list(constraint_specs)
267270
}
268271

269272
#' @rdname apply_to_each
270273
#' @export
271274
apply_to_each_col <- function(.cstr, .cols, .args = list()) {
275+
call <- sys.call()
272276
check_arg_type(.cstr, "function")
273277
check_arg_type(.args, "list")
274278
col_specs <- rlang::enquo(.cols)
275279

276280
new_qf_constraint_specifier({
277-
cols <- select_names(col_specs, .table)
281+
cols <- select_names(col_specs, .table, error_call = call)
278282
constraint_objs <- purrr::map(cols, \(col) {
279283
f <- do.call(.cstr, c(list(col), .args))
280284
(f)(.table)

tests/testthat/test-qf_constraint.R

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,22 @@ test_that("eval_select() errors have informative calls", {
181181
cstr_primary_key(tailnumber) # wrong column name
182182
})
183183

184-
expect_equal(
185-
e$call[[1]],
186-
quote(`constraints<-`)
187-
)
188-
expect_equal(
189-
e$parent$call[[1]],
190-
quote(cstr_primary_key)
191-
)
184+
expect_equal(e$call[[1]], quote(`constraints<-`))
185+
expect_equal(e$parent$call[[1]], quote(cstr_primary_key))
186+
})
187+
188+
test_that("apply_to_each() and apply_to_each_col() have informative error calls", {
189+
dset1 <- example_dataset(constrained = FALSE)
190+
e1 <- rlang::catch_cnd({
191+
constraints(dset1$planes) <-
192+
cstr_not_missing |> apply_to_each(year, type, mfr, model) # `mfr` is wrong
193+
})
194+
expect_equal(e1$parent$call[[1]], quote(apply_to_each))
195+
196+
dset2 <- example_dataset(constrained = FALSE)
197+
e2 <- rlang::catch_cnd({
198+
constraints(dset2$planes) <-
199+
cstr_not_missing |> apply_to_each_col(c(year, type, mfr, model)) # `mfr` is wrong
200+
})
201+
expect_equal(e2$parent$call[[1]], quote(apply_to_each_col))
192202
})

0 commit comments

Comments
 (0)