Skip to content

Commit

Permalink
resolves #150 add missing temporal typed aggregate operators and gene…
Browse files Browse the repository at this point in the history
…ric aggregate operator
  • Loading branch information
zigzago committed Oct 8, 2019
1 parent ffec0d1 commit 7718efa
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package org.litote.kmongo

import com.mongodb.client.model.Projections
import org.bson.types.ObjectId
import org.junit.Test
import org.junit.experimental.categories.Categories
import org.junit.experimental.categories.Categories.IncludeCategory
import org.junit.experimental.categories.Category
import org.litote.kmongo.MongoOperator.oid
import java.time.Instant
import kotlin.test.assertEquals

@Category(JacksonMappingCategory::class, NativeMappingCategory::class)
class JsonExtensionTest : KMongoRootTest(){
class JsonExtensionTest : KMongoRootTest() {

class DataTest(var a: String)

Expand All @@ -42,20 +42,57 @@ class JsonExtensionTest : KMongoRootTest(){
fun pojoToBson() = assertEquals("{\"first\":\"z\",\"second\":4}", Pair("z", 4).json)

@Test
fun arrayToBson() = assertEquals("[\"z\\\"z\",\"aa\"]", arrayOf("z\"z", "aa").json.replace(" ",""))
fun arrayToBson() = assertEquals("[\"z\\\"z\",\"aa\"]", arrayOf("z\"z", "aa").json.replace(" ", ""))

@Test
fun objectIdToBson() {
val id = ObjectId()
assertEquals("{\"$oid\":\"$id\"}", id.json.replace(" ",""))
assertEquals("{\"$oid\":\"$id\"}", id.json.replace(" ", ""))
}

@Test
fun objectMutation() {
val data = DataTest("a")
assertEquals("{\"a\":\"a\"}", data.json.replace(" ",""))
assertEquals("{\"a\":\"a\"}", data.json.replace(" ", ""))
data.a = "b"
assertEquals("{\"a\":\"b\"}", data.json.replace(" ",""))
assertEquals("{\"a\":\"b\"}", data.json.replace(" ", ""))
}

@Test
fun `week aggregate`() {
data class Base(val timestamp: Instant)
data class Result(val week: Int, val documentCount: Int)

val pipeline = """
{
"$ group": {
"_id": {"$ week": "$ timestamp"},
"documentCount": {"$ sum": 1}
}
}""".formatJson().replace(" ", "").replace("\n", "")

val bson =
group(
Projections.computed("\$week", Base::timestamp.projection),
Result::documentCount sum (1)
)

val bson2 =
group(
week(Base::timestamp),
Result::documentCount sum (1)
)

val bson3 =
group(
MongoOperator.week.op(Base::timestamp),
Result::documentCount sum (1)
)

assertEquals(pipeline.replace(" ", ""), bson.json.replace(" ", ""))
assertEquals(pipeline.replace(" ", ""), bson2.json.replace(" ", ""))
assertEquals(pipeline.replace(" ", ""), bson3.json.replace(" ", ""))
}

}

44 changes: 43 additions & 1 deletion kmongo-property/src/main/kotlin/org/litote/kmongo/Aggregates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,46 @@ fun dayOfWeek(property: KProperty<TemporalAccessor?>): Bson =
* Builds $year expression for this property .
*/
fun year(property: KProperty<TemporalAccessor?>): Bson =
Projections.computed("\$year", property.projection)
Projections.computed("\$year", property.projection)

/**
* Builds $hour expression for this property .
*/
fun hour(property: KProperty<TemporalAccessor?>): Bson =
Projections.computed("\$hour", property.projection)

/**
* Builds $millisecond expression for this property .
*/
fun millisecond(property: KProperty<TemporalAccessor?>): Bson =
Projections.computed("\$millisecond", property.projection)

/**
* Builds $minute expression for this property .
*/
fun minute(property: KProperty<TemporalAccessor?>): Bson =
Projections.computed("\$minute", property.projection)

/**
* Builds $month expression for this property .
*/
fun month(property: KProperty<TemporalAccessor?>): Bson =
Projections.computed("\$month", property.projection)

/**
* Builds $second expression for this property .
*/
fun second(property: KProperty<TemporalAccessor?>): Bson =
Projections.computed("\$second", property.projection)

/**
* Builds $week expression for this property .
*/
fun week(property: KProperty<TemporalAccessor?>): Bson =
Projections.computed("\$week", property.projection)

/**
* Builds the [MongoOperator] expression for the specified property.
*/
fun MongoOperator.op(property: KProperty<Any?>): Bson =
Projections.computed(toString(), property.projection)

0 comments on commit 7718efa

Please sign in to comment.