Search in sources :

Example 6 with PersonCriteria

use of org.immutables.criteria.personmodel.PersonCriteria in project immutables by immutables.

the class ExpressionAsStringTest method or.

@Test
void or() {
    PersonCriteria other = person.or(person.age.atMost(1)).or(person.age.atLeast(2));
    assertExpressional(other, "call op=OR", "  call op=LESS_THAN_OR_EQUAL path=age constant=1", "  call op=GREATER_THAN_OR_EQUAL path=age constant=2");
    assertExpressional(other.or(person.age.is(3)), "call op=OR", "  call op=LESS_THAN_OR_EQUAL path=age constant=1", "  call op=GREATER_THAN_OR_EQUAL path=age constant=2", "  call op=EQUAL path=age constant=3");
    assertExpressional(person.or(person.age.atMost(1)).or(person.age.atLeast(2)).or(person.age.is(3)), "call op=OR", "  call op=LESS_THAN_OR_EQUAL path=age constant=1", "  call op=GREATER_THAN_OR_EQUAL path=age constant=2", "  call op=EQUAL path=age constant=3");
}
Also used : PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) Test(org.junit.jupiter.api.Test)

Example 7 with PersonCriteria

use of org.immutables.criteria.personmodel.PersonCriteria in project immutables by immutables.

the class ChangeStreamsTest method multiFilter.

@Test
void multiFilter() throws InterruptedException {
    PersonCriteria filter = person.age.atLeast(21).isActive.isTrue();
    Flowable<WatchEvent<Person>> flowable = Flowable.fromPublisher(repository.watcher(filter).watch());
    TestSubscriber<WatchEvent<Person>> test = flowable.test();
    repository.insert(generator.next().withId("id1").withAge(19));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id2").withAge(21).withIsActive(false));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id3").withAge(21).withIsActive(true));
    test.awaitCount(1).assertValueCount(1);
    Person person = test.values().get(0).newValue().get();
    check(person.id()).is("id3");
}
Also used : PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) WatchEvent(org.immutables.criteria.backend.WatchEvent) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 8 with PersonCriteria

use of org.immutables.criteria.personmodel.PersonCriteria in project immutables by immutables.

the class ExpressionInterpreterTest method reflection.

@Test
public void reflection() {
    final PersonCriteria crit = PersonCriteria.person;
    final ImmutablePerson person = example.withFullName("John");
    check(!evaluate(crit.age.is(11), person));
    check(evaluate(crit.age.isNot(11), person));
    check(evaluate(crit.age.is(11), person.withAge(11)));
    check(evaluate(crit.age.is(22).fullName.is("John"), person));
    check(!evaluate(crit.age.is(22).fullName.is("Mary"), person));
    check(!evaluate(crit.age.in(1, 2, 3), person));
    check(evaluate(crit.age.notIn(1, 2, 3), person));
    check(evaluate(crit.age.in(22, 23, 24), person));
    check(!evaluate(crit.age.notIn(22, 23, 24), person));
    check(!evaluate(crit.isActive.isTrue(), person));
    check(evaluate(crit.isActive.isFalse(), person));
    check(evaluate(crit.isActive.isTrue().or().isActive.isFalse(), person));
    check(evaluate(crit.isActive.isFalse().or().isActive.isTrue().nickName.isAbsent(), person));
    check(!evaluate(crit.age.atLeast(23), person));
    check(evaluate(crit.age.atMost(22), person));
    check(!evaluate(crit.age.lessThan(22), person));
    check(!evaluate(crit.age.greaterThan(22), person));
    check(!evaluate(crit.age.atLeast(23), person));
    // optionals
    check(evaluate(crit.nickName.isPresent(), person));
    check(!evaluate(crit.nickName.isAbsent(), person));
    check(!evaluate(crit.nickName.isPresent(), person.withNickName(Optional.empty())));
    check(evaluate(crit.nickName.isAbsent(), person.withNickName(Optional.empty())));
    // == value().$expr
    check(!evaluate(crit.nickName.isNot("Smith"), person.withNickName("Smith")));
    check(evaluate(crit.nickName.in("Smith", "Nobody"), person.withNickName("Smith")));
    check(!evaluate(crit.nickName.in("Nobody", "Sky"), person.withNickName("Smith")));
    check(evaluate(crit.nickName.notIn("Nobody", "Sky"), person.withNickName("Smith")));
    // == value($expr)
    check(evaluate(crit.nickName.with(v -> v.is("Smith")), person.withNickName("Smith")));
    check(!evaluate(crit.nickName.with(v -> v.isNot("Smith")), person.withNickName("Smith")));
    check(evaluate(crit.nickName.with(v -> v.in("Smith", "Nobody")), person.withNickName("Smith")));
    check(!evaluate(crit.nickName.with(v -> v.in("Nobody", "Sky")), person.withNickName("Smith")));
    check(evaluate(crit.nickName.with(v -> v.notIn("Nobody", "Sky")), person.withNickName("Smith")));
}
Also used : Test(org.junit.jupiter.api.Test) ImmutableAddress(org.immutables.criteria.personmodel.ImmutableAddress) Person(org.immutables.criteria.personmodel.Person) Checkers.check(org.immutables.check.Checkers.check) Address(org.immutables.criteria.personmodel.Address) LocalDate(java.time.LocalDate) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) Criterias(org.immutables.criteria.Criterias) Optional(java.util.Optional) ImmutablePerson(org.immutables.criteria.personmodel.ImmutablePerson) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) ImmutablePerson(org.immutables.criteria.personmodel.ImmutablePerson) Test(org.junit.jupiter.api.Test)

Example 9 with PersonCriteria

use of org.immutables.criteria.personmodel.PersonCriteria in project immutables by immutables.

the class AggregateQueryBuilderTest method agg1.

@Test
void agg1() {
    PersonCriteria person = PersonCriteria.person;
    // select nickName, sum(age), max(dateOfBirth) from person
    // where age >= 30
    // group by nickName
    // order by nickName
    // limit 11
    Query query = Query.of(Person.class).addGroupBy(Matchers.toExpression(person.nickName)).addCollations(Collections.singleton(Collation.of(Matchers.toExpression(person.nickName)))).addProjections(Matchers.toExpression(person.nickName), Matchers.toExpression(person.age.sum()), Matchers.toExpression(person.dateOfBirth.max())).withFilter(Criterias.toQuery(person.age.atLeast(30)).filter().get()).withLimit(11);
    AggregateQueryBuilder builder = new AggregateQueryBuilder(query, MAPPER, mapping, pathNaming, idPredicate);
    ObjectNode json = builder.jsonQuery();
    JsonChecker.of(json).is("{'_source':false,", "size:0,", "'stored_fields':'_none_',", "'query.constant_score.filter.range.age.gte' : 30,", "aggregations:{expr0:{terms:{field:'nickName',missing:'__MISSING__',size:11," + " order:{'_key':'asc'}},", "aggregations:{expr1:{sum:{field:'age'}},expr2:{max:{field:'dateOfBirth'}}}}}}");
}
Also used : Query(org.immutables.criteria.expression.Query) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Aggregations

PersonCriteria (org.immutables.criteria.personmodel.PersonCriteria)9 Test (org.junit.jupiter.api.Test)9 Person (org.immutables.criteria.personmodel.Person)5 ImmutablePerson (org.immutables.criteria.personmodel.ImmutablePerson)3 WatchEvent (org.immutables.criteria.backend.WatchEvent)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Single (io.reactivex.Single)1 LocalDate (java.time.LocalDate)1 Optional (java.util.Optional)1 Checkers.check (org.immutables.check.Checkers.check)1 Criterias (org.immutables.criteria.Criterias)1 Query (org.immutables.criteria.expression.Query)1 Address (org.immutables.criteria.personmodel.Address)1 ImmutableAddress (org.immutables.criteria.personmodel.ImmutableAddress)1 Disabled (org.junit.jupiter.api.Disabled)1