Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Kalmbach <[email protected]>
  • Loading branch information
joka921 committed Oct 22, 2024
1 parent 9f37d16 commit e5ca697
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/engine/ExportQueryExecutionTrees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
#include "util/ConstexprUtils.h"
#include "util/http/MediaTypes.h"

bool getResultForAsk(const std::shared_ptr<const Result>& res) {
if (res->isFullyMaterialized()) {
return !res->idTable().empty();
// Return true iff the `result` is nonempty.
bool getResultForAsk(const std::shared_ptr<const Result>& result) {
if (result->isFullyMaterialized()) {
return !result->idTable().empty();
} else {
return std::ranges::any_of(res->idTables(), std::not_fn(&IdTable::empty));
return std::ranges::any_of(result->idTables(),
std::not_fn(&IdTable::empty));
}

Check warning on line 22 in src/engine/ExportQueryExecutionTrees.cpp

View check run for this annotation

Codecov / codecov/patch

src/engine/ExportQueryExecutionTrees.cpp#L20-L22

Added lines #L20 - L22 were not covered by tests
}

Expand Down Expand Up @@ -52,6 +54,7 @@ ad_utility::streams::stream_generator computeResultForAsk(
j["head"] = nlohmann::json::object_t{};
j["boolean"] = result;
co_yield j.dump();
break;
}

Check warning on line 58 in src/engine/ExportQueryExecutionTrees.cpp

View check run for this annotation

Codecov / codecov/patch

src/engine/ExportQueryExecutionTrees.cpp#L58

Added line #L58 was not covered by tests
default:
throw std::runtime_error{
Expand Down Expand Up @@ -835,7 +838,7 @@ ExportQueryExecutionTrees::computeResultAsQLeverJSON(
jsonPrefix["selected"] =
std::vector<std::string>{"?subject", "?predicate", "?object"};
} else {
// TODO<joka921> Assert that this is an ASK clause.
AD_CORRECTNESS_CHECK(query.hasAskClause());
jsonPrefix["selected"] = std::vector<std::string>{"?result"};
}

Expand Down
47 changes: 37 additions & 10 deletions test/ExportQueryExecutionTreesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ struct TestCaseSelectQuery {
std::string resultXml;
};

// A test case that tests the correct execution and exporting of an ASK query
// in various formats.
struct TestCaseAskQuery {
std::string kg; // The knowledge graph (TURTLE)
std::string query; // The query (SPARQL)
Expand All @@ -100,15 +102,6 @@ struct TestCaseAskQuery {
std::string resultXml;
};

// TODO<joka921> setup one test case for the ASK -> true and one for the ASK ->
// false case and use them for unit testing.
/*
TestCaseAskQuery askResultTrue() {
TestCaseAskQuery testCase;
testCase.resultQLeverJSON = nlohmann::json::parse("[]");
}
*/

struct TestCaseConstructQuery {
std::string kg; // The knowledge graph (TURTLE)
std::string query; // The query (SPARQL)
Expand Down Expand Up @@ -176,7 +169,7 @@ void runConstructQueryTestCase(
void runAskQueryTestCase(
const TestCaseAskQuery& testCase,
ad_utility::source_location l = ad_utility::source_location::current()) {
auto trace = generateLocationTrace(l, "runConstructQueryTestCase");
auto trace = generateLocationTrace(l, "runAskQueryTestCase");
using enum ad_utility::MediaType;
// TODO<joka921> match the exception
EXPECT_ANY_THROW(runQueryStreamableResult(testCase.kg, testCase.query, tsv));
Expand Down Expand Up @@ -1222,6 +1215,40 @@ TEST(ExportQueryExecutionTrees, CornerCases) {
::testing::ContainsRegex("should be unreachable"));
}

// Test the correct exporting of ASK queries.
TEST(ExportQueryExecutionTrees, AskQuery) {
auto askResultTrue = []() {
TestCaseAskQuery testCase;
testCase.query = "ASK { BIND (3 as ?x) FILTER (?x > 0)}";
testCase.resultQLeverJSON = nlohmann::json{std::vector<std::string>{
"\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>"}};
testCase.resultSparqlJSON =
nlohmann::json::parse(R"({"head":{ }, "boolean" : true})");
testCase.resultXml =
"<?xml version=\"1.0\"?>\n<sparql "
"xmlns=\"http://www.w3.org/2005/sparql-results#\">\n <head/>\n "
"<boolean>true</boolean>\n</sparql>";

return testCase;
};

auto askResultFalse = []() {
TestCaseAskQuery testCase;
testCase.query = "ASK { BIND (3 as ?x) FILTER (?x < 0)}";
testCase.resultQLeverJSON = nlohmann::json{std::vector<std::string>{
"\"false\"^^<http://www.w3.org/2001/XMLSchema#boolean>"}};
testCase.resultSparqlJSON =
nlohmann::json::parse(R"({"head":{ }, "boolean" : false})");
testCase.resultXml =
"<?xml version=\"1.0\"?>\n<sparql "
"xmlns=\"http://www.w3.org/2005/sparql-results#\">\n <head/>\n "
"<boolean>false</boolean>\n</sparql>";
return testCase;
};
runAskQueryTestCase(askResultTrue());
runAskQueryTestCase(askResultFalse());
}

using enum ad_utility::MediaType;

// ____________________________________________________________________________
Expand Down

0 comments on commit e5ca697

Please sign in to comment.