Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added three tests to cover changes made by the pull request #1361 #1645

Merged
merged 10 commits into from
Jan 6, 2022
21 changes: 16 additions & 5 deletions rdflib/plugins/sparql/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import functools
import logging
GreenfishK marked this conversation as resolved.
Show resolved Hide resolved
import operator
import collections

Expand Down Expand Up @@ -847,9 +848,16 @@ def find_nth(haystack, needle, n):
with open("query.txt", "w") as file:
file.write(filedata)

aggr_vars = collections.defaultdict(list) # type: dict

def convert_node_arg(node_arg):
if isinstance(node_arg, Identifier):
return node_arg.n3()
if node_arg in aggr_vars.keys():
grp_var = aggr_vars[node_arg].pop(0).n3()
logging.debug(grp_var)
GreenfishK marked this conversation as resolved.
Show resolved Hide resolved
return grp_var
else:
return node_arg.n3()
elif isinstance(node_arg, CompValue):
return "{" + node_arg.name + "}"
elif isinstance(node_arg, Expr):
Expand Down Expand Up @@ -959,7 +967,9 @@ def sparql_query_text(node):
raise ExpressionNotCoveredException(
"This expression might not be covered yet."
)
agg_func_name = agg_func.name.split("_")[1]
aggr_vars[agg_func.res].append(agg_func.vars)

agg_func_name = agg_func.name.split('_')[1]
distinct = ""
if agg_func.distinct:
distinct = agg_func.distinct + " "
Expand Down Expand Up @@ -1022,7 +1032,7 @@ def sparql_query_text(node):
if isinstance(c.expr, Identifier):
var = c.expr.n3()
if c.order is not None:
cond = var + "(" + c.order + ")"
cond = c.order + "(" + var + ")"
else:
cond = var
order_conditions.append(cond)
Expand Down Expand Up @@ -1233,7 +1243,7 @@ def sparql_query_text(node):
"{Builtin_STRLEN}", "STRLEN(" + convert_node_arg(node.arg) + ")"
)
elif node.name.endswith("Builtin_SUBSTR"):
args = [node.arg.n3(), node.start]
args = [convert_node_arg(node.arg), node.start]
if node.length:
args.append(node.length)
expr = "SUBSTR(" + ", ".join(args) + ")"
Expand Down Expand Up @@ -1294,7 +1304,8 @@ def sparql_query_text(node):
)
elif node.name.endswith("Builtin_CONCAT"):
expr = "CONCAT({vars})".format(
vars=", ".join(elem.n3() for elem in node.arg)
#vars=", ".join(elem.n3() for elem in node.arg)
GreenfishK marked this conversation as resolved.
Show resolved Hide resolved
vars=", ".join(convert_node_arg(elem) for elem in node.arg)
)
replace("{Builtin_CONCAT}", expr)
elif node.name.endswith("Builtin_LANGMATCHES"):
Expand Down
20 changes: 17 additions & 3 deletions test/test_translate_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ def _format_query(query: str) -> str:
"test_graph_patterns__group",
'Test if "group" gets properly translated into the query text.',
),
AlgebraTest(
"test_graph_patterns__group_and_substr",
'Test if a query with a variable that is used in the "GROUP BY" clause '
'and in the SUBSTR function gets properly translated into the query text.',
),
AlgebraTest(
"test_graph_patterns__group_and_nested_concat",
'Test if a query with a nested concat expression in the select clause which '
'uses a group variable gets properly translated into the query text.',
),
AlgebraTest(
"test_graph_patterns__having",
'Test if "having" gets properly translated into the query text.',
Expand Down Expand Up @@ -266,26 +276,30 @@ def test_all_files_used(data_path: Path) -> None:
@pytest.mark.parametrize("test_spec", [test.pytest_param() for test in algebra_tests])
def test_roundtrip(test_spec: AlgebraTest, data_path: Path) -> None:
"""
Query remains the same over two successuive parse and translate cycles.
Query remains the same over two successive parse and translate cycles.
"""
query_text = (data_path / test_spec.filename).read_text()

logging.info("checking expectation: %s", test_spec.description)
query_tree = parser.parseQuery(query_text)
query_algebra = algebra.translateQuery(query_tree)
query_from_algebra = translateAlgebra(query_algebra)
logging.debug(
"query_from_query_from_algebra = \n%s",
query_from_algebra,
)

query_tree_2 = parser.parseQuery(query_from_algebra)
query_algebra_2 = algebra.translateQuery(query_tree_2)
query_from_query_from_algebra = translateAlgebra(query_algebra_2)
logging.debug(
"query_from_query_from_algebra = \n%s",
_format_query(query_from_query_from_algebra),
query_from_query_from_algebra,
)
assert (
query_from_algebra == query_from_query_from_algebra
), f"failed expectation: {test_spec.description}"

# TODO: Execute the raw query (query_text) and the reconstitued query
# TODO: Execute the raw query (query_text) and the reconstituted query
# (query_from_query_from_algebra) against a well defined graph and ensure
# they yield the same result.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ select
(concat("foo"@en, "bar"@en) as ?concat)
(replace("abcd", "b", "Z") as ?replace)
(regex(substr("foobar", 4), "bar", "bar") as ?regex)
(regex(substr("foobar", 4, 1), "b", "b") as ?regex2)
where {
?s ?p ?o .
FILTER langMatches(lang(?o), "EN" )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT
(concat(str(?a), concat("foo"@en, "bar"@en)) as ?nested_concat)
WHERE {
?a rdf:value ?val .
} GROUP BY ?a
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT
(SUM(?val) AS ?sum)
(COUNT(?a) AS ?count)
(SUBSTR(?a, 4, 1) as ?a_substr)
WHERE {
?a rdf:value ?val .
} GROUP BY ?a
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ setenv =
PYTHONHASHSEED = 0
commands =
sphinx-build -n -T -b html -d {envtmpdir}/doctrees docs docs/_build/html

[pytest]
log_cli = 1
log_cli_level = DEBUG
GreenfishK marked this conversation as resolved.
Show resolved Hide resolved
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S