Skip to content

Commit

Permalink
audit calls to expect_error() for backend-*.R files (#1553)
Browse files Browse the repository at this point in the history
* audit `expect_error()` for `backend-.R`
* audit `expect_error()` for `backend-access.R`
* audit `expect_error()` for `backend-mssql.R`
* audit `expect_error()` for `backend-oracle.R`
* audit `expect_error()` for `backend-postgres.R`
* audit `expect_error()` for `backend-redshift.R`
* audit `expect_error()` for `backend-spark-sql.R`

also, subclass widely-used error.
  • Loading branch information
simonpcouch authored Oct 31, 2024
1 parent ad8a9d9 commit d0994d1
Show file tree
Hide file tree
Showing 19 changed files with 364 additions and 80 deletions.
5 changes: 4 additions & 1 deletion R/translate-sql-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ sql_not_supported <- function(f) {
check_string(f)

function(...) {
cli_abort("{.fun {f}} is not available in this SQL variant.")
cli_abort(
"{.fun {f}} is not available in this SQL variant.",
class = "dbplyr_error_unsupported_fn"
)
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/backend-.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# basic arithmetic is correct

Code
test_translate_sql(100L %/% 3L)
Condition
Error in `100L %/% 3L`:
! `%/%()` is not available in this SQL variant.

# can translate subsetting

Code
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/backend-access.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# custom scalar translated correctly

Code
test_translate_sql(paste(x, collapse = "-"))
Condition
Error in `check_collapse()`:
! `collapse` not supported in DB translation of `paste()`.
i Please use `str_flatten()` instead.

# queries translate correctly

Code
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/_snaps/backend-mssql.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@
! `abbr = TRUE` isn't supported in SQL Server translation.
i It must be FALSE instead.

---

Code
test_translate_sql(quarter(x, fiscal_start = 5))
Condition
Error in `quarter()`:
! `fiscal_start = 5` isn't supported in SQL Server translation.
i It must be 1 instead.

# custom clock functions translated correctly

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "year"))
Condition
Error in `date_count_between()`:
! `precision` must be "day" on SQL backends.

---

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5))
Condition
Error in `date_count_between()`:
! `n` must be "1" on SQL backends.

# difftime is translated correctly

Code
test_translate_sql(difftime(start_date, end_date, units = "auto"))
Condition
Error in `difftime()`:
! The only supported value for `units` on SQL backends is "days"

---

Code
test_translate_sql(difftime(start_date, end_date, tz = "UTC", units = "days"))
Condition
Error in `difftime()`:
! The `tz` argument is not supported for SQL backends.

# convert between bit and boolean as needed

Code
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/backend-oracle.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,19 @@
SELECT 1, '{1,2,3}' FROM DUAL
) `values_table`

# difftime is translated correctly

Code
test_translate_sql(difftime(start_date, end_date, units = "auto"))
Condition
Error in `difftime()`:
! The only supported value for `units` on SQL backends is "days"

---

Code
test_translate_sql(difftime(start_date, end_date, tz = "UTC", units = "days"))
Condition
Error in `difftime()`:
! The `tz` argument is not supported for SQL backends.

64 changes: 57 additions & 7 deletions tests/testthat/_snaps/backend-postgres.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,68 @@
# pasting translated correctly

Code
test_translate_sql(paste0(x, collapse = ""), window = FALSE)
Condition
Error in `check_collapse()`:
! `collapse` not supported in DB translation of `paste()`.
i Please use `str_flatten()` instead.

# custom lubridate functions translated correctly

Code
test_translate_sql(quarter(x, fiscal_start = 2))
Condition
Error in `quarter()`:
! `fiscal_start = 2` isn't supported in PostgreSQL translation.
i It must be 1 instead.

# custom clock functions translated correctly

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "year"))
Condition
Error in `date_count_between()`:
! `precision` must be "day" on SQL backends.

---

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5))
Condition
Error in `date_count_between()`:
! `n` must be "1" on SQL backends.

# difftime is translated correctly

Code
test_translate_sql(difftime(start_date, end_date, units = "auto"))
Condition
Error in `difftime()`:
! The only supported value for `units` on SQL backends is "days"

---

Code
test_translate_sql(difftime(start_date, end_date, tz = "UTC", units = "days"))
Condition
Error in `difftime()`:
! The `tz` argument is not supported for SQL backends.

# custom window functions translated correctly

Code
(expect_error(test_translate_sql(quantile(x, 0.3, na.rm = TRUE), window = TRUE))
)
Output
<error/rlang_error>
test_translate_sql(quantile(x, 0.3, na.rm = TRUE), window = TRUE)
Condition
Error in `quantile()`:
! Translation of `quantile()` in `mutate()` is not supported for PostgreSQL.
i Use a combination of `summarise()` and `left_join()` instead:
`df %>% left_join(summarise(<col> = quantile(x, 0.3, na.rm = TRUE)))`.

---

Code
(expect_error(test_translate_sql(median(x, na.rm = TRUE), window = TRUE)))
Output
<error/rlang_error>
test_translate_sql(median(x, na.rm = TRUE), window = TRUE)
Condition
Error in `median()`:
! Translation of `median()` in `mutate()` is not supported for PostgreSQL.
i Use a combination of `summarise()` and `left_join()` instead:
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/_snaps/backend-redshift.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,35 @@
SELECT 1, '{1,2,3}'
) AS `values_table`

# custom clock functions translated correctly

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "year"))
Condition
Error in `date_count_between()`:
! `precision` must be "day" on SQL backends.

---

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5))
Condition
Error in `date_count_between()`:
! `n` must be "1" on SQL backends.

# difftime is translated correctly

Code
test_translate_sql(difftime(start_date, end_date, units = "auto"))
Condition
Error in `difftime()`:
! The only supported value for `units` on SQL backends is "days"

---

Code
test_translate_sql(difftime(start_date, end_date, tz = "UTC", units = "days"))
Condition
Error in `difftime()`:
! The `tz` argument is not supported for SQL backends.

32 changes: 32 additions & 0 deletions tests/testthat/_snaps/backend-spark-sql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# custom clock functions translated correctly

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "year"))
Condition
Error in `date_count_between()`:
! `precision` must be "day" on SQL backends.

---

Code
test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5))
Condition
Error in `date_count_between()`:
! `n` must be "1" on SQL backends.

# difftime is translated correctly

Code
test_translate_sql(difftime(start_date, end_date, units = "auto"))
Condition
Error in `difftime()`:
! The only supported value for `units` on SQL backends is "days"

---

Code
test_translate_sql(difftime(start_date, end_date, tz = "UTC", units = "days"))
Condition
Error in `difftime()`:
! The `tz` argument is not supported for SQL backends.

17 changes: 0 additions & 17 deletions tests/testthat/_snaps/backend-sqlite.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
# custom aggregates translated

Code
(expect_error(test_translate_sql(quantile(x, 0.5, na.rm = TRUE), window = FALSE))
)
Output
<error/rlang_error>
Error in `quantile()`:
! `quantile()` is not available in this SQL variant.
Code
(expect_error(test_translate_sql(quantile(x, 0.5, na.rm = TRUE), window = TRUE))
)
Output
<error/rlang_error>
Error in `quantile()`:
! `quantile()` is not available in this SQL variant.

# custom SQL translation

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/translate-sql-helpers.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# warns informatively with unsupported function

Code
sql_not_supported("cor")()
Condition
Error:
! `cor()` is not available in this SQL variant.

# duplicates throw an error

Code
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-backend-.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_that("basic arithmetic is correct", {
expect_equal(test_translate_sql(5 ^ 2), sql("POWER(5.0, 2.0)"))
expect_equal(test_translate_sql(100L %% 3L), sql("100 % 3"))

expect_error(test_translate_sql(100L %/% 3L), "not available")
expect_snapshot(error = TRUE, test_translate_sql(100L %/% 3L))
})

test_that("small numbers aren't converted to 0", {
Expand Down
17 changes: 13 additions & 4 deletions tests/testthat/test-backend-access.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test_that("custom scalar translated correctly", {
# paste()
expect_equal(test_translate_sql(paste(x, y, sep = "+")), sql("`x` & '+' & `y`"))
expect_equal(test_translate_sql(paste0(x, y)), sql("`x` & `y`"))
expect_error(test_translate_sql(paste(x, collapse = "-")),"`collapse` not supported")
expect_snapshot(error = TRUE, test_translate_sql(paste(x, collapse = "-")))
# Logic
expect_equal(test_translate_sql(ifelse(x, "true", "false")), sql("IIF(`x`, 'true', 'false')"))
})
Expand All @@ -45,9 +45,18 @@ test_that("custom aggregators translated correctly", {
expect_equal(test_translate_sql(sd(x, na.rm = TRUE), window = FALSE), sql("STDEV(`x`)"))
expect_equal(test_translate_sql(var(x, na.rm = TRUE), window = FALSE), sql("VAR(`x`)"))

expect_error(test_translate_sql(cor(x), window = FALSE), "not available")
expect_error(test_translate_sql(cov(x), window = FALSE), "not available")
expect_error(test_translate_sql(n_distinct(x), window = FALSE), "not available")
expect_error(
test_translate_sql(cor(x), window = FALSE),
class = "dbplyr_error_unsupported_fn"
)
expect_error(
test_translate_sql(cov(x), window = FALSE),
class = "dbplyr_error_unsupported_fn"
)
expect_error(
test_translate_sql(n_distinct(x), window = FALSE),
class = "dbplyr_error_unsupported_fn"
)
})

test_that("custom escaping works as expected", {
Expand Down
Loading

0 comments on commit d0994d1

Please sign in to comment.