|
| 1 | + |
| 2 | + |
| 3 | +import com.mongodb.ExplainVerbosity |
| 4 | +import com.mongodb.kotlin.client.coroutine.MongoClient |
| 5 | +import config.getConfig |
| 6 | +import kotlinx.coroutines.flow.* |
| 7 | +import kotlinx.coroutines.runBlocking |
| 8 | +import org.bson.Document |
| 9 | +import org.junit.jupiter.api.AfterAll |
| 10 | +import org.junit.jupiter.api.Assertions.* |
| 11 | +import org.junit.jupiter.api.BeforeAll |
| 12 | +import org.junit.jupiter.api.TestInstance |
| 13 | +import java.util.* |
| 14 | +import kotlin.test.* |
| 15 | + |
| 16 | + |
| 17 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 18 | +internal class FlowTest { |
| 19 | + |
| 20 | + data class PaintOrder( |
| 21 | + val id: Int, |
| 22 | + val color: String, |
| 23 | + val qty: Int |
| 24 | + ) |
| 25 | + companion object { |
| 26 | + val config = getConfig() |
| 27 | + val client = MongoClient.create(config.connectionUri) |
| 28 | + val database = client.getDatabase("paint_store") |
| 29 | + val collection = database.getCollection<PaintOrder>("paint_order") |
| 30 | + |
| 31 | + @BeforeAll |
| 32 | + @JvmStatic |
| 33 | + fun beforeAll() { |
| 34 | + runBlocking { |
| 35 | + collection.insertMany(listOf( |
| 36 | + PaintOrder(1, "red", 5), |
| 37 | + PaintOrder(2, "purple", 8), |
| 38 | + PaintOrder(3, "yellow", 0), |
| 39 | + PaintOrder(4, "green", 6), |
| 40 | + PaintOrder(5, "pink", 0) |
| 41 | + )) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @AfterAll |
| 46 | + @JvmStatic |
| 47 | + fun afterAll() { |
| 48 | + runBlocking { |
| 49 | + collection.drop() |
| 50 | + client.close() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun firstOrNullTest() = runBlocking { |
| 57 | + // :snippet-start: firstOrNull |
| 58 | + val resultsFlow = collection.find() |
| 59 | + val firstResultOrNull = resultsFlow.firstOrNull() |
| 60 | + // :snippet-end: |
| 61 | + assertNotNull(firstResultOrNull) |
| 62 | + } |
| 63 | + @Test |
| 64 | + fun firstTest() = runBlocking { |
| 65 | + var isReached = false |
| 66 | + // :snippet-start: first |
| 67 | + try { |
| 68 | + val resultsFlow = collection.find() |
| 69 | + val firstResult = resultsFlow.first() |
| 70 | + isReached = true // :remove: |
| 71 | + } catch (e: NoSuchElementException) { |
| 72 | + println("No results found") |
| 73 | + } |
| 74 | + // :snippet-end: |
| 75 | + assert(isReached) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun countTest() = runBlocking { |
| 80 | + // :snippet-start: count |
| 81 | + val resultsFlow = collection.find() |
| 82 | + val count = resultsFlow.count() |
| 83 | + // :snippet-end: |
| 84 | + assertEquals(5, count) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun toListTest() = runBlocking { |
| 89 | + // :snippet-start: toList |
| 90 | + val resultsFlow = collection.find() |
| 91 | + val results = resultsFlow.toList() |
| 92 | + // :snippet-end: |
| 93 | + assertEquals(5, results.size) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun iterateTest() = runBlocking { |
| 98 | + // :snippet-start: iterate |
| 99 | + val resultsFlow = collection.find() |
| 100 | + resultsFlow.collect { println(it) } |
| 101 | + // :snippet-end: |
| 102 | + assertEquals(5, resultsFlow.count()) |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun explainTest() = runBlocking { |
| 107 | + // :snippet-start: explain |
| 108 | + val explanation = collection.find().explain(ExplainVerbosity.EXECUTION_STATS) |
| 109 | + val jsonSummary = explanation.getEmbedded( |
| 110 | + listOf("queryPlanner", "winningPlan"), |
| 111 | + Document::class.java |
| 112 | + ).toJson() |
| 113 | + println(jsonSummary) |
| 114 | + // :snippet-end: |
| 115 | + val expected = """{"stage": "COLLSCAN", "direction": "forward"} |
| 116 | + """.trimIndent() |
| 117 | + assertEquals(expected, jsonSummary) |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments