Skip to content

Commit

Permalink
fix: uncomment test on using embrace operator
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Sep 27, 2024
1 parent bc03d7c commit 54af305
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 109 deletions.
14 changes: 9 additions & 5 deletions R/utils-expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ translate <- function(
}
)

user_defined <- get_globenv_functions(caller = caller)
user_defined <- get_user_defined_functions(caller = caller)
known_ops <- c("+", "-", "*", "/", "^", "**", ">", ">=", "<", "<=", "==", "!=",
"&", "|", "!", "%%", "%/%")
fn_names <- add_pkg_suffix(name, known_ops, user_defined)
Expand Down Expand Up @@ -627,13 +627,17 @@ polars_col <- function(x) {
polars::pl$col(x)
}

# Look for user-defined functions in the global environment
get_globenv_functions <- function(caller) {
get_user_defined_functions <- function(caller) {
# browser()
x <- ls(caller)
list_fns <- list()
for (i in x) {
foo <- get(i, envir = caller)
if (is.function(foo)) {
foo <- tryCatch(
env_get(nm = i, env = caller),
warning = function(w) invisible(),
error = function(e) NULL
)
if (!is.null(foo) && is.function(foo)) {
list_fns[[i]] <- i
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/replace_na-lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
! Execution halted with the following contexts
0: In R: in $collect():
1: Encountered the following error in Rust-Polars:
conversion from `str` to `i32` failed in column 'literal' for 1 out of 1 values: ["a"]
conversion from `str` to `f64` failed in column 'literal' for 1 out of 1 values: ["a"]

---

Expand All @@ -18,5 +18,5 @@
! Execution halted with the following contexts
0: In R: in $collect():
1: Encountered the following error in Rust-Polars:
conversion from `str` to `i32` failed in column 'literal' for 1 out of 1 values: ["unknown"]
conversion from `str` to `f64` failed in column 'literal' for 1 out of 1 values: ["unknown"]

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/replace_na.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
! Execution halted with the following contexts
0: In R: in $with_columns()
1: Encountered the following error in Rust-Polars:
conversion from `str` to `i32` failed in column 'literal' for 1 out of 1 values: ["a"]
conversion from `str` to `f64` failed in column 'literal' for 1 out of 1 values: ["a"]

---

Expand All @@ -18,5 +18,5 @@
! Execution halted with the following contexts
0: In R: in $with_columns()
1: Encountered the following error in Rust-Polars:
conversion from `str` to `i32` failed in column 'literal' for 1 out of 1 values: ["unknown"]
conversion from `str` to `f64` failed in column 'literal' for 1 out of 1 values: ["unknown"]

97 changes: 48 additions & 49 deletions tests/testthat/test-indirection-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,53 @@

Sys.setenv('TIDYPOLARS_TEST' = TRUE)

# TODO: fix that
# test_that("basic behavior works", {
# test <- pl$LazyFrame(x = c(1, 1, 1), y = 4:6, z = c("a", "a", "b"))
#
# add_one <- function(data, add_col) {
# data |>
# mutate(new_col = {{ add_col }} + 1)
# }
#
# add_two <- function(data, add_col) {
# data |>
# mutate("{{add_col}}_plus_2" := {{ add_col }} + 2)
# }
#
# var_summary <- function(data, var) {
# data |>
# summarise(min = min({{ var }}), max = max({{ var }}))
# }
#
# expect_equal_lazy(
# test |>
# add_one(x) |>
# pull(new_col),
# rep(2, 3)
# )
#
# expect_equal_lazy(
# test |>
# add_two(x) |>
# pull(x_plus_2),
# rep(3, 3)
# )
#
# pl_mtcars <- pl$LazyFrame(mtcars)
#
# out <- pl_mtcars |>
# group_by(cyl) |>
# var_summary(mpg)
#
# expect_equal_lazy(
# out |> arrange(min) |> pull(min),
# c(10.4, 17.8, 21.4)
# )
#
# expect_equal_lazy(
# out |> arrange(max) |> pull(max),
# c(19.2, 21.4, 33.9)
# )
# })
test_that("basic behavior works", {
test <- pl$LazyFrame(x = c(1, 1, 1), y = 4:6, z = c("a", "a", "b"))

add_one <- function(data, add_col) {
data |>
mutate(new_col = {{ add_col }} + 1)
}

add_two <- function(data, add_col) {
data |>
mutate("{{add_col}}_plus_2" := {{ add_col }} + 2)
}

var_summary <- function(data, var) {
data |>
summarise(min = min({{ var }}), max = max({{ var }}))
}

expect_equal_lazy(
test |>
add_one(x) |>
pull(new_col),
rep(2, 3)
)

expect_equal_lazy(
test |>
add_two(x) |>
pull(x_plus_2),
rep(3, 3)
)

pl_mtcars <- pl$LazyFrame(mtcars)

out <- pl_mtcars |>
group_by(cyl) |>
var_summary(mpg)

expect_equal_lazy(
out |> arrange(min) |> pull(min),
c(10.4, 17.8, 21.4)
)

expect_equal_lazy(
out |> arrange(max) |> pull(max),
c(19.2, 21.4, 33.9)
)
})

Sys.setenv('TIDYPOLARS_TEST' = FALSE)
97 changes: 48 additions & 49 deletions tests/testthat/test-indirection.R
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
# TODO: fix that
# test_that("basic behavior works", {
# test <- pl$DataFrame(x = c(1, 1, 1), y = 4:6, z = c("a", "a", "b"))
#
# add_one <- function(data, add_col) {
# data |>
# mutate(new_col = {{ add_col }} + 1)
# }
#
# add_two <- function(data, add_col) {
# data |>
# mutate("{{add_col}}_plus_2" := {{ add_col }} + 2)
# }
#
# var_summary <- function(data, var) {
# data |>
# summarise(min = min({{ var }}), max = max({{ var }}))
# }
#
# expect_equal(
# test |>
# add_one(x) |>
# pull(new_col),
# rep(2, 3)
# )
#
# expect_equal(
# test |>
# add_two(x) |>
# pull(x_plus_2),
# rep(3, 3)
# )
#
# pl_mtcars <- pl$DataFrame(mtcars)
#
# out <- pl_mtcars |>
# group_by(cyl) |>
# var_summary(mpg)
#
# expect_equal(
# out |> arrange(min) |> pull(min),
# c(10.4, 17.8, 21.4)
# )
#
# expect_equal(
# out |> arrange(max) |> pull(max),
# c(19.2, 21.4, 33.9)
# )
# })
test_that("basic behavior works", {
test <- pl$DataFrame(x = c(1, 1, 1), y = 4:6, z = c("a", "a", "b"))

add_one <- function(data, add_col) {
data |>
mutate(new_col = {{ add_col }} + 1)
}

add_two <- function(data, add_col) {
data |>
mutate("{{add_col}}_plus_2" := {{ add_col }} + 2)
}

var_summary <- function(data, var) {
data |>
summarise(min = min({{ var }}), max = max({{ var }}))
}

expect_equal(
test |>
add_one(x) |>
pull(new_col),
rep(2, 3)
)

expect_equal(
test |>
add_two(x) |>
pull(x_plus_2),
rep(3, 3)
)

pl_mtcars <- pl$DataFrame(mtcars)

out <- pl_mtcars |>
group_by(cyl) |>
var_summary(mpg)

expect_equal(
out |> arrange(min) |> pull(min),
c(10.4, 17.8, 21.4)
)

expect_equal(
out |> arrange(max) |> pull(max),
c(19.2, 21.4, 33.9)
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-replace_na-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test_that("basic behavior works", {
})

test_that("error if original values and replacement have no supertype", {
pl_test <- polars::pl$LazyFrame(x = c(NA, 1), y = c(2L, NA_integer_))
pl_test <- polars::pl$LazyFrame(x = c(NA, 1), y = c(2, NA))
expect_snapshot_lazy(
replace_na(pl_test, "a"),
error = TRUE
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-replace_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("basic behavior works", {
})

test_that("error if original values and replacement have no supertype", {
pl_test <- polars::pl$DataFrame(x = c(NA, 1), y = c(2L, NA_integer_))
pl_test <- polars::pl$DataFrame(x = c(NA, 1), y = c(2, NA))
expect_snapshot(
replace_na(pl_test, "a"),
error = TRUE
Expand Down

0 comments on commit 54af305

Please sign in to comment.