Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Qup42 committed May 21, 2024
1 parent cbca09c commit 3172dd0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
45 changes: 44 additions & 1 deletion test/SparqlAntlrParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,10 @@ TEST(SparqlParser, binaryStringExpressions) {
expectBuiltInCall("STRBEFORE(?x, ?y)", makeMatcher(&makeStrBeforeExpression));
}

TEST(SparqlParser, updateUnsupported) {
// Update queries are WIP. The individual parts to parse some update queries
// are in place the code to process them is still unfinished. Therefore we
// don't accept update queries.
TEST(SparqlParser, updateQueryUnsupported) {
auto expectUpdateFails = ExpectParseFails<&Parser::queryOrUpdate>{};
auto contains = [](const std::string& s) { return ::testing::HasSubstr(s); };
auto updateUnsupported =
Expand All @@ -1647,3 +1650,43 @@ TEST(SparqlParser, updateUnsupported) {
expectUpdateFails("MOVE DEFAULT TO GRAPH <a>", updateUnsupported);
expectUpdateFails("COPY GRAPH <a> TO GRAPH <a>", updateUnsupported);
}

TEST(SparqlParser, UpdateQuery) {
auto expectUpdate = ExpectCompleteParse<&Parser::update>{
{{INTERNAL_PREDICATE_PREFIX_NAME, INTERNAL_PREDICATE_PREFIX_IRI}}};
auto expectUpdateFails = ExpectParseFails<&Parser::query>{};
auto Iri = [](std::string_view stringWithBrackets) {
return TripleComponent::Iri::fromIriref(stringWithBrackets);
};
auto Literal = [](std::string s) {
return TripleComponent::Literal::fromStringRepresentation(s);
};

expectUpdate("INSERT DATA { <a> <b> <c> }",
m::UpdateQuery({}, {{Iri("<a>"), Iri("<b>"), Iri("<c>")}},
m::GraphPattern()));
expectUpdate(
"INSERT DATA { \"foo:bar\" <b> <c> }",
m::UpdateQuery({}, {{Literal("\"foo:bar\""), Iri("<b>"), Iri("<c>")}},
m::GraphPattern()));
expectUpdate("DELETE DATA { <a> <b> <c> }",
m::UpdateQuery({{Iri("<a>"), Iri("<b>"), Iri("<c>")}}, {},
m::GraphPattern()));
expectUpdate(
"DELETE { ?a <b> <c> } WHERE { <d> <e> ?a }",
m::UpdateQuery(
{{Var("?a"), Iri("<b>"), Iri("<c>")}}, {},
m::GraphPattern(m::Triples({{Iri("<d>"), "<e>", Var{"?a"}}}))));
expectUpdate(
"DELETE { ?a <b> <c> } INSERT { <a> ?a <c> } WHERE { <d> <e> ?a }",
m::UpdateQuery(
{{Var("?a"), Iri("<b>"), Iri("<c>")}},
{{Iri("<a>"), Var("?a"), Iri("<c>")}},
m::GraphPattern(m::Triples({{Iri("<d>"), "<e>", Var{"?a"}}}))));
// Unsupported features.
expectUpdateFails(
"INSERT DATA { <a> <b> <c> } ; INSERT { ?a <b> <c> } WHERE { <d> <e> ?a "
"}");
expectUpdateFails("DELETE { ?a <b> <c> } USING <foo> WHERE { <d> <e> ?a }");
expectUpdateFails("WITH <foo> DELETE { ?a <b> <c> } WHERE { <d> <e> ?a }");
}
16 changes: 16 additions & 0 deletions test/SparqlAntlrParserTestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,20 @@ inline auto VisibleVariables =
return AD_PROPERTY(ParsedQuery, getVisibleVariables, testing::Eq(elems));
};

inline auto UpdateQuery =
[](const std::vector<SparqlTripleSimple>& toDelete,
const std::vector<SparqlTripleSimple>& toInsert,
const Matcher<const p::GraphPattern&>& graphPatternMatcher)
-> Matcher<const ::ParsedQuery&> {
return testing::AllOf(
AD_PROPERTY(ParsedQuery, hasUpdateClause, testing::IsTrue()),
AD_PROPERTY(ParsedQuery, updateClause,
AD_FIELD(parsedQuery::UpdateClause, toDelete_,
testing::Eq(toDelete))),
AD_PROPERTY(ParsedQuery, updateClause,
AD_FIELD(parsedQuery::UpdateClause, toInsert_,
testing::Eq(toInsert))),
RootGraphPattern(graphPatternMatcher));
};

} // namespace matchers

0 comments on commit 3172dd0

Please sign in to comment.