Skip to content

Commit

Permalink
Merge pull request #539 from yjunechoe/comparison-preserve-classes
Browse files Browse the repository at this point in the history
Preserve classes when reading from `validation_set$values`
  • Loading branch information
yjunechoe authored Jun 13, 2024
2 parents fbc7bb2 + 52b8225 commit c54b035
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Fixed a regression in `col_vals_*()` functions, where `vars("col")` was evaluating to the string `"col"`. Behavior of `vars("col")` is now aligned back with `vars(col)` - both evaluate to the column name `col`.

- Warnings/errors arising from comparing `columns` to a `value` of different class (for example, comparing a datetime column to a date value `Sys.Date()` instead of another datetime value `Sys.time()`) are now signalled appropriately at `interrogate()`.

# pointblank 0.12.1

- Ensured that the column string is a symbol before constructing the expression for the `col_vals_*()` functions.
Expand Down
6 changes: 3 additions & 3 deletions R/interrogate.R
Original file line number Diff line number Diff line change
Expand Up @@ -1436,10 +1436,10 @@ interrogate_set <- function(
}

extra_variables <-
base::setdiff(table_col_distinct_values, set)
table_col_distinct_values[!table_col_distinct_values %in% set]

table_col_distinct_set <-
base::intersect(table_col_distinct_values, set)
table_col_distinct_values[table_col_distinct_values %in% set]

dplyr::bind_rows(
dplyr::tibble(set_element = as.character(set)) %>%
Expand Down Expand Up @@ -1511,7 +1511,7 @@ interrogate_set <- function(
}

table_col_distinct_set <-
base::intersect(table_col_distinct_values, set)
table_col_distinct_values[table_col_distinct_values %in% set]

dplyr::tibble(set_element = as.character(set)) %>%
dplyr::left_join(
Expand Down
16 changes: 15 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,21 @@ get_column_as_sym_at_idx <- function(agent, idx) {
}

get_values_at_idx <- function(agent, idx) {
agent$validation_set[[idx, "values"]] %>% unlist(recursive = FALSE)

# Get list-column element (`values` is always a length-1 list)
values <- agent$validation_set[[idx, "values"]]

# Expressions (via `col_vals_expr()`) and functions (via `specially()`)
# can get the old `unlist()` treatment
if (rlang::is_expression(values[[1]]) || rlang::is_function(values[[1]])) {
values <- unlist(values, recursive = FALSE)
} else {
# In other cases (e.g., `values`, `left`, `right`), flatten with subsetting
# to preserve class
values <- values[[1]]
}

values
}

get_column_na_pass_at_idx <- function(agent, idx) {
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-interrogate_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -2133,3 +2133,25 @@ test_that("vars(col) and vars('col') both evaluate to tbl column", {
expect_true(test_col_vals_gt(df, x, vars("y")))

})

test_that("class preserved in `value`", {

# A custom class with a `==` method that simply errors
custom_val <- structure(1L, class = "pb-test-custom-class")
registerS3method(
"==", "pb-test-custom-class", function(x, ...) stop("Bad class for `==`")
)

# We expect error (class preserved) vs. TRUE (class stripped)
expect_error(custom_val == 1L)
expect_true(unclass(custom_val) == 1L)

# Error is correctly thrown and safely caught at interrogate
expect_no_error({
agent <- create_agent(data.frame(col = custom_val)) %>%
col_vals_equal(columns = col, value = 1L) %>%
interrogate()
})
expect_true(agent$validation_set$eval_error)

})

0 comments on commit c54b035

Please sign in to comment.