-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
.../main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/SampleRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.github.inflab.example.spring.data.mongodb.repository | ||
|
||
import com.github.inflab.spring.data.mongodb.core.aggregation.aggregation | ||
import org.springframework.data.annotation.Id | ||
import org.springframework.data.mongodb.core.MongoTemplate | ||
import org.springframework.data.mongodb.core.aggregate | ||
import org.springframework.data.mongodb.core.aggregation.AggregationResults | ||
import org.springframework.data.mongodb.core.mapping.Document | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class SampleRepository( | ||
private val mongoTemplate: MongoTemplate, | ||
) { | ||
|
||
@Document("user") | ||
data class User(@Id val id: String, val name: String, val q1: Boolean, val q2: Boolean) | ||
|
||
/** | ||
* @see <a href="https://www.mongodb.com/docs/manual/reference/operator/aggregation/sample/#example">Example</a> | ||
*/ | ||
fun example(): AggregationResults<User> { | ||
val aggregation = aggregation { | ||
sample(3) | ||
} | ||
|
||
return mongoTemplate.aggregate<User, User>(aggregation) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...t/kotlin/com/github/inflab/example/spring/data/mongodb/repository/SampleRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.inflab.example.spring.data.mongodb.repository | ||
|
||
import com.github.inflab.example.spring.data.mongodb.extension.makeMongoTemplate | ||
import com.github.inflab.example.spring.data.mongodb.repository.SampleRepository.User | ||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
import org.springframework.data.mongodb.core.MongoTemplate | ||
|
||
internal class SampleRepositoryTest : FreeSpec({ | ||
val mongoTemplate: MongoTemplate = makeMongoTemplate() | ||
val sampleRepository = SampleRepository(mongoTemplate) | ||
|
||
beforeSpec { | ||
val documents = listOf<User>( | ||
User(id = "1", name = "dave123", q1 = true, q2 = true), | ||
User(id = "2", name = "dave2", q1 = false, q2 = false), | ||
User(id = "3", name = "ahn", q1 = true, q2 = true), | ||
User(id = "4", name = "li", q1 = true, q2 = false), | ||
User(id = "5", name = "annT", q1 = false, q2 = true), | ||
User(id = "6", name = "li", q1 = true, q2 = true), | ||
User(id = "7", name = "ty", q1 = false, q2 = true), | ||
) | ||
|
||
mongoTemplate.insertAll(documents) | ||
} | ||
|
||
"example" { | ||
// when | ||
val result = sampleRepository.example() | ||
|
||
// then | ||
result.mappedResults.toSet().size shouldBe 3 | ||
} | ||
}) |