Skip to content

Commit

Permalink
implicit prefixes
Browse files Browse the repository at this point in the history
Update R/spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update R/spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update R/spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update R/spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update R/spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update R/spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update tests/testthat/test-spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update tests/testthat/test-spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update tests/testthat/test-spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

Update tests/testthat/test-spq_perform.R

Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>

update tests

uncomment test
  • Loading branch information
lvaudor authored and maelle committed Dec 21, 2023
1 parent cdce3e0 commit fdcce0c
Show file tree
Hide file tree
Showing 31 changed files with 1,182 additions and 563 deletions.
11 changes: 10 additions & 1 deletion R/spq_perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,17 @@ spq_perform = function(.query,
)

if (replace_prefixes) {
endpoint_url = .query[["endpoint_info"]][["endpoint_url"]]
endpoint_is_usual <- (endpoint_url %in% usual_endpoints$url)
if (endpoint_is_usual) {
endpoint_name = usual_endpoints[["name"]][usual_endpoints[["url"]] == endpoint_url]
prefixes = usual_prefixes[["name"]][usual_prefixes[["type"]] == endpoint_name]
} else {
prefixes = NULL
}
prefixes = c(prefixes, .query[["prefixes_used"]])
results = purrr::reduce(
.query[["prefixes_used"]],
prefixes,
\(results, x) replace_prefix(x, results, .query = .query),
.init = results
)
Expand Down
14 changes: 7 additions & 7 deletions data-raw/usual_prefixes.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type,name,url
Wikidata,wd,http://www.wikidata.org/entity/
Wikidata,wdt,http://www.wikidata.org/prop/direct/
Wikidata,ps,http://www.wikidata.org/prop/statement/
Wikidata,psv,http://www.wikidata.org/prop/statement/value/
Wikidata,pq,http://www.wikidata.org/prop/qualifier/
Wikidata,p,http://www.wikidata.org/prop/
Wikidata,wikibase,http://wikiba.se/ontology#
wikidata,wd,http://www.wikidata.org/entity/
wikidata,wdt,http://www.wikidata.org/prop/direct/
wikidata,ps,http://www.wikidata.org/prop/statement/
wikidata,psv,http://www.wikidata.org/prop/statement/value/
wikidata,pq,http://www.wikidata.org/prop/qualifier/
wikidata,p,http://www.wikidata.org/prop/
wikidata,wikibase,http://wikiba.se/ontology#
dbpedia,dbo,http://dbpedia.org/ontology/
generic,foaf,http://xmlns.com/foaf/0.1/
generic,rdfs,http://www.w3.org/2000/01/rdf-schema#
Expand Down
Binary file modified data/usual_prefixes.rda
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/build_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
spq_mutate(coords = wdt::P625(city), .within_distance = list(center = c(long = 4.84,
lat = 45.76), radius = 5))
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?city (COALESCE(?city_labell,'') AS ?city_label) ?coords
WHERE {
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/send_sparql.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@
The `user_agent` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `user_agent` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `max_tries` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `max_tries` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `max_seconds` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `max_seconds` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `timeout` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `timeout` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `request_type` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `request_type` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Code
req$method
Output
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/_snaps/sparql-protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/spq_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Code
spq_init() %>% spq_add(spq("?software p:P348/pq:P577 ?date"))
Output
PREFIX p: <http://www.wikidata.org/prop/>
SELECT ?date ?software
WHERE {
Expand All @@ -17,7 +17,7 @@
Code
spq_init() %>% spq_add(spq("?software pq:P577 ?date."))
Output
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT ?date ?software
WHERE {
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/_snaps/spq_assemble.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
spq_summarise(subject_label_concat = str_c(subject_label, sep = "; ")) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?film (GROUP_CONCAT(?subject_label;SEPARATOR="; ") AS ?subject_label_concat)
WHERE {
Expand All @@ -30,6 +32,8 @@
spq_init() %>% spq_add("?item wdt:P31 wd:Q13442814") %>% spq_label(item) %>%
spq_filter(str_detect(str_to_lower(item_label), "wikidata")) %>% spq_head(n = 5)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item (COALESCE(?item_labell,'') AS ?item_label)
WHERE {
Expand All @@ -50,9 +54,8 @@

Code
spq_init() %>% spq_filter(lang(itemTitleLOOKTYPO) == "en") %>% spq_assemble()
Condition
Error in `spq_assemble()`:
! Can't filter on undefined variables: ?itemTitleLOOKTYPO
Error <rlang_error>
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: 10 additions & 15 deletions tests/testthat/_snaps/spq_control_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,37 @@

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

---

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

---

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

---

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

---

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

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/spq_filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"CONTAINS(LCASE(?itemTitle),'wikidata')")) %>% spq_filter(spq(
"LANG(?itemTitle)='en'"))
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item ?itemTitle
WHERE {
Expand All @@ -24,6 +26,8 @@
"?item rdfs:label ?itemTitle") %>% spq_filter(str_detect(str_to_lower(
itemTitle), "wikidata")) %>% spq_filter(lang(itemTitle) == "en")
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item ?itemTitle
WHERE {
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/_snaps/spq_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
narrative_location = wdt::P840(film)) %>% spq_label(film, narrative_location) %>%
spq_count(narrative_location_label, sort = TRUE, name = "n_films")
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?narrative_location_label (COUNT(*) AS ?n_films)
WHERE {
Expand All @@ -31,7 +33,6 @@

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

19 changes: 19 additions & 0 deletions tests/testthat/_snaps/spq_label.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
spq_add("?node ps:P39 wd:Q30185") %>% spq_add("?node pq:P642 ?place") %>%
spq_label(mayor, place, .languages = "en$")
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?mayor (COALESCE(?mayor_labell,'') AS ?mayor_label) ?node ?place (COALESCE(?place_labell,'') AS ?place_label) ?species
WHERE {
Expand Down Expand Up @@ -37,6 +42,11 @@
spq_add("?node ps:P39 wd:Q30185") %>% spq_add("?node pq:P642 ?place") %>%
spq_label(mayor, place, .languages = c("fr", "en"))
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?mayor (COALESCE(?mayor_labell,'') AS ?mayor_label) (lang(?mayor_labell) AS ?mayor_label_lang) ?node ?place (COALESCE(?place_labell,'') AS ?place_label) (lang(?place_labell) AS ?place_label_lang) ?species
WHERE {
Expand Down Expand Up @@ -92,6 +102,11 @@
spq_add("?node ps:P39 wd:Q30185") %>% spq_add("?node pq:P642 ?place") %>%
spq_label(mayor, place, .languages = "en$", .overwrite = TRUE)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?node ?species (COALESCE(?mayor_labell,'') AS ?mayor) (COALESCE(?place_labell,'') AS ?place)
WHERE {
Expand Down Expand Up @@ -146,6 +161,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_label(film, loc, subject) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?coords ?date ?film (COALESCE(?film_labell,'') AS ?film_label) ?image ?loc (COALESCE(?loc_labell,'') AS ?loc_label) ?subject (COALESCE(?subject_labell,'') AS ?subject_label)
WHERE {
Expand Down Expand Up @@ -188,6 +205,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_label(film, loc, subject, .required = TRUE) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?coords ?date ?film (COALESCE(?film_labell,'') AS ?film_label) ?image ?loc (COALESCE(?loc_labell,'') AS ?loc_label) ?subject (COALESCE(?subject_labell,'') AS ?subject_label)
WHERE {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/_snaps/spq_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

Code
spq_init() %>% spq_language("en")
Condition
Warning:
Warning <lifecycle_warning_deprecated>
`spq_language()` was deprecated in glitter 0.2.0.
i Please use `spq_label()` instead.
i See the `.languages` argument
Expand Down
7 changes: 6 additions & 1 deletion tests/testthat/_snaps/spq_mutate.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
spq_init() %>% spq_mutate(statement = wdt::P1843(wd::Q331676)) %>% spq_mutate(
lang = lang(statement))
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?statement (lang(?statement) AS ?lang)
WHERE {
Expand All @@ -20,6 +21,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_mutate(date = year(date)) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?film (COALESCE(?film_labell,'') AS ?film_label) (YEAR(?date0) AS ?date)
WHERE {
Expand All @@ -43,6 +46,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_mutate(date = year(date)) %>%
spq_mutate(date = date - 2000)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?film (COALESCE(?film_labell,'') AS ?film_label) (?date0-2000 AS ?date)
WHERE {
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/_snaps/spq_offset.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"?item wikibase:sitelinks ?linkcount") %>% spq_arrange(desc(linkcount)) %>%
spq_head(42) %>% spq_offset(11)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?linkcount
WHERE {
Expand Down
10 changes: 4 additions & 6 deletions tests/testthat/_snaps/spq_select.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@
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)
Condition
Error in `check_variables_present()`:
! Can't use `spq_select()` on absent variables: ?station_label, ?blop.
Error <rlang_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 @@ -169,8 +168,7 @@
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)
Condition
Error in `check_variables_present()`:
! Can't use `spq_select()` on absent variables: ?blop.
Error <rlang_error>
Can't use `spq_select()` on absent variables: ?blop.
i Did you forget a call to `spq_add()`, `spq_mutate()` or `spq_label()`?

Loading

0 comments on commit fdcce0c

Please sign in to comment.