Skip to content

Commit

Permalink
test: add project examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 committed Sep 30, 2023
1 parent 85ee16e commit a625ae3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.spring.data.mongodb.core.aggregation.aggregation
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.aggregation.Aggregation.REMOVE
import org.springframework.data.mongodb.core.aggregation.AggregationResults
import org.springframework.data.mongodb.core.aggregation.ComparisonOperators
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators
import org.springframework.stereotype.Repository

@Repository
class ProjectRepository(
private val mongoTemplate: MongoTemplate,
) {

data class Author(val first: String, val last: String, val middle: String?)
data class TitleAndAuthorDto(
val id: String,
val title: String,
val author: Author,
)

/**
* @see <a href="https://www.mongodb.com/docs/manual/reference/operator/aggregation/project/#conditionally-exclude-fields">Conditionally Exclude Fields</a>
*/
fun excludeConditionally(): AggregationResults<TitleAndAuthorDto> {
val aggregation = aggregation {
project {
+"title"
"author.first" alias "author.first"
"author.last" alias "author.last"
"author.middle" expression ConditionalOperators.`when`(
ComparisonOperators.valueOf("author.middle").equalToValue(""),
).thenValueOf(REMOVE).otherwiseValueOf("author.middle")
}
}

return mongoTemplate.aggregate(aggregation, BOOKS, TitleAndAuthorDto::class.java)
}

companion object {
const val BOOKS = "books"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ class SortByCountRepository(
private val mongoTemplate: MongoTemplate,
) {

data class IdAndCount(val id: String, val count: Int)
data class IdAndCountDto(val id: String, val count: Int)

/**
* @see <a href="https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortByCount/#example">Example</a>
*/
fun sortByTags(): AggregationResults<IdAndCount> {
fun sortByTags(): AggregationResults<IdAndCountDto> {
val aggregation = aggregation {
unwind { path("tags") }
sortByCount("tags")
}

return mongoTemplate.aggregate(aggregation, EXHIBITS, IdAndCount::class.java)
return mongoTemplate.aggregate(aggregation, EXHIBITS, IdAndCountDto::class.java)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.makeMongoTemplate
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.shouldBe
import org.bson.Document

internal class ProjectRepositoryTest : FreeSpec({
val mongoTemplate = makeMongoTemplate()
val sortByCountRepository = ProjectRepository(mongoTemplate)

beforeSpec {
val documents = listOf(
mapOf("_id" to 1, "title" to "abc123", "isbn" to "0001122223334", "author" to mapOf("last" to "zzz", "first" to "aaa"), "copies" to 5, "lastModified" to "2016-07-28"),
mapOf("_id" to 2, "title" to "Baked Goods", "isbn" to "9999999999999", "author" to mapOf("last" to "xyz", "first" to "abc", "middle" to ""), "copies" to 2, "lastModified" to "2017-07-21"),
mapOf("_id" to 3, "title" to "Ice Cream Cakes", "isbn" to "8888888888888", "author" to mapOf("last" to "xyz", "first" to "abc", "middle" to "mmm"), "copies" to 5, "lastModified" to "2017-07-22"),
).map(::Document)

mongoTemplate.insert(documents, ProjectRepository.BOOKS)
}

"excludeConditionally" {
// when
val result = sortByCountRepository.excludeConditionally()

// then
result.mappedResults.map { it.title } shouldBe listOf(
"abc123",
"Baked Goods",
"Ice Cream Cakes",
)
result.mappedResults.map { it.author.middle } shouldBe listOf(
null,
null,
"mmm",
)
}
})

0 comments on commit a625ae3

Please sign in to comment.