org.umlg.sqlg.test.union.TestUnion Maven / Gradle / Ivy
package org.umlg.sqlg.test.union;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.Assert;
import org.junit.Test;
import org.umlg.sqlg.test.BaseTest;
import java.util.*;
/**
* Date: 2016/05/30
* Time: 9:01 PM
*/
@SuppressWarnings("unchecked")
public class TestUnion extends BaseTest {
/**
* https://github.com/pietermartin/sqlg/issues/416
*/
@Test
public void testAliasesWithinUnion() {
Vertex a = this.sqlgGraph.addVertex(T.label, "A", "name", "A");
Vertex b = this.sqlgGraph.addVertex(T.label, "B", "name", "B");
a.addEdge("edge", b);
// First, confirm that a simple traversal using an alias works properly. This works as
// expected (the failure is below with the union where the exact traversal is used inside the union).
List noUnionItems = this.sqlgGraph.traversal()
.V()
.hasLabel("A")
.as("alias1")
.out()
.select("alias1")
.toList();
Assert.assertEquals(1, noUnionItems.size());
Assert.assertEquals(a, noUnionItems.get(0));
// This one doesn't work even though the exact same traversal is used inside the union. Debugging
// the code shows that it cannot find the "alias1" label in the SelectOneStep (in the map method).
List unionItems = this.sqlgGraph.traversal()
.inject("ignore") // Normally an inject would be used here, but see #415
.union(
__.V().hasLabel("A").as("alias1").out().select("alias1")
)
.toList();
// This fails because unionItems contains 0 results.
Assert.assertEquals(1, unionItems.size());
}
@Test
public void testdkarthikeyan88_bug359() {
Graph g = this.sqlgGraph;
Vertex cluster = g.addVertex(T.label, "Cluster", "name", "Test Cluster");
Vertex service = g.addVertex(T.label, "Service", "name", "Test Service");
Vertex database = g.addVertex(T.label, "Database", "name", "Test DB");
Vertex schema1 = g.addVertex(T.label, "Schema", "name", "Test Schema1");
Vertex schema2 = g.addVertex(T.label, "Schema", "name", "Test Schema2");
Vertex table1 = g.addVertex(T.label, "Table", "name", "Table1");
Vertex table2 = g.addVertex(T.label, "Table", "name", "Table2");
Vertex table3 = g.addVertex(T.label, "Table", "name", "Table3");
Vertex table4 = g.addVertex(T.label, "Table", "name", "Table4");
Vertex column1 = g.addVertex(T.label, "Column", "name", "Column1");
Vertex column2 = g.addVertex(T.label, "Column", "name", "Column2");
Vertex column3 = g.addVertex(T.label, "Column", "name", "Column3");
Vertex column4 = g.addVertex(T.label, "Column", "name", "Column4");
Vertex column5 = g.addVertex(T.label, "Column", "name", "Column5");
Vertex column6 = g.addVertex(T.label, "Column", "name", "Column6");
Vertex column7 = g.addVertex(T.label, "Column", "name", "Column7");
Vertex column8 = g.addVertex(T.label, "Column", "name", "Column8");
cluster.addEdge("has_Service", service);
service.addEdge("has_Database", database);
database.addEdge("has_Schema", schema1);
database.addEdge("has_Schema", schema2);
schema1.addEdge("has_Table", table1);
schema1.addEdge("has_Table", table2);
schema2.addEdge("has_Table", table3);
schema2.addEdge("has_Table", table4);
table1.addEdge("has_Column", column1);
table1.addEdge("has_Column", column2);
table2.addEdge("has_Column", column3);
table2.addEdge("has_Column", column4);
table3.addEdge("has_Column", column5);
table3.addEdge("has_Column", column6);
table4.addEdge("has_Column", column7);
table4.addEdge("has_Column", column8);
g.tx().commit();
// String expected = "" +
// "{" +
// " Test Cluster={" +
// " Test Service={" +
// " Test DB={" +
// " Test Schema1={" +
// " Table1={" +
// " Column1={}, Column2={}" +
// " }" +
// " }, " +
// " Test Schema2={" +
// " Table3={" +
// " Column5={}, Column6={}" +
// " }" +
// " }" +
// " }" +
// " }" +
// " }" +
// "}";
GraphTraversal traversal = g.traversal().V()
.hasLabel("public.Cluster")
.has("name", "Test Cluster")
.out("has_Service").has("name", "Test Service")
.out("has_Database").has("name", "Test DB")
.union(
__.out("has_Schema").has("name", P.eq("Test Schema1")).out("has_Table").has("name", P.without("Table2")),
__.out("has_Schema").has("name", P.eq("Test Schema1")).out("has_Table").has("name", P.within("Table1")),
__.out("has_Schema").has("name", P.eq("Test Schema2")).out("has_Table").has("name", P.neq("Table4")))
.out("has_Column")
.range(0, 100).tree();
Tree tree = traversal.next();
List clusters = tree.getObjectsAtDepth(1);
Assert.assertEquals(1, clusters.size());
List services = tree.getObjectsAtDepth(2);
Assert.assertEquals(1, services.size());
List databases = tree.getObjectsAtDepth(3);
Assert.assertEquals(1, databases.size());
List schemas = tree.getObjectsAtDepth(4);
Assert.assertEquals(2, schemas.size());
List tables = tree.getObjectsAtDepth(5);
Assert.assertEquals(2, tables.size());
List columns = tree.getObjectsAtDepth(6);
Assert.assertEquals(4, columns.size());
columns = tree.getLeafObjects();
Assert.assertEquals(4, columns.size());
Assert.assertTrue(tree.containsKey(cluster));
Assert.assertTrue(tree.get(cluster).containsKey(service));
Assert.assertTrue(tree.get(cluster).get(service).containsKey(database));
Assert.assertTrue(tree.get(cluster).get(service).get(database).containsKey(schema1));
Assert.assertTrue(tree.get(cluster).get(service).get(database).get(schema1).containsKey(table1));
Assert.assertTrue(tree.get(cluster).get(service).get(database).get(schema1).get(table1).containsKey(column1));
Assert.assertTrue(tree.get(cluster).get(service).get(database).get(schema1).get(table1).containsKey(column2));
Assert.assertTrue(tree.get(cluster).get(service).get(database).containsKey(schema2));
Assert.assertTrue(tree.get(cluster).get(service).get(database).get(schema2).containsKey(table3));
Assert.assertTrue(tree.get(cluster).get(service).get(database).get(schema2).get(table3).containsKey(column5));
Assert.assertTrue(tree.get(cluster).get(service).get(database).get(schema2).get(table3).containsKey(column6));
}
@Test
public void testUnionHasPath() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "A1");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "A2");
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "A3");
Vertex a4 = this.sqlgGraph.addVertex(T.label, "A", "name", "A4");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "B1");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "B2");
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "B3");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "C1");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "C2");
Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "C3");
a1.addEdge("toB", b1);
a1.addEdge("toB", b2);
a1.addEdge("toB", b3);
b1.addEdge("toC", c1);
b2.addEdge("toC", c2);
b3.addEdge("toC", c3);
GraphTraversal traversal = this.sqlgGraph.traversal().V().has("A", "name", "A1")
.union(
__.out("toB").has("name", P.eq("B1")).out("toC"),
__.out("toB").has("name", P.eq("B2")).out("toC"))
.path();
printTraversalForm(traversal);
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy