From a625ae3378ee940ac03ae789bdfcbaec2d080279 Mon Sep 17 00:00:00 2001 From: Jake Son Date: Sat, 30 Sep 2023 15:12:02 +0900 Subject: [PATCH] test: add project examples --- .../mongodb/repository/ProjectRepository.kt | 44 +++++++++++++++++++ .../repository/SortByCountRepository.kt | 6 +-- .../repository/ProjectRepositoryTest.kt | 38 ++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepository.kt create mode 100644 example/spring-data-mongodb/src/test/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepositoryTest.kt diff --git a/example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepository.kt b/example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepository.kt new file mode 100644 index 00000000..6c19590f --- /dev/null +++ b/example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepository.kt @@ -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 Conditionally Exclude Fields + */ + fun excludeConditionally(): AggregationResults { + 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" + } +} diff --git a/example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/SortByCountRepository.kt b/example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/SortByCountRepository.kt index 5a8afa43..d06c91de 100644 --- a/example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/SortByCountRepository.kt +++ b/example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/SortByCountRepository.kt @@ -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 Example */ - fun sortByTags(): AggregationResults { + fun sortByTags(): AggregationResults { 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 { diff --git a/example/spring-data-mongodb/src/test/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepositoryTest.kt b/example/spring-data-mongodb/src/test/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepositoryTest.kt new file mode 100644 index 00000000..2b5f4c27 --- /dev/null +++ b/example/spring-data-mongodb/src/test/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepositoryTest.kt @@ -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", + ) + } +})