Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for generating OQL expressions
- Loading branch information
1 parent
f30ef44
commit 0c5e5e3
Showing
4 changed files
with
197 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
criteria/geode/test/org/immutables/criteria/geode/GeodeQueryVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package org.immutables.criteria.geode; | ||
|
||
import static org.immutables.check.Checkers.check; | ||
import static org.immutables.criteria.matcher.Matchers.toExpression; | ||
import static org.immutables.criteria.personmodel.PersonCriteria.person; | ||
|
||
import org.immutables.criteria.backend.PathNaming; | ||
import org.immutables.criteria.personmodel.PersonCriteria; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class GeodeQueryVisitorTest { | ||
|
||
@Test | ||
void filter() { | ||
check(toOql(person.age.is(18))).is("age = 18"); | ||
check(toOql(person.age.isNot(18))).is("age != 18"); | ||
|
||
check(toOql(person.age.atMost(18))).is("age <= 18"); | ||
check(toOql(person.age.lessThan(18))).is("age < 18"); | ||
|
||
check(toOql(person.age.atLeast(18))).is("age >= 18"); | ||
check(toOql(person.age.greaterThan(18))).is("age > 18"); | ||
|
||
check(toOql(person.age.between(18, 21))).is("(age >= 18) AND (age <= 21)"); | ||
|
||
check(toOql(person.address.isPresent())).is("address != null"); | ||
check(toOql(person.address.isAbsent())).is("address = null"); | ||
} | ||
|
||
@Test | ||
void filterWithBindParams() { | ||
check(toOqlWithBindParams(person.age.is(18))).is("age = $1"); | ||
check(toOqlWithBindParams(person.age.isNot(18))).is("age != $1"); | ||
|
||
check(toOqlWithBindParams(person.age.atMost(18))).is("age <= $1"); | ||
check(toOqlWithBindParams(person.age.lessThan(18))).is("age < $1"); | ||
|
||
check(toOqlWithBindParams(person.age.atLeast(18))).is("age >= $1"); | ||
check(toOqlWithBindParams(person.age.greaterThan(18))).is("age > $1"); | ||
|
||
check(toOqlWithBindParams(person.age.between(18, 21))).is("(age >= $1) AND (age <= $2)"); | ||
|
||
check(toOqlWithBindParams(person.address.isPresent())).is("address != null"); | ||
check(toOqlWithBindParams(person.address.isAbsent())).is("address = null"); | ||
|
||
check(toOqlWithBindParams(person.fullName.isEmpty())).is("fullName = $1"); | ||
check(toOqlWithBindParams(person.fullName.notEmpty())).is("fullName != $1"); | ||
} | ||
|
||
@Test | ||
void filterNested() { | ||
check(toOql(person.address.value().city.is("London"))) | ||
.is("address.city = 'London'"); | ||
} | ||
|
||
@Test | ||
void filterNestedWithBindParams() { | ||
check(toOqlWithBindParams(person.address.value().city.is("London"))) | ||
.is("address.city = $1"); | ||
} | ||
|
||
@Test | ||
void filterIn() { | ||
check(toOql(person.age.in(18, 19, 20, 21))).is("age IN SET(18, 19, 20, 21)"); | ||
|
||
check(toOql(person.age.notIn(18, 19, 20, 21))).is("NOT (age IN SET(18, 19, 20, 21))"); | ||
} | ||
|
||
@Test | ||
void filterInWithBindParams() { | ||
check(toOqlWithBindParams(person.age.in(18, 19, 20, 21))).is("age IN $1"); | ||
|
||
check(toOqlWithBindParams(person.age.notIn(18, 19, 20, 21))).is("NOT (age IN $1)"); | ||
} | ||
|
||
@Test | ||
void filterNegation() { | ||
check(toOqlWithBindParams(person.age.not(self -> self.greaterThan(18)))).is("NOT (age > $1)"); | ||
} | ||
|
||
@Test | ||
void filterConjunction() { | ||
check(toOqlWithBindParams(person.age.greaterThan(18) | ||
.address.value().city.is("London") | ||
.and(person.isActive.isTrue()))) | ||
.is("((age > $1) AND (address.city = $2)) AND (isActive = $3)"); | ||
} | ||
|
||
@Test | ||
void filterDisjunction() { | ||
check(toOqlWithBindParams(person.age.greaterThan(18) | ||
.or(person.isActive.isTrue()))).is("(age > $1) OR (isActive = $2)"); | ||
} | ||
|
||
@Test | ||
void filterConjunctionWithDisjunction() { | ||
check(toOqlWithBindParams(person.age.greaterThan(18) | ||
.address.value().city.is("London") | ||
.or(person.isActive.isTrue()))).is("((age > $1) AND (address.city = $2)) OR (isActive = $3)"); | ||
} | ||
|
||
private static String toOqlWithBindParams(PersonCriteria personCriteria) { | ||
return toOql(personCriteria, true); | ||
} | ||
|
||
private static String toOql(PersonCriteria personCriteria) { | ||
return toOql(personCriteria, false); | ||
} | ||
|
||
private static String toOql(PersonCriteria personCriteria, boolean useBindVariables) { | ||
final PathNaming pathNaming = ReservedWordNaming.of(PathNaming.defaultNaming()); | ||
final GeodeQueryVisitor queryVisitor = new GeodeQueryVisitor(useBindVariables, pathNaming); | ||
return toExpression(personCriteria).accept(queryVisitor).oql(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters