Skip to content

Commit

Permalink
Merge pull request #275 from tidyverse/f-telemetry-tests
Browse files Browse the repository at this point in the history
test: Test telemetry code
  • Loading branch information
krlmlr authored Oct 16, 2024
2 parents 89063e1 + 2394f3e commit a294979
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion R/anti_join.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ anti_join.duckplyr_df <- function(x, y, by = NULL, copy = FALSE, ..., na_matches
na_matches <- check_na_matches(na_matches, error_call = error_call)

# Our implementation
rel_try(call = list(name = "anti_join", x = x, y = y, args = list(by = if(!is.null(by)) as_join_by(by), copy = copy, na_matches = na_matches)),
rel_try(call = list(name = "anti_join", x = x, y = y, args = list(by = if(!is.null(by) && !is_cross_by(by)) as_join_by(by), copy = copy, na_matches = na_matches)),
{
out <- rel_join_impl(x, y, by, "anti", na_matches, error_call = error_call)
return(out)
Expand Down
18 changes: 8 additions & 10 deletions R/count.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ count.duckplyr_df <- function(x, ..., wt = NULL, sort = FALSE, name = NULL, .dro
by_exprs <- unname(map(by, quo_get_expr))
is_name <- map_lgl(by_exprs, is_symbol)

rel_try(call = list(name = "count", x = x, args = list(dots = enquos(...), wt = enquo(wt), sort = sort, name = if (!is.null(name)) sym(name), .drop = .drop)),
by_chr <- map_chr(by_exprs, as_string)
name_was_null <- is.null(name)
name <- check_n_name(name, by_chr, call = dplyr_error_call())

n <- tally_n(x, {{ wt }})

rel_try(call = list(name = "count", x = x, args = list(dots = enquos(...), wt = enquo(wt), sort = sort, name = if (!name_was_null) sym(name), .drop = .drop)),
"count() needs all(is_name)" = !all(is_name),
"count() only implemented for .drop = TRUE" = !.drop,
"count() only implemented for sort = FALSE" = sort,
"Name clash in count()" = (name %in% by_chr),
{
by_chr <- map_chr(by_exprs, as_string)
name <- check_n_name(name, by_chr)

if (name %in% by_chr) {
cli::cli_abort("Name clash in `count()`")
}

n <- tally_n(x, {{ wt }})

rel <- duckdb_rel_from_df(x)

groups <- rel_translate_dots(by, x)
Expand Down
2 changes: 1 addition & 1 deletion R/full_join.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ full_join.duckplyr_df <- function(x, y, by = NULL, copy = FALSE, suffix = c(".x"
y <- auto_copy(x, y, copy = copy)

# Our implementation
rel_try(call = list(name = "full_join", x = x, y = y, args = list(by = if(!is.null(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, relationship = relationship)),
rel_try(call = list(name = "full_join", x = x, y = y, args = list(by = if(!is.null(by) && !is_cross_by(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, relationship = relationship)),
"No implicit cross joins for full_join()" = is_cross_by(by),
{
out <- rel_join_impl(x, y, by, "full", na_matches, suffix, keep, error_call)
Expand Down
2 changes: 1 addition & 1 deletion R/inner_join.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ inner_join.duckplyr_df <- function(x, y, by = NULL, copy = FALSE, suffix = c(".x
y <- auto_copy(x, y, copy = copy)

# Our implementation
rel_try(call = list(name = "inner_join", x = x, y = y, args = list(by = if(!is.null(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, unmatched = unmatched, relationship = relationship)),
rel_try(call = list(name = "inner_join", x = x, y = y, args = list(by = if(!is.null(by) && !is_cross_by(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, unmatched = unmatched, relationship = relationship)),
"No implicit cross joins for inner_join()" = is_cross_by(by),
{
out <- rel_join_impl(x, y, by, "inner", na_matches, suffix, keep, error_call)
Expand Down
2 changes: 1 addition & 1 deletion R/left_join.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ left_join.duckplyr_df <- function(x, y, by = NULL, copy = FALSE, suffix = c(".x"
y <- auto_copy(x, y, copy = copy)

# Our implementation
rel_try(call = list(name = "left_join", x = x, y = y, args = list(by = if(!is.null(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, unmatched = unmatched, relationship = relationship)),
rel_try(call = list(name = "left_join", x = x, y = y, args = list(by = if(!is.null(by) && !is_cross_by(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, unmatched = unmatched, relationship = relationship)),
"No implicit cross joins for left_join()" = is_cross_by(by),
{
out <- rel_join_impl(x, y, by, "left", na_matches, suffix, keep, error_call)
Expand Down
4 changes: 4 additions & 0 deletions R/relational.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ rel_try <- function(rel, ..., call = NULL) {

stats$attempts <- stats$attempts + 1L

if (Sys.getenv("DUCKPLYR_TELEMETRY_PREP_TEST") == "TRUE") {
force(call)
}

if (Sys.getenv("DUCKPLYR_TELEMETRY_TEST") == "TRUE") {
force(call)
json <- call_to_json(
Expand Down
2 changes: 1 addition & 1 deletion R/right_join.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ right_join.duckplyr_df <- function(x, y, by = NULL, copy = FALSE, suffix = c(".x
y <- auto_copy(x, y, copy = copy)

# Our implementation
rel_try(call = list(name = "right_join", x = x, y = y, args = list(by = if(!is.null(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, unmatched = unmatched, relationship = relationship)),
rel_try(call = list(name = "right_join", x = x, y = y, args = list(by = if(!is.null(by) && !is_cross_by(by)) as_join_by(by), copy = copy, keep = keep, na_matches = na_matches, multiple = multiple, unmatched = unmatched, relationship = relationship)),
"No implicit cross joins for right_join()" = is_cross_by(by),
{
out <- rel_join_impl(x, y, by, "right", na_matches, suffix, keep, error_call)
Expand Down
2 changes: 1 addition & 1 deletion R/semi_join.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ semi_join.duckplyr_df <- function(x, y, by = NULL, copy = FALSE, ..., na_matches
na_matches <- check_na_matches(na_matches, error_call = error_call)

# Our implementation
rel_try(call = list(name = "semi_join", x = x, y = y, args = list(by = if(!is.null(by)) as_join_by(by), copy = copy, na_matches = na_matches)),
rel_try(call = list(name = "semi_join", x = x, y = y, args = list(by = if(!is.null(by) && !is_cross_by(by)) as_join_by(by), copy = copy, na_matches = na_matches)),
{
out <- rel_join_impl(x, y, by, "semi", na_matches, error_call = error_call)
return(out)
Expand Down
12 changes: 5 additions & 7 deletions tests/testthat/_snaps/count-tally.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
Code
duckplyr_count(df, x, name = 1)
Condition
Error in `tally()`:
Error in `count()`:
! `name` must be a single string, not the number 1.

---

Code
duckplyr_count(df, x, name = letters)
Condition
Error in `tally()`:
Error in `count()`:
! `name` must be a single string, not a character vector.

# can only explicitly chain together multiple tallies
Expand Down Expand Up @@ -45,11 +45,9 @@
Code
(expect_error(duckplyr_count(mtcars, new = 1 + "")))
Output
<error/dplyr:::mutate_error>
Error in `group_by()`:
i In argument: `new = 1 + ""`.
Caused by error in `1 + ""`:
! non-numeric argument to binary operator
<error/rlang_error>
Error in `FUN()`:
! Can't convert a call to a string.
Code
(expect_error(duckplyr_count(mtcars, wt = 1 + "")))
Output
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ withr::local_envvar(DUCKPLYR_FALLBACK_COLLECT = 0, .local_envir = testthat::tear

withr::local_envvar(DUCKPLYR_OUTPUT_ORDER = TRUE, .local_envir = testthat::teardown_env())

# withr::local_envvar(DUCKPLYR_TELEMETRY_PREP_TEST = TRUE, .local_envir = testthat::teardown_env())

# withr::local_envvar(DUCKPLYR_FORCE = TRUE, .local_envir = testthat::teardown_env())

withr::defer(envir = testthat::teardown_env(), {
Expand Down

0 comments on commit a294979

Please sign in to comment.