Skip to content

Commit

Permalink
Add Criteria infix functions for maxDistance and minDistance.
Browse files Browse the repository at this point in the history
Closes: #3761
  • Loading branch information
sangyongchoi authored and mp911de committed Aug 25, 2021
1 parent 7c6e951 commit 302c803
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,28 @@ infix fun KProperty<GeoJson<*>>.maxDistance(d: Double): Criteria =
infix fun KProperty<GeoJson<*>>.minDistance(d: Double): Criteria =
Criteria(asString(this)).minDistance(d)

/**
* Creates a geo-spatial criterion using a $maxDistance operation, for use with $near
*
* See [MongoDB Query operator:
* $maxDistance](https://docs.mongodb.com/manual/reference/operator/query/maxDistance/)
* @author Sangyong Choi
* @since 3.2
* @see Criteria.maxDistance
*/
infix fun Criteria.maxDistance(d: Double): Criteria =
this.maxDistance(d)

/**
* Creates a geospatial criterion using a $minDistance operation, for use with $near or
* $nearSphere.
* @author Sangyong Choi
* @since 3.2
* @see Criteria.minDistance
*/
infix fun Criteria.minDistance(d: Double): Criteria =
this.minDistance(d)

/**
* Creates a criterion using the $elemMatch operator
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,54 @@ class TypedCriteriaExtensionsTests {
assertThat(typed).isEqualTo(expected)
}

@Test
fun `maxDistance() should equal expected criteria with nearSphere`() {
val point = Point(0.0, 0.0)

val typed = Building::location nearSphere point maxDistance 3.0
val expected = Criteria("location")
.nearSphere(point)
.maxDistance(3.0)

assertThat(typed).isEqualTo(expected)
}

@Test
fun `minDistance() should equal expected criteria with nearSphere`() {
val point = Point(0.0, 0.0)

val typed = Building::location nearSphere point minDistance 3.0
val expected = Criteria("location")
.nearSphere(point)
.minDistance(3.0)

assertThat(typed).isEqualTo(expected)
}

@Test
fun `maxDistance() should equal expected criteria with near`() {
val point = Point(0.0, 0.0)

val typed = Building::location near point maxDistance 3.0
val expected = Criteria("location")
.near(point)
.maxDistance(3.0)

assertThat(typed).isEqualTo(expected)
}

@Test
fun `minDistance() should equal expected criteria with near`() {
val point = Point(0.0, 0.0)

val typed = Building::location near point minDistance 3.0
val expected = Criteria("location")
.near(point)
.minDistance(3.0)

assertThat(typed).isEqualTo(expected)
}

@Test
fun `elemMatch() should equal expected criteria`() {

Expand Down

0 comments on commit 302c803

Please sign in to comment.