public static BaseCondition c(String field, String operator, Object comparison) { return c(field, WhereOperator.toOperator(operator), comparison); }
@Test public void testWhereWithComplexAndOrStructure() { saveManyBasicObjects(3); List<BasicObject> objects1 = yawp(BasicObject.class).where( or(and(c("intValue", "=", 1), c("intValue", "=", 2)), and(c("intValue", "=", 3), c("intValue", "=", 3)))).list(); assertEquals(1, objects1.size()); assertEquals(3, objects1.get(0).getIntValue()); BaseCondition condition = or(and(c("intValue", "=", 3), c("intValue", "=", 3)), and(c("intValue", "=", 1), c("intValue", "=", 2))); List<BasicObject> objects2 = yawp(BasicObject.class).where(condition).list(); assertEquals(1, objects2.size()); assertEquals(3, objects2.get(0).getIntValue()); }
@Test public void testWhereInWithEmptyListOrTrueExpression() { saveManyBasicObjects(3); BaseCondition emptyListCondition = c("intValue", "in", Collections.emptyList()); BaseCondition condition = or(emptyListCondition, c("stringValue", "=", "xpto")); List<BasicObject> objects = yawp(BasicObject.class).where(condition).list(); assertEquals(3, objects.size()); }
@Test public void testWhereInWithEmptyListAndFalseExpression() { saveManyBasicObjects(1); BaseCondition emptyListCondition = c("intValue", "in", Collections.emptyList()); BaseCondition condition = and(emptyListCondition, c("stringValue", "=", "otpx")); List<BasicObject> objects = yawp(BasicObject.class).where(condition).list(); assertEquals(0, objects.size()); }
@Test public void testGrandparentFalseCondition() { Grandchild grandchild = saveGrandchild("granchild", saveChild("child", saveParent("another-parent"))); RuleConditions conditions = new RuleConditions(yawp, Grandchild.class, null, Arrays.asList(grandchild)); conditions.where(c("name", "=", "granchild")); conditions.and(c("parent->name", "=", "child")); conditions.and(c("parent->parent->name", "=", "parent")); assertFalse(conditions.evaluate()); }
@Test public void testWhereInWithEmptyListOrComplexExpression() { saveManyBasicObjects(1); BaseCondition emptyListCondition = c("intValue", "in", Collections.emptyList()); BaseCondition condition = and(emptyListCondition, and(c("stringValue", "=", "otpx"), emptyListCondition)); List<BasicObject> objects = yawp(BasicObject.class).where(condition).list(); assertEquals(0, objects.size()); }
@Test public void testParentTrueCondition() { Child child = saveChild("child", saveParent("parent")); RuleConditions conditions = new RuleConditions(yawp, Child.class, null, Arrays.asList(child)); conditions.where(c("name", "=", "child")); conditions.where(c("parent->name", "=", "parent")); assertTrue(conditions.evaluate()); }
@Test public void testParentFalseCondition() { Child child = saveChild("child", saveParent("another-parent")); RuleConditions conditions = new RuleConditions(yawp, Child.class, null, Arrays.asList(child)); conditions.where(c("name", "=", "child")); conditions.and(c("parent->name", "=", "parent")); assertFalse(conditions.evaluate()); }
@Test public void testEvaluateIncomingEqualsExisting() { BasicObject object1 = saveObject("xpto"); BasicObject object2 = saveObject("xpto"); BasicObject object3 = saveObject("xyz"); assertTrue(createConditions(Arrays.asList(object1, object2), c("stringValue", "=", "xpto")).evaluate()); assertFalse(createConditions(Arrays.asList(object1, object3), c("stringValue", "=", "xpto")).evaluate()); }
@Test public void testGrandparentWithoutObjectFalseCondition() { Parent parent = saveParent("another-parent"); saveGrandchild("granchild", saveChild("child", parent)); RuleConditions conditions = new RuleConditions(yawp, Grandchild.class, parent.getId(), null); conditions.where(c("name", "=", "granchild")); conditions.and(c("parent->name", "=", "child")); conditions.and(c("parent->parent->name", "=", "parent")); assertFalse(conditions.evaluate()); }
@Test public void testChainedWheresWithAnd() { saveManyBasicObjects(1); List<BasicObject> objects = yawp(BasicObject.class).where(and(c("intValue", "=", 1), c("stringValue", "=", "xpto"))).list(); assertEquals(1, objects.size()); assertEquals("xpto", objects.get(0).getStringValue()); assertEquals(1, objects.get(0).getIntValue()); }
@Test public void testGrandparentWithoutObjectTrueCondition() { Parent parent = saveParent("parent"); saveGrandchild("granchild", saveChild("child", parent)); RuleConditions conditions = new RuleConditions(yawp, Grandchild.class, parent.getId(), null); conditions.where(c("name", "=", "granchild")); conditions.and(c("parent->name", "=", "child")); conditions.and(c("parent->parent->name", "=", "parent")); assertTrue(conditions.evaluate()); }
@Test public void testWhereWithOrNot() { saveManyBasicObjects(3); List<BasicObject> objects = yawp(BasicObject.class).where(or(c("intValue", "=", 1), c("intValue", "=", 2)).not()).list(); assertEquals(1, objects.size()); assertEquals("xpto", objects.get(0).getStringValue()); assertEquals(3, objects.get(0).getIntValue()); }
@Test public void testEvaluateIncomingNotEqualsExisting() { BasicObject object1 = saveObject("xpto"); BasicObject object2 = saveObject("xpto"); BasicObject object3 = saveObject("xyz"); object1.setStringValue("xyz"); object2.setStringValue("xyz"); assertFalse(createConditions(Arrays.asList(object1, object3), c("stringValue", "=", "xpto")).evaluate()); assertFalse(createConditions(Arrays.asList(object2), c("stringValue", "=", "xpto")).evaluate()); assertTrue(createConditions(Arrays.asList(object3), c("stringValue", "=", "xyz")).evaluate()); }
@Test public void testWhereWithOr() { saveManyBasicObjects(2); List<BasicObject> objects = yawp(BasicObject.class).where(or(c("intValue", "=", 1), c("intValue", "=", 2))).list(); assertEquals(2, objects.size()); sort(objects); assertEquals(2, objects.size()); assertEquals("xpto", objects.get(0).getStringValue()); assertEquals(1, objects.get(0).getIntValue()); assertEquals("xpto", objects.get(1).getStringValue()); assertEquals(2, objects.get(1).getIntValue()); }
@Test public void testWhereWithAndNot() { saveManyBasicObjects(5); List<BasicObject> objects = yawp(BasicObject.class).where(and(c("intValue", ">", 1), c("intValue", "<", 5)).not()).list(); assertEquals(2, objects.size()); sort(objects); assertEquals(1, objects.get(0).getIntValue()); assertEquals(5, objects.get(1).getIntValue()); }
@Test public void testSimpleWhereWithNot() { saveManyBasicObjects(2); List<BasicObject> objects = yawp(BasicObject.class).where(c("intValue", "=", 1).not()).list(); assertEquals(1, objects.size()); assertEquals("xpto", objects.get(0).getStringValue()); assertEquals(2, objects.get(0).getIntValue()); }
@PUT("single") public void single(IdRef<ShieldedChild> id) { allow(isId100(id)); allow(isJanis()).where(c("parent->name", "=", "ok-for-janis")); }
@Test public void testWhereWithoutIndexComplexConditions() { yawp.save(new BasicObject("a", 1l)); yawp.save(new BasicObject("a", 2l)); yawp.save(new BasicObject("c", 3l)); yawp.save(new BasicObject("c", 4l)); yawp.save(new BasicObject("d", 5l)); yawp.save(new BasicObject("e", 6l)); BaseCondition c2 = c("stringValue", "=", "c").and(c("longValue", "=", 3l)); List<BasicObject> objects = yawp(BasicObject.class).where(c("stringValue", "=", "a").or(c2)).list(); assertObjects(objects, "a", "a", "c"); }
@Test public void testQueryRefWithPreFilter() { BasicObject ref1 = new BasicObject("right"); BasicObject ref2 = new BasicObject("right"); yawp.save(ref1); yawp.save(ref2); yawp.save(new BasicObject("a", ref1.getId())); yawp.save(new BasicObject("b", ref2.getId())); BasicObject object = yawp(BasicObject.class).where(c("objectId->stringValue", "=", "right").and(c("stringValue", "=", "a"))).only(); assertEquals("a", object.getStringValue()); }