package org.umlg.sqlg.test;

import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * Date: 2014/07/29
 * Time: 2:21 PM
 */
public class TestHasLabel extends BaseTest {

    @Test
    public void testHasLabel() {
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.tx().commit();

        Assert.assertEquals(8, this.sqlgGraph.traversal().V().has(T.label, "Person").count().next(), 0);
    }

    @Test
    public void testNonExistingLabel() {
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.addVertex(T.label, "Person");
        this.sqlgGraph.tx().commit();
        Assert.assertEquals(2, this.sqlgGraph.traversal().V().has(T.label, "Person").count().next(), 0);
        Assert.assertEquals(0, this.sqlgGraph.traversal().V().has(T.label, "Animal").count().next(), 0);
    }

    @Test
    public void testInLabels() {
        Vertex a = this.sqlgGraph.addVertex(T.label, "Person", "name", "a");
        Vertex b = this.sqlgGraph.addVertex(T.label, "Person", "name", "b");
        Vertex c = this.sqlgGraph.addVertex(T.label, "Person", "name", "c");
        Vertex d = this.sqlgGraph.addVertex(T.label, "Person", "name", "d");
        a.addEdge("knows", b);
        a.addEdge("created", b);
        a.addEdge("knows", c);
        a.addEdge("created", d);
        this.sqlgGraph.tx().commit();
        Assert.assertEquals(4, this.sqlgGraph.traversal().V().has(T.label, "Person").count().next(), 0);
        Assert.assertEquals(2, this.sqlgGraph.traversal().E().has(T.label, P.within(Arrays.asList("knows"))).count().next(), 0);
        Assert.assertEquals(2, this.sqlgGraph.traversal().E().has(T.label, P.within(Arrays.asList("created"))).count().next(), 0);
        Assert.assertEquals(4, this.sqlgGraph.traversal().E().has(T.label, P.within(Arrays.asList("knows", "created"))).count().next(), 0);
    }

    @Test
    public void g_V_outE_hasLabel_inV() {
        loadModern();
        List<Vertex> vertices = gt.V().outE().hasLabel("created").inV().toList();
        Assert.assertEquals(4, vertices.size());
        vertices = gt.V().outE().hasLabel("knows").inV().toList();
        Assert.assertEquals(2, vertices.size());
    }

    @Test
    public void testEdgeHasLabel() {
        Vertex person1 = this.sqlgGraph.addVertex(T.label, "schema1.person");
        Vertex person2 = this.sqlgGraph.addVertex(T.label, "schema1.person");
        Vertex address1 = this.sqlgGraph.addVertex(T.label, "schema2.address");
        Vertex address2 = this.sqlgGraph.addVertex(T.label, "schema2.address");
        person1.addEdge("address", address1);
        person2.addEdge("address", address2);
        this.sqlgGraph.tx().commit();
        Assert.assertEquals(2, this.sqlgGraph.traversal().V().outE().otherV().count().next().intValue());
        Assert.assertEquals(2, this.sqlgGraph.traversal().V().inE().otherV().count().next().intValue());

    }

}

@@ -79,16 +79,68 @@
 rsal.P;%0A
+import org.apache.tinkerpop.gremlin.structure.Edge;%0A
 import o
@@ -332,16 +332,101 @@
 .List;%0A%0A
+import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.hasLabel;%0A%0A
 /**%0A * D
@@ -3599,16 +3599,222 @@
 lue());%0A
+    %7D%0A%0A    @Test%0A    public void testEdgeNotHasLabel() %7B%0A        loadModern();%0A        List%3CEdge%3E edges = gt.V().outE().not(hasLabel(%22created%22).inV()).toList();%0A        Assert.assertEquals(2, edges.size());
 %0A    %7D%0A%0A

add not has edge test