Skip to content

Commit

Permalink
CONSTRUCT query tests with UNION
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Cuddihy committed Feb 17, 2021
1 parent 5a71bc1 commit e26255c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,14 @@ public JSONArray executeQueryToGraph(String query) throws Exception {
if (responseJson.containsKey("@graph")) {
return (JSONArray)responseJson.get("@graph");
} else if (responseJson.containsKey("@id")) {
// only one object returned, no array (fuseki behavior)
JSONArray ret = new JSONArray();
ret.add(responseJson);
return ret;
} else if (responseJson.containsKey("@context")) {
// @context, but no @graph or @id: so empty (fuseki behavior)
JSONArray ret = new JSONArray();
return ret;
} else {
throw new Exception("Invalid @graph response: " + responseJson.toJSONString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ public void propertyOptionalMinus() throws Exception {
@Test
public void constructPropertyOptionalMinus() throws Exception {
// virtuoso is unreliable with CONSTRUCT
assumeFalse(TestGraph.getSei().getServerType().equals(SparqlEndpointInterface.VIRTUOSO_SERVER));SparqlEndpointInterface sei = TestGraph.getSei();
assumeFalse(TestGraph.getSei().getServerType().equals(SparqlEndpointInterface.VIRTUOSO_SERVER));

// get every pair of links that is NOT attached to a chain
NodeGroup ng = TestGraph.getNodeGroup("src/test/resources/chain_chain.json");
PropertyItem pItem = ng.getNode(0).getPropertyItemBySparqlID("?chainDesc");

pItem.setOptMinus(PropertyItem.OPT_MINUS_NONE);
String sparql = ng.generateSparqlConstruct();
JSONArray graph = sei.executeQueryToGraph(sparql);
JSONArray graph = TestGraph.getSei().executeQueryToGraph(sparql);

assertEquals("JSON-LD has wrong number of elements", 1, graph.size());
assertEquals("Not all items have @types", 1, countAttributes(graph, "@type", null));
Expand All @@ -344,7 +344,7 @@ public void constructPropertyOptionalMinus() throws Exception {
// OPTIONAL
pItem.setOptMinus(PropertyItem.OPT_MINUS_OPTIONAL);
sparql = ng.generateSparqlConstruct();
graph = sei.executeQueryToGraph(sparql);
graph = TestGraph.getSei().executeQueryToGraph(sparql);

assertEquals("JSON-LD has wrong number of elements", 2, graph.size());
assertEquals("Not all items have @types", 2, countAttributes(graph, "@type", null));
Expand All @@ -354,7 +354,7 @@ public void constructPropertyOptionalMinus() throws Exception {
// MINUS
pItem.setOptMinus(PropertyItem.OPT_MINUS_MINUS);
sparql = ng.generateSparqlConstruct();
graph = sei.executeQueryToGraph(sparql);
graph = TestGraph.getSei().executeQueryToGraph(sparql);

assertEquals("JSON-LD has wrong number of elements", 1, graph.size());
assertEquals("Not all items have @types", 1, countAttributes(graph, "@type", null));
Expand All @@ -375,8 +375,23 @@ public void unionTwoNodeItems() throws Exception {
assertEquals(2, tab.getNumRows());
}

// TODO constructUnionTwoNodeItems()
@Test
public void constructUnionTwoNodeItems() throws Exception {

// virtuoso is unreliable with CONSTRUCT
assumeFalse(TestGraph.getSei().getServerType().equals(SparqlEndpointInterface.VIRTUOSO_SERVER));

NodeGroup ng = TestGraph.getNodeGroup("src/test/resources/animalSubPropsCatNItemUnion.json");
String query = ng.generateSparqlConstruct();
JSONArray graph = TestGraph.getSei().executeQueryToGraph(query);

// try it as construct: subject to virtuoso incomplete results problem. assume away if needed
assertEquals("JSON-LD has wrong number of elements", 4, graph.size());
assertEquals("Not all items have @types", 4, countAttributes(graph, "@type", null));
assertEquals("Not all items have @id", 4, countAttributes(graph, "@id", null));
assertEquals("Wrong number of name->beelz attributes", 1, countAttributes(graph, "name", "beelz"));
assertEquals("Wrong number of name->white attributes", 1, countAttributes(graph, "name", "white"));
}

@Test
/**
Expand All @@ -395,8 +410,23 @@ public void unionTwoNodeItemsNonHead() throws Exception {
assertEquals(9, tab.getNumRows());
}

// TODO constructUnionTwoNodeItemsNonHead()

@Test
public void constructUnionTwoNodeItemsNonHead() throws Exception {

// virtuoso is unreliable with CONSTRUCT
assumeFalse(TestGraph.getSei().getServerType().equals(SparqlEndpointInterface.VIRTUOSO_SERVER));

NodeGroup ng = TestGraph.getNodeGroup("src/test/resources/chain_node_minus_union.json");
String query = ng.generateSparqlConstruct();
JSONArray graph = TestGraph.getSei().executeQueryToGraph(query);

// try it as construct: subject to virtuoso incomplete results problem. assume away if needed
assertEquals("JSON-LD has wrong number of elements", 9, graph.size());
assertEquals("Not all items have @types", 9, countAttributes(graph, "@type", null));
assertEquals("Not all items have @id", 9, countAttributes(graph, "@id", null));
assertEquals("Wrong number of linkName attributes", 9, countAttributes(graph, "linkName", null));
}

@Test
public void unionTwoPropItems() throws Exception {
Expand All @@ -411,7 +441,29 @@ public void unionTwoPropItems() throws Exception {
assertEquals(2, tab.getNumRows());
}

// TODO constructUnionTwoPropItems()
@Test
public void constructUnionTwoPropItems() throws Exception {

// virtuoso is unreliable with CONSTRUCT
assumeFalse(TestGraph.getSei().getServerType().equals(SparqlEndpointInterface.VIRTUOSO_SERVER));

// Union between a link and a chain
// Each has their version of name bound to ?name
// Link names need to contain "orphan" and chain names need to contain "B" (sharing bound variables)

NodeGroup ng = TestGraph.getNodeGroup("src/test/resources/chainUnionProperties.json");
String query = ng.generateSparqlConstruct();
JSONArray graph = TestGraph.getSei().executeQueryToGraph(query);

// try it as construct: subject to virtuoso incomplete results problem. assume away if needed
assertEquals("JSON-LD has wrong number of elements", 2, graph.size());
assertEquals("Not all items have @types", 2, countAttributes(graph, "@type", null));
assertEquals("Not all items have @id", 2, countAttributes(graph, "@id", null));
assertEquals("Wrong number of chainName attributes", 1, countAttributes(graph, "chainName", null));
assertEquals("Wrong number of chainDesc attributes", 1, countAttributes(graph, "chainDesc", null));

}


@Test
public void unionTwoNodeSubgraphs() throws Exception {
Expand All @@ -429,7 +481,28 @@ public void unionTwoNodeSubgraphs() throws Exception {
assertEquals(3, tab.getNumRows());
}

// TODO constructUnionTwoNodeSubgraphs()
@Test
public void constructUnionTwoNodeSubgraphs() throws Exception {
// virtuoso is unreliable with CONSTRUCT
assumeFalse(TestGraph.getSei().getServerType().equals(SparqlEndpointInterface.VIRTUOSO_SERVER));

// Union between a link and a chain
// Each has their version of name bound to ?name
// Link names need to contain "orphan" and chain names need to contain "B" (sharing bound variables)

NodeGroup ng = TestGraph.getNodeGroup("src/test/resources/chainUnionNodes.json");
String query = ng.generateSparqlConstruct();
JSONArray graph = TestGraph.getSei().executeQueryToGraph(query);

// try it as construct: subject to virtuoso incomplete results problem. assume away if needed
assertEquals("JSON-LD has wrong number of elements", 3, graph.size());
assertEquals("Not all items have @types", 3, countAttributes(graph, "@type", null));
assertEquals("Not all items have @id", 3, countAttributes(graph, "@id", null));
assertEquals("Wrong number of linkName attributes", 2, countAttributes(graph, "linkName", null));
assertEquals("Wrong number of chainName attributes", 1, countAttributes(graph, "chainName", null));

}


@Test
public void constructQuery() throws Exception{
Expand All @@ -439,7 +512,7 @@ public void constructQuery() throws Exception{
NodeGroup ng = sgJsonBattery.getNodeGroup();
SparqlEndpointInterface sei = sgJsonBattery.getSparqlConn().getDefaultQueryInterface();

String query = ng.generateSparqlConstruct(false);
String query = ng.generateSparqlConstruct();
JSONObject responseJson = sei.executeQuery(query, SparqlResultTypes.GRAPH_JSONLD);

JSONArray graph = (JSONArray)responseJson.get("@graph");
Expand All @@ -460,7 +533,7 @@ public void constructQueryWithConstraints() throws Exception {
NodeGroup ng = TestGraph.getNodeGroup("src/test/resources/sampleBattery_PlusConstraints.json");
SparqlEndpointInterface sei = sgJsonBattery.getSparqlConn().getDefaultQueryInterface();

String query = ng.generateSparqlConstruct(false);
String query = ng.generateSparqlConstruct();

JSONObject responseJson = sei.executeQuery(query, SparqlResultTypes.GRAPH_JSONLD);
JSONArray graph = (JSONArray)responseJson.get("@graph");
Expand Down Expand Up @@ -541,13 +614,10 @@ private static void testCatHasChildrenConstruct(NodeGroup ng) throws Exception {

// run construct: subject to virtuoso incomplete results problem. assume away if needed
graph = TestGraph.getSei().executeQueryToGraph(sparql);
assertEquals("JSON-LD has wrong number of elements", 8, graph.size());
assertEquals("Not all items have @types", 8, countAttributes(graph, "@type", null));
assertEquals("Not all items have @id", 8, countAttributes(graph, "@id", null));
assertEquals("Missing name attributes", 8, countAttributes(graph, "name", null));
assertEquals("Missing hasKitties attributes", 3, countAttributes(graph, "hasKitties", null));
assertEquals("Missing hasDemons attributes", 2, countAttributes(graph, "hasDemons", null));
assertEquals("Missing scaryName attributes", 1, countAttributes(graph, "scaryName", null));

// no oInfo will not use sub properties, so results are empty
// if this is fixed, then you'd get the same results as above.
assertEquals("JSON-LD has wrong number of elements", 0, graph.size());
}

@Test
Expand Down

0 comments on commit e26255c

Please sign in to comment.