Search in sources :

Example 36 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionOr.

@Test
public void testConditionOr() {
    Condition c1 = Condition.lt(HugeKeys.ID, 18);
    Condition c2 = Condition.gt(HugeKeys.ID, 30);
    Condition c3 = c1.or(c2);
    Assert.assertEquals(Condition.or(c1, c2), c3);
    Assert.assertFalse(c3.test(18));
    Assert.assertFalse(c3.test(19));
    Assert.assertFalse(c3.test(29));
    Assert.assertFalse(c3.test(30));
    Assert.assertTrue(c3.test(17));
    Assert.assertTrue(c3.test(31));
    // null means 0
    Assert.assertTrue(c3.test((Object) null));
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 37 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionEq.

@Test
public void testConditionEq() {
    Condition c1 = Condition.eq(HugeKeys.ID, 123);
    Assert.assertTrue(c1.test(123));
    Assert.assertTrue(c1.test(123.0));
    Assert.assertFalse(c1.test(122));
    Assert.assertFalse(c1.test(123.01));
    Assert.assertFalse(c1.test(120));
    Assert.assertFalse(c1.test(20));
    Assert.assertTrue(c1.test("123"));
    Assert.assertTrue(c1.test("123.0"));
    Assert.assertFalse(c1.test("123.01"));
    Assert.assertFalse(c1.test("200"));
    // null means 0
    Assert.assertFalse(c1.test((Object) null));
    Condition c2 = Condition.eq(HugeKeys.ID, 0);
    Assert.assertTrue(c2.test((Object) null));
    Condition c3 = Condition.eq(HugeKeys.ID, -1);
    Assert.assertFalse(c3.test((Object) null));
    Condition c4 = Condition.eq(HugeKeys.ID, "123");
    Assert.assertFalse(c4.test(123));
    Assert.assertFalse(c4.test(new Date(0L)));
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) Date(java.util.Date) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 38 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionTextContainsAny.

@Test
public void testConditionTextContainsAny() {
    Condition c1 = Condition.textContainsAny(IdGenerator.of("1"), ImmutableSet.of("tom", "cat"));
    Assert.assertTrue(c1.test("tom"));
    Assert.assertTrue(c1.test("tomcat"));
    Assert.assertTrue(c1.test("tomcat2"));
    Assert.assertTrue(c1.test("cat"));
    Assert.assertFalse(c1.test("text"));
    Assert.assertFalse(c1.test("abc"));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test((Object) null);
    }, e -> {
        Assert.assertEquals("Can't execute `textcontainsany` " + "on type null, expect String", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test(123L);
    }, e -> {
        Assert.assertEquals("Can't execute `textcontainsany` " + "on type Long, expect String", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test(ImmutableList.of("1"));
    }, e -> {
        Assert.assertEquals("Can't execute `textcontainsany` " + "on type SingletonImmutableList, expect String", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Condition c2 = new SyspropRelation(HugeKeys.ID, RelationType.TEXT_CONTAINS_ANY, "abc");
        c2.test("abc");
    }, e -> {
        Assert.assertEquals("Can't test 'abc'(String) for " + "`textcontainsany`, expect Collection", e.getMessage());
    });
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 39 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionEqWithSysprop.

@Test
public void testConditionEqWithSysprop() {
    Condition c1 = Condition.eq(HugeKeys.ID, "123");
    Assert.assertEquals("ID == 123", c1.toString());
    Assert.assertTrue(c1.isRelation());
    Assert.assertTrue(c1.isSysprop());
    Assert.assertTrue(c1.isFlattened());
    Assert.assertFalse(c1.isLogic());
    Assert.assertTrue(c1.test("123"));
    Assert.assertFalse(c1.test("1234"));
    Assert.assertFalse(c1.test(123));
    Assert.assertFalse(c1.test(new Date(123)));
    Assert.assertFalse(c1.test((Object) null));
    Relation r1 = (Relation) c1;
    Assert.assertEquals(HugeKeys.ID, r1.key());
    Assert.assertEquals("123", r1.value());
    Assert.assertEquals("123", r1.serialValue());
    Assert.assertEquals(RelationType.EQ, r1.relation());
    Assert.assertTrue(r1.test("123"));
    Relation r2 = (Relation) c1.copy();
    Assert.assertEquals(r1, r2);
    Assert.assertEquals(HugeKeys.ID, r2.key());
    Assert.assertEquals("123", r2.value());
    Assert.assertEquals("123", r2.serialValue());
    Assert.assertEquals(RelationType.EQ, r2.relation());
    Assert.assertTrue(r2.test("123"));
    r2.serialValue("1234");
    Assert.assertEquals("1234", r2.serialValue());
    Assert.assertEquals("123", r1.serialValue());
    Assert.assertTrue(r2.test("123"));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Condition.eq(HugeKeys.ID, null).test("any");
    }, e -> {
        Assert.assertEquals("Can't test null value for `==`", e.getMessage());
    });
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation) Date(java.util.Date) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 40 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionPrefix.

@Test
public void testConditionPrefix() {
    Condition c1 = Condition.prefix(HugeKeys.ID, IdGenerator.of("abc"));
    Assert.assertTrue(c1.test(IdGenerator.of("a")));
    Assert.assertTrue(c1.test(IdGenerator.of("ab")));
    Assert.assertTrue(c1.test(IdGenerator.of("abc")));
    Assert.assertFalse(c1.test(IdGenerator.of("abcd")));
    Assert.assertFalse(c1.test(IdGenerator.of("b")));
    Assert.assertFalse(c1.test(IdGenerator.of("bc")));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test((Object) null);
    }, e -> {
        Assert.assertEquals("Can't execute `prefix` on type null, " + "expect Id", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test(123);
    }, e -> {
        Assert.assertEquals("Can't execute `prefix` on type Integer, " + "expect Id", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Condition c2 = new SyspropRelation(HugeKeys.ID, RelationType.PREFIX, "abc");
        c2.test(IdGenerator.of("ab"));
    }, e -> {
        Assert.assertEquals("Can't test 'abc'(String) for `prefix`, " + "expect Id", e.getMessage());
    });
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

Condition (com.baidu.hugegraph.backend.query.Condition)49 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)29 Test (org.junit.Test)29 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)16 ArrayList (java.util.ArrayList)13 Id (com.baidu.hugegraph.backend.id.Id)12 Collection (java.util.Collection)8 Date (java.util.Date)8 SyspropRelation (com.baidu.hugegraph.backend.query.Condition.SyspropRelation)6 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)4 RangeConditions (com.baidu.hugegraph.backend.query.Condition.RangeConditions)4 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)4 HugeException (com.baidu.hugegraph.HugeException)3 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)3 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)3 HugeType (com.baidu.hugegraph.type.HugeType)3 Directions (com.baidu.hugegraph.type.define.Directions)3 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)3 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)3 OrP (org.apache.tinkerpop.gremlin.process.traversal.util.OrP)3