Skip to content

Commit

Permalink
Merge branch 'main' into OPTIONAL
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Oct 20, 2023
2 parents 408fe6a + 2bbac18 commit f1da1cb
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 67 deletions.
File renamed without changes.
48 changes: 0 additions & 48 deletions tests/testthat/_snaps/build_sparql.md

This file was deleted.

48 changes: 48 additions & 0 deletions tests/testthat/_snaps/spq_assemble.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
# spq_assemble() works - bindings

Code
spq_init() %>% spq_add("?film wdt:P31 wd:Q11424") %>% spq_add(
"?film wdt:P921 ?subject") %>% spq_label(subject) %>% spq_group_by(film) %>%
spq_summarise(subject_label_concat = str_c(subject_label, sep = "; ")) %>%
spq_head(10)
Output
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?film (GROUP_CONCAT(?subject_label;SEPARATOR="; ") AS ?subject_label_concat)
WHERE {
?film wdt:P31 wd:Q11424.
?film wdt:P921 ?subject.
OPTIONAL {
?subject rdfs:label ?subject_labell.
FILTER(lang(?subject_labell) IN ('en'))
}
BIND(COALESCE(?subject_labell,'') AS ?subject_label)
}
GROUP BY ?film
LIMIT 10

---

Code
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 rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item (COALESCE(?item_labell,'') AS ?item_label)
WHERE {
?item wdt:P31 wd:Q13442814.
OPTIONAL {
?item rdfs:label ?item_labell.
FILTER(lang(?item_labell) IN ('en'))
}
BIND(COALESCE(?item_labell,'') AS ?item_label)
FILTER(REGEX(LCASE(?item_label),"wikidata"))
}
LIMIT 5

# spq_assemble() detects undefined variables in filter

Code
Expand Down
44 changes: 44 additions & 0 deletions tests/testthat/_snaps/spq_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,47 @@
}

# spq_set() in two examples

Code
spq_init() %>% spq_prefix(prefixes = c(schema = "http://schema.org/")) %>%
spq_set(lemma = c("'Wikipedia'@de", "'Wikidata'@de", "'Berlin'@de",
"'Technische Universität Berlin'@de")) %>% spq_add(
"?sitelink schema:about ?item") %>% spq_add(
"?sitelink schema:isPartOf <https://de.wikipedia.org/>") %>% spq_add(
"?sitelink schema:name ?lemma") %>% spq_select(lemma, item)
Output
PREFIX schema: <http://schema.org/>
SELECT ?item ?lemma
WHERE {
?sitelink schema:about ?item.
?sitelink schema:isPartOf <https://de.wikipedia.org/>.
?sitelink schema:name ?lemma.
VALUES ?lemma {'Wikipedia'@de 'Wikidata'@de 'Berlin'@de 'Technische Universität Berlin'@de}
}

---

Code
spq_init() %>% spq_prefix(prefixes = c(schema = "http://schema.org/")) %>%
spq_set(lemma = c("'Wikipedia'@de", "'Wikidata'@de", "'Berlin'@de",
"'Technische Universität Berlin'@de")) %>% spq_mutate(item = schema::about(
sitelink)) %>% spq_add(
"?sitelink schema:isPartOf <https://de.wikipedia.org/>") %>% spq_mutate(
lemma = schema::name(sitelink)) %>% spq_select(lemma, item)
Output
PREFIX schema: <http://schema.org/>
SELECT ?item ?lemma
WHERE {
?sitelink schema:about ?item.
?sitelink schema:isPartOf <https://de.wikipedia.org/>.
?sitelink schema:name ?lemma.
VALUES ?lemma {'Wikipedia'@de 'Wikidata'@de 'Berlin'@de 'Technische Universität Berlin'@de}
}

19 changes: 0 additions & 19 deletions tests/testthat/test-build_sparql.R
Original file line number Diff line number Diff line change
@@ -1,19 +0,0 @@
test_that("build_sparql() works - bindings", {
expect_snapshot(
spq_init() %>%
spq_add("?film wdt:P31 wd:Q11424") %>%
spq_add("?film wdt:P921 ?subject") %>%
spq_label(subject) %>%
spq_group_by(film) %>%
spq_summarise(subject_label_concat=str_c(subject_label,sep="; ")) %>%
spq_head(10)
)

expect_snapshot(
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)
)
})
22 changes: 22 additions & 0 deletions tests/testthat/test-spq_assemble.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
test_that("spq_assemble() works - bindings", {
expect_snapshot(
spq_init() %>%
spq_add("?film wdt:P31 wd:Q11424") %>%
spq_add("?film wdt:P921 ?subject") %>%
spq_label(subject) %>%
spq_group_by(film) %>%
spq_summarise(subject_label_concat=str_c(subject_label,sep="; ")) %>%
spq_head(10)
)

expect_snapshot(
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)
)
})


test_that("spq_assemble() detects undefined variables in filter", {
expect_snapshot({
spq_init() %>%
Expand All @@ -12,3 +33,4 @@ test_that("spq_assemble() called from printing isn't strict", {
spq_filter(lang(itemTitleLOOKTYPO)=="en")
})
})

34 changes: 34 additions & 0 deletions tests/testthat/test-spq_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,40 @@ test_that("spq_set() works", {
)
})

test_that("spq_set() in two examples", {
expect_snapshot(
spq_init() %>%
spq_prefix(prefixes = c(schema = "http://schema.org/")) %>%
spq_set(lemma = c(
"'Wikipedia'@de",
"'Wikidata'@de",
"'Berlin'@de",
"'Technische Universität Berlin'@de"
)
) %>%
spq_add("?sitelink schema:about ?item") %>%
spq_add("?sitelink schema:isPartOf <https://de.wikipedia.org/>") %>%
spq_add("?sitelink schema:name ?lemma") %>%
spq_select(lemma, item)
)

expect_snapshot(
spq_init() %>%
spq_prefix(prefixes = c(schema = "http://schema.org/")) %>%
spq_set(lemma = c(
"'Wikipedia'@de",
"'Wikidata'@de",
"'Berlin'@de",
"'Technische Universität Berlin'@de"
)
) %>%
spq_mutate(item = schema::about(sitelink)) %>%
spq_add("?sitelink schema:isPartOf <https://de.wikipedia.org/>") %>%
spq_mutate(lemma = schema::name(sitelink)) %>%
spq_select(lemma, item)
)
})

test_that("spq_set() in query", {
httptest2::with_mock_dir(file.path("fixtures", "auteurset"), {
tibble = spq_init(endpoint = "dataBNF") %>%
Expand Down

0 comments on commit f1da1cb

Please sign in to comment.