|
| 1 | + |
| 2 | +import com.mongodb.client.model.Aggregates |
| 3 | +import com.mongodb.client.model.Filters |
| 4 | +import com.mongodb.client.model.Sorts.descending |
| 5 | +import com.mongodb.kotlin.client.coroutine.MongoClient |
| 6 | +import io.github.cdimascio.dotenv.dotenv |
| 7 | +import kotlinx.coroutines.flow.toList |
| 8 | +import kotlinx.coroutines.runBlocking |
| 9 | +import org.bson.codecs.pojo.annotations.BsonId |
| 10 | +import org.junit.jupiter.api.AfterAll |
| 11 | +import org.junit.jupiter.api.Assertions.* |
| 12 | +import org.junit.jupiter.api.BeforeAll |
| 13 | +import org.junit.jupiter.api.TestInstance |
| 14 | +import java.util.* |
| 15 | +import kotlin.test.* |
| 16 | + |
| 17 | + |
| 18 | +// :snippet-start: skip-data-model |
| 19 | +data class PaintOrder( |
| 20 | + @BsonId val id: Int, |
| 21 | + val qty: Int, |
| 22 | + val color: String |
| 23 | +) |
| 24 | +// :snippet-end: |
| 25 | + |
| 26 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 27 | +internal class SkipTest { |
| 28 | + |
| 29 | + companion object { |
| 30 | + val dotenv = dotenv() |
| 31 | + val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"]) |
| 32 | + val database = client.getDatabase("paint_store") |
| 33 | + val collection = database.getCollection<PaintOrder>("paint_order") |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + @JvmStatic |
| 37 | + private fun beforeAll() { |
| 38 | + runBlocking { |
| 39 | + |
| 40 | + val paintOrders = listOf( |
| 41 | + PaintOrder(1, 5, "red"), |
| 42 | + PaintOrder(2, 10, "purple"), |
| 43 | + PaintOrder(3, 9, "blue"), |
| 44 | + PaintOrder(4, 6, "white"), |
| 45 | + PaintOrder(5, 11, "yellow"), |
| 46 | + PaintOrder(6, 3, "pink"), |
| 47 | + PaintOrder(7, 8, "green"), |
| 48 | + PaintOrder(8, 7, "orange") |
| 49 | + ) |
| 50 | + collection.insertMany(paintOrders) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + @AfterAll |
| 56 | + @JvmStatic |
| 57 | + private fun afterAll() { |
| 58 | + runBlocking { |
| 59 | + collection.drop() |
| 60 | + client.close() |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun basicSkipTest() = runBlocking { |
| 67 | + // :snippet-start: basic-skip |
| 68 | + collection.find().skip(2) |
| 69 | + // :snippet-end: |
| 70 | + |
| 71 | + // :snippet-start: aggregates-skip |
| 72 | + val filter = Filters.empty() |
| 73 | + val results = collection.aggregate(listOf( |
| 74 | + Aggregates.match(filter), |
| 75 | + Aggregates.skip(2)) |
| 76 | + ) |
| 77 | + // :snippet-end: |
| 78 | + |
| 79 | + // Junit test for the above code |
| 80 | + val expected = listOf( |
| 81 | + PaintOrder(3, 9, "blue"), |
| 82 | + PaintOrder(4, 6, "white"), |
| 83 | + PaintOrder(5, 11, "yellow"), |
| 84 | + PaintOrder(6, 3, "pink"), |
| 85 | + PaintOrder(7, 8, "green"), |
| 86 | + PaintOrder(8, 7, "orange") |
| 87 | + ) |
| 88 | + assertEquals(expected, results.toList()) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun findIterableTest() = runBlocking { |
| 93 | + // :snippet-start: find-iterable |
| 94 | + val filter = Filters.empty() |
| 95 | + val results = collection.find(filter) |
| 96 | + .sort(descending(PaintOrder::qty.name)) |
| 97 | + .skip(5) |
| 98 | + .collect { println(it) } |
| 99 | + // :snippet-end: |
| 100 | + // Junit test for the above code |
| 101 | + val expected = listOf( |
| 102 | + PaintOrder(4, 6, "white"), |
| 103 | + PaintOrder(1, 5, "red"), |
| 104 | + PaintOrder(6, 3, "pink") |
| 105 | + ) |
| 106 | + assertEquals(expected, collection.find(filter).sort(descending(PaintOrder::qty.name)) |
| 107 | + .skip(5).toList() ) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun basicAggregationTest() = runBlocking { |
| 112 | + // :snippet-start: aggregation |
| 113 | + val filter = Filters.empty() |
| 114 | + val aggregate = listOf( |
| 115 | + Aggregates.match(filter), |
| 116 | + Aggregates.sort(descending(PaintOrder::qty.name)), |
| 117 | + Aggregates.skip(5) |
| 118 | + ) |
| 119 | + collection.aggregate(aggregate).collect { println(it) } |
| 120 | + // :snippet-end: |
| 121 | + |
| 122 | + // Junit test for the above code |
| 123 | + val expected = listOf( |
| 124 | + PaintOrder(4, 6, "white"), |
| 125 | + PaintOrder(1, 5, "red"), |
| 126 | + PaintOrder(6, 3, "pink") |
| 127 | + ) |
| 128 | + assertEquals(expected, collection.aggregate(aggregate).toList()) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun noResultsTest() = runBlocking { |
| 133 | + // :snippet-start: no-results |
| 134 | + val filter = Filters.empty() |
| 135 | + val emptyQuery = listOf( |
| 136 | + Aggregates.match(filter), |
| 137 | + Aggregates.sort(descending(PaintOrder::qty.name)), |
| 138 | + Aggregates.skip(9) |
| 139 | + ) |
| 140 | + collection.aggregate(emptyQuery).collect { println(it) } |
| 141 | + // :snippet-end: |
| 142 | + // Junit test for the above code |
| 143 | + assertTrue(collection.aggregate(emptyQuery).toList().isEmpty()) |
| 144 | + } |
| 145 | +} |
0 commit comments