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

fix: correct regex pattern for SELECT clause extraction #123

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions rdfproxy/utils/sparql_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@

def replace_query_select_clause(query: str, repl: str) -> str:
"""Replace the SELECT clause of a query with repl."""
if re.search(r"select\s.+", query, re.I) is None:
pattern: re.Pattern = re.compile(
r"select\s+.*?(?=\s+where)", flags=re.IGNORECASE | re.DOTALL
)

if re.search(pattern=pattern, string=query) is None:
raise Exception("Unable to obtain SELECT clause.")

count_query = re.sub(
pattern=r"select\s.+",
modified_query = re.sub(
pattern=pattern,
repl=repl,
string=query,
count=1,
flags=re.I,
)

return count_query
return modified_query


def construct_count_query(query: str, model: type[_TModelInstance]) -> str:
Expand Down
75 changes: 75 additions & 0 deletions tests/unit/test_replace_query_select_clause.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Unit tests for rdfproxy.utils.sparql_utils.replace_query_select_clause."""

import re
from textwrap import dedent

import pytest

from rdfproxy.utils.sparql_utils import replace_query_select_clause
from tests.utils._types import QueryConstructionParameter


def _normalize_whitespace(string: str) -> str:
return re.sub(r"\s+", " ", string.strip())


expected_simple_query = "select <test> where { ?s ?p ?o . }"

query_construction_parameters = [
QueryConstructionParameter(
input_query="""
select ?s ?p ?o
where {
?s ?p ?o .
}
""",
expected_query=expected_simple_query,
),
QueryConstructionParameter(
input_query="""
select ?s
?p ?o
where {
?s ?p ?o .
}
""",
expected_query=expected_simple_query,
),
QueryConstructionParameter(
input_query="""
select ?s ?p
?o
where {
?s ?p ?o .
}
""",
expected_query=expected_simple_query,
),
QueryConstructionParameter(
input_query="""
select ?s ?p ?o where {
?s ?p ?o .
}
""",
expected_query=expected_simple_query,
),
QueryConstructionParameter(
input_query="select ?s ?p ?o where { ?s ?p ?o . }",
expected_query=expected_simple_query,
),
]


@pytest.mark.parametrize(
["input_query", "expected_query"], query_construction_parameters
)
def test_basic_replace_query_select_clause(input_query, expected_query):
_constructed_indent: str = replace_query_select_clause(input_query, "select <test>")
_constructed_dedent: str = replace_query_select_clause(
dedent(input_query), "select <test>"
)

constructed_indent = _normalize_whitespace(_constructed_indent)
constructed_dedent = _normalize_whitespace(_constructed_dedent)

assert constructed_dedent == constructed_indent == expected_query
7 changes: 7 additions & 0 deletions tests/utils/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ class CountQueryParameter(NamedTuple):
query: str
model: type[BaseModel]
expected: int


class QueryConstructionParameter(NamedTuple):
"""Parameter type for query constructors."""

input_query: str
expected_query: str
Loading