From 7110e270dd755196918b20018ff9908cadea4b3e Mon Sep 17 00:00:00 2001 From: Lukas Plank Date: Mon, 2 Dec 2024 12:06:10 +0100 Subject: [PATCH] test: implement basic tests for query injections with prefixes --- tests/unit/test_inject_subquery.py | 122 +++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/unit/test_inject_subquery.py diff --git a/tests/unit/test_inject_subquery.py b/tests/unit/test_inject_subquery.py new file mode 100644 index 0000000..e95494d --- /dev/null +++ b/tests/unit/test_inject_subquery.py @@ -0,0 +1,122 @@ +"""Unit tests for inject_subquery.""" + +from typing import NamedTuple + +import pytest +from rdfproxy.utils.sparql_utils import inject_subquery + + +class InjectSubqueryParameter(NamedTuple): + query: str + subquery: str + expected: str + + +inject_subquery_parameters = [ + InjectSubqueryParameter( + query="select * where {?s ?p ?o .}", + subquery="select * where {?s ?p ?o .}", + expected="select * where {?s ?p ?o . {select * where {?s ?p ?o .}} }", + ), + InjectSubqueryParameter( + query="select * where {?s ?p ?o .}", + subquery="prefix : select * where {?s ?p ?o .}", + expected="select * where {?s ?p ?o . {select * where {?s ?p ?o .}} }", + ), + InjectSubqueryParameter( + query="prefix : select * where {?s ?p ?o .}", + subquery="prefix : select * where {?s ?p ?o .}", + expected="prefix : select * where {?s ?p ?o . {select * where {?s ?p ?o .}} }", + ), + InjectSubqueryParameter( + query="PREFIX crm: select * where {?s ?p ?o .}", + subquery="select * where {?s ?p ?o .}", + expected="PREFIX crm: select * where {?s ?p ?o . {select * where {?s ?p ?o .}} }", + ), + InjectSubqueryParameter( + query=""" + PREFIX crm: + PREFIX lrmoo: + PREFIX star: + PREFIX skos: + PREFIX r11: + PREFIX r11pros: + + SELECT + ?location + ?location__location_descriptive_name + + WHERE { + ?location a crm:E53_Place. + + ?location crm:P3_has_note ?location__location_descriptive_name. + } + """, + subquery="select * where {?s ?p ?o .}", + expected=""" + PREFIX crm: + PREFIX lrmoo: + PREFIX star: + PREFIX skos: + PREFIX r11: + PREFIX r11pros: + + SELECT + ?location + ?location__location_descriptive_name + + WHERE { + ?location a crm:E53_Place. + + ?location crm:P3_has_note ?location__location_descriptive_name. + {select * where {?s ?p ?o .}} } + """, + ), + InjectSubqueryParameter( + query=""" + PREFIX : + PREFIX crm: + PREFIX lrmoo: + PREFIX star: + PREFIX skos: + PREFIX r11: + PREFIX r11pros: + + SELECT + ?location + ?location__location_descriptive_name + + WHERE { + ?location a crm:E53_Place. + + ?location crm:P3_has_note ?location__location_descriptive_name. + } + """, + subquery="select * where {?s ?p ?o .}", + expected=""" + PREFIX : + PREFIX crm: + PREFIX lrmoo: + PREFIX star: + PREFIX skos: + PREFIX r11: + PREFIX r11pros: + + SELECT + ?location + ?location__location_descriptive_name + + WHERE { + ?location a crm:E53_Place. + + ?location crm:P3_has_note ?location__location_descriptive_name. + {select * where {?s ?p ?o .}} } + """, + ), +] + + +@pytest.mark.parametrize(["query", "subquery", "expected"], inject_subquery_parameters) +def test_inject_subquery(query, subquery, expected): + injected = inject_subquery(query=query, subquery=subquery) + assert injected == expected