Skip to content

Commit

Permalink
tests: update snapshots and error code
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Dec 21, 2023
1 parent fdcce0c commit 96202d9
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 39 deletions.
9 changes: 6 additions & 3 deletions R/sparql-protection.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' expression.
#' @export
spq <- function(...) {
x <- c_character(...)
x <- c_character(..., call = rlang::caller_env())
structure(x, class = c("spq", "character"))
}

Expand All @@ -30,14 +30,17 @@ format.spq <- function(x, ...) {
#' @export
is.spq <- function(x) inherits(x, "spq")

c_character <- function(...) {
c_character <- function(..., call) {
x <- c(...)
if (length(x) == 0) {
return(character())
}

if (!is.character(x)) {
rlang::abort("Character input expected")
cli::cli_abort(
"Character input expected",
call = call
)
}

x
Expand Down
10 changes: 7 additions & 3 deletions R/spq_assemble.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ spq_assemble = function(.query, strict = TRUE) {
# prefixes -----
prefixes_known = .query[["prefixes_provided"]] %>%
dplyr::bind_rows(usual_prefixes)
check_prefixes(.query[["prefixes_used"]], prefixes_known = prefixes_known)
check_prefixes(
.query[["prefixes_used"]],
prefixes_known = prefixes_known,
call = rlang::caller_env()
)

part_prefixes <- if (nrow(.query[["prefixes_provided"]]) > 0) {
glue::glue(
Expand Down Expand Up @@ -249,6 +253,6 @@ add_label = function(vector, label, label_name, old_select) {

}

check_prefixes <- function(prefixes, prefixes_known) {
purrr::walk(prefixes, check_prefix, prefixes_known = prefixes_known)
check_prefixes <- function(prefixes, prefixes_known, call) {
purrr::walk(prefixes, check_prefix, prefixes_known = prefixes_known, call = call)
}
8 changes: 4 additions & 4 deletions R/spq_select.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) {

if (length(plus_variables) > 0) {

check_variables_present(.query, plus_variables)
check_variables_present(.query, plus_variables, call = rlang::caller_env())

if (is.data.frame(.query[["structure"]])) {
.query[["structure"]][["selected"]] = FALSE
Expand All @@ -65,7 +65,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) {
str_remove("\\-")

if (length(minus_variables) > 0) {
check_variables_present(.query, minus_variables)
check_variables_present(.query, minus_variables, call = rlang::caller_env())

.query = purrr::reduce(
minus_variables,
Expand All @@ -77,7 +77,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) {
return(.query)
}

check_variables_present <- function(query, variables) {
check_variables_present <- function(query, variables, call) {

if (nzchar(Sys.getenv("GLITTER.TESTING.SELECT"))) {
return()
Expand All @@ -89,6 +89,6 @@ check_variables_present <- function(query, variables) {
cli::cli_abort(c(
"Can't use {.fun spq_select} on absent variables: {toString(absent_variables)}.",
i = "Did you forget a call to {.fun spq_add}, {.fun spq_mutate} or {.fun spq_label}?"
))
), call = call)
}
}
5 changes: 3 additions & 2 deletions R/treat-argument.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ spq_translate_dsl <- function(code) {
# Abort if not sparqlish
not_sparqlish = xml2::xml_find_all(code_data, ".//SYMBOL_FUNCTION_CALL[@sparqlish='false']")
if (length(not_sparqlish) > 0) {
rlang::abort(
cli::cli_abort(
c(
x = sprintf(
"Can't find SPARQL equivalent for %s().",
Expand All @@ -67,7 +67,8 @@ spq_translate_dsl <- function(code) {
)
),
i = "If you think there should be one, open an issue in https://github.com/lvaudor/glitter."
)
),
call = NULL
)
}

Expand Down
2 changes: 1 addition & 1 deletion R/utils-str.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ is_value = function(string){
#' @examples
#' check_prefix(prefixes_used=c("wd","wdt"), prefixes_known = usual_prefixes) # TRUE
#' check_prefix("blop:blabla", prefixes_known=usual_prefixes) #returns error message
check_prefix = function(prefixes_used, prefixes_known) {
check_prefix = function(prefixes_used, prefixes_known, call = NULL) {
unknown_prefixes <- prefixes_used[!(prefixes_used %in% prefixes_known$name)]
if (length(unknown_prefixes) == 0) {
return(TRUE)
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/_snaps/sparql-protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

Code
spq(1)
Error <rlang_error>
Character input expected
Condition
Error:
! Character input expected

5 changes: 3 additions & 2 deletions tests/testthat/_snaps/spq_assemble.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@

Code
spq_init() %>% spq_filter(lang(itemTitleLOOKTYPO) == "en") %>% spq_assemble()
Error <rlang_error>
Can't filter on undefined variables: ?itemTitleLOOKTYPO
Condition
Error in `spq_assemble()`:
! Can't filter on undefined variables: ?itemTitleLOOKTYPO
i You haven't mentioned them in any triple, VALUES, mutate.

# spq_assemble() called from printing isn't strict
Expand Down
25 changes: 15 additions & 10 deletions tests/testthat/_snaps/spq_control_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,42 @@

Code
spq_control_request(timeout = "ahahah")
Error <rlang_error>
Must provide an integer as `timeout`.
Condition
Error in `spq_control_request()`:
! Must provide an integer as `timeout`.
i You provided a "character".

---

Code
spq_control_request(max_tries = "ahahah")
Error <rlang_error>
Must provide an integer as `max_tries`.
Condition
Error in `spq_control_request()`:
! Must provide an integer as `max_tries`.
i You provided a "character".

---

Code
spq_control_request(max_seconds = "ahahah")
Error <rlang_error>
Must provide an integer as `max_seconds`.
Condition
Error in `spq_control_request()`:
! Must provide an integer as `max_seconds`.
i You provided a "character".

---

Code
spq_control_request(request_type = "ahahah")
Error <rlang_error>
`request_type` must be one of "url" or "body-form", not "ahahah".
Condition
Error in `spq_control_request()`:
! `request_type` must be one of "url" or "body-form", not "ahahah".

---

Code
spq_control_request(user_agent = 42)
Error <rlang_error>
Must provide a character as `user_agent`.
Condition
Error in `spq_control_request()`:
! Must provide a character as `user_agent`.

5 changes: 3 additions & 2 deletions tests/testthat/_snaps/spq_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

Code
spq_init(request_control = list(max_tries = 1L))
Error <rlang_error>
`request_control` must be created by `spq_control_request()`.
Condition
Error in `spq_init()`:
! `request_control` must be created by `spq_control_request()`.

3 changes: 2 additions & 1 deletion tests/testthat/_snaps/spq_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

Code
spq_init() %>% spq_language("en")
Warning <lifecycle_warning_deprecated>
Condition
Warning:
`spq_language()` was deprecated in glitter 0.2.0.
i Please use `spq_label()` instead.
i See the `.languages` argument
Expand Down
14 changes: 8 additions & 6 deletions tests/testthat/_snaps/spq_select.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@

i In index: 1.
i With name: birthyear.
Caused by error in `spq_translate_dsl()`:
! x Can't find SPARQL equivalent for collapse().
Caused by error:
x Can't find SPARQL equivalent for collapse().
i If you think there should be one, open an issue in https://github.com/lvaudor/glitter.

# spq_select can use DISTINCT and REDUCED
Expand Down Expand Up @@ -158,8 +158,9 @@
spq_init() %>% spq_add("?station wdt:P16 wd:Q1552") %>% spq_add(
"?station wdt:P31 wd:Q928830") %>% spq_add("?station wdt:P625 ?coords") %>%
spq_select(station_label, blop)
Error <rlang_error>
Can't use `spq_select()` on absent variables: ?station_label, ?blop.
Condition
Error:
! Can't use `spq_select()` on absent variables: ?station_label, ?blop.
i Did you forget a call to `spq_add()`, `spq_mutate()` or `spq_label()`?

---
Expand All @@ -168,7 +169,8 @@
spq_init() %>% spq_add("?station wdt:P16 wd:Q1552") %>% spq_add(
"?station wdt:P31 wd:Q928830") %>% spq_add("?station wdt:P625 ?coords") %>%
spq_label(station) %>% spq_select(station_label, blop)
Error <rlang_error>
Can't use `spq_select()` on absent variables: ?blop.
Condition
Error:
! Can't use `spq_select()` on absent variables: ?blop.
i Did you forget a call to `spq_add()`, `spq_mutate()` or `spq_label()`?

3 changes: 2 additions & 1 deletion tests/testthat/_snaps/treat-argument.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

Code
spq_treat_argument("something(bla)")
Error <rlang_error>
Condition
Error:
x Can't find SPARQL equivalent for something().
i If you think there should be one, open an issue in https://github.com/lvaudor/glitter.

Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/_snaps/utils-str.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Code
check_prefix("blop", usual_prefixes)
Error <simpleError>
Can't find prefix(es) blop. Please use spq_prefix().
Condition
Error in `check_prefix()`:
! Can't find prefix(es) blop. Please use spq_prefix().

0 comments on commit 96202d9

Please sign in to comment.