Skip to content

Commit 10737a7

Browse files
committed
DATAMONGO-2138 - Implement and with registerCriteriaChainElement
1 parent f66ffb2 commit 10737a7

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ public Criteria andOperator(Criteria... criteria) {
679679
return registerCriteriaChainElement(new Criteria("$and").is(bsonList));
680680
}
681681

682-
private Criteria registerCriteriaChainElement(Criteria criteria) {
682+
Criteria registerCriteriaChainElement(Criteria criteria) {
683683

684684
if (lastOperatorWasNot()) {
685685
throw new IllegalArgumentException(

Diff for: spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensions.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ infix fun KProperty<*>.bits(bitwiseCriteria: Criteria.BitwiseCriteriaOperators.(
391391
* @since 2.2
392392
* @see Criteria.orOperator
393393
*/
394-
infix fun Criteria.orOperator(other: Criteria): Criteria = this.orOperator(other)
394+
infix fun Criteria.orOperator(other: Criteria): Criteria = Criteria().orOperator(this, other)
395395

396396
/**
397397
* Creates a 'nor' criteria using the $nor operator for all of the provided criteria.
@@ -401,7 +401,7 @@ infix fun Criteria.orOperator(other: Criteria): Criteria = this.orOperator(other
401401
* @since 2.2
402402
* @see Criteria.norOperator
403403
*/
404-
infix fun Criteria.norOperator(other: Criteria): Criteria = this.norOperator(other)
404+
infix fun Criteria.norOperator(other: Criteria): Criteria = Criteria().norOperator(this, other)
405405

406406
/**
407407
* Creates an 'and' criteria using the $and operator for all of the provided criteria.
@@ -411,10 +411,10 @@ infix fun Criteria.norOperator(other: Criteria): Criteria = this.norOperator(oth
411411
* @since 2.2
412412
* @see Criteria.andOperator
413413
*/
414-
infix fun Criteria.andOperator(other: Criteria): Criteria = this.andOperator(other)
414+
infix fun Criteria.andOperator(other: Criteria): Criteria = Criteria().andOperator(this, other)
415415

416416
infix fun Criteria.and(other: Criteria): Criteria {
417-
// TODO
418-
return this
417+
// TODO: Fix weird behaviour (test `isEqualTo and orOperator` fails)
418+
return registerCriteriaChainElement(other)
419419
}
420420

Diff for: spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensionsTests.kt

+34-3
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ class TypedCriteriaExtensionsTests {
211211
fun `not() should equal classic criteria`() {
212212

213213
// TODO: improve not() syntax
214-
val typed = Book::title.not().isEqualTo("Moby-Dick")
215-
val classic = Criteria("title").not().isEqualTo("Moby-Dick")
214+
val typed = Book::price.not().lt(123)
215+
val classic = Criteria("price").not().lt(123)
216216
assertEqualCriteria(typed, classic)
217217
}
218218

@@ -413,14 +413,45 @@ class TypedCriteriaExtensionsTests {
413413
}
414414

415415
@Test
416-
fun `infix or should equal classic criteria`() {
416+
fun `isEqualTo and orOperator should equal classic criteria`() {
417417

418418
val typed = (Book::title isEqualTo "Moby-Dick") and (Book::price lt 1200 orOperator (Book::price gt 240))
419419
val classic = Criteria("title").isEqualTo("Moby-Dick")
420420
.orOperator(Criteria("price").lt(1200), Criteria("price").gt(240))
421421
assertEqualCriteria(typed, classic)
422422
}
423423

424+
@Test
425+
fun `another complex chained composition`() {
426+
427+
val typed = (Book::author elemMatch (Author::name ne "Herman Melville")) and
428+
(Book::price lt 1200 orOperator (Book::price gt 240)) and
429+
(Book::title regex "^a" norOperator (Book::title regex "b$"))
430+
431+
val classic = Criteria("book.author").ne("Herman Melville")
432+
.orOperator(Criteria("price").lt(1200), Criteria("price").gt(240))
433+
.norOperator(Criteria("title").regex("^a"), Criteria("title").regex("b$"))
434+
assertEqualCriteria(typed, classic)
435+
}
436+
437+
@Test
438+
fun `orOperator and isEqualTo should equal classic criteria`() {
439+
440+
val typed = (Book::price lt 1200 orOperator (Book::price gt 240)) and (Book::title isEqualTo "Moby-Dick")
441+
val classic = Criteria()
442+
.orOperator(Criteria("price").lt(1200), Criteria("price").gt(240))
443+
.and("title").isEqualTo("Moby-Dick")
444+
assertEqualCriteria(typed, classic)
445+
}
446+
447+
@Test
448+
fun `infix or should equal classic criteria`() {
449+
450+
val typed = (Book::price lt 1200) orOperator (Book::price gt 240)
451+
val classic = Criteria().orOperator(Criteria("price").lt(1200), Criteria("price").gt(240))
452+
assertEqualCriteria(typed, classic)
453+
}
454+
424455
@Test
425456
fun `infix nor should equal classic criteria`() {
426457

0 commit comments

Comments
 (0)