|
| 1 | +package aws.sdk.kotlin.hll.dynamodbmapper.operations |
| 2 | + |
| 3 | +import aws.sdk.kotlin.hll.dynamodbmapper.items.* |
| 4 | +import aws.sdk.kotlin.hll.dynamodbmapper.model.Item |
| 5 | +import aws.sdk.kotlin.hll.dynamodbmapper.model.itemOf |
| 6 | +import aws.sdk.kotlin.hll.dynamodbmapper.testutils.DdbLocalTest |
| 7 | +import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.IntConverter |
| 8 | +import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.StringConverter |
| 9 | +import kotlinx.coroutines.flow.map |
| 10 | +import kotlinx.coroutines.flow.toSet |
| 11 | +import kotlinx.coroutines.test.runTest |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertTrue |
| 14 | + |
| 15 | +class PaginatedScanTest : DdbLocalTest() { |
| 16 | + companion object { |
| 17 | + private const val TABLE_NAME = "paginated-scan-test" |
| 18 | + |
| 19 | + private fun rankName(rank: Int) = when (rank) { |
| 20 | + 2, 3, 4, 5, 6, 7, 8, 9, 10 -> rank.toString() |
| 21 | + 11 -> "Jack" |
| 22 | + 12 -> "Queen" |
| 23 | + 13 -> "King" |
| 24 | + 14 -> "Ace" |
| 25 | + else -> "Unknown ($rank)" |
| 26 | + } |
| 27 | + |
| 28 | + private data class Card( |
| 29 | + var rank: Int = 14, |
| 30 | + var suit: String = "Spades", |
| 31 | + var description: String = "This is the Ace of Spades. ".repeat(2000), |
| 32 | + ) { |
| 33 | + val rankName: String |
| 34 | + get() = rankName(rank) |
| 35 | + |
| 36 | + override fun toString() = "$rankName of $suit" // Easier when debugging big outputs |
| 37 | + } |
| 38 | + |
| 39 | + private val converter = object : ItemConverter<Card> { |
| 40 | + val delegate = SimpleItemConverter( |
| 41 | + ::Card, |
| 42 | + { this }, |
| 43 | + AttributeDescriptor("rank", Card::rank, Card::rank::set, IntConverter), |
| 44 | + AttributeDescriptor("suit", Card::suit, Card::suit::set, StringConverter), |
| 45 | + AttributeDescriptor("description", Card::description, Card::description::set, StringConverter), |
| 46 | + ) |
| 47 | + |
| 48 | + override fun convertFrom(to: Item): Card = delegate.convertFrom(to) |
| 49 | + |
| 50 | + override fun convertTo(from: Card, onlyAttributes: Set<String>?): Item = |
| 51 | + delegate.convertTo(from, null) // Ignore `onlyAttributes` arg to mock badly-behaved converter |
| 52 | + } |
| 53 | + |
| 54 | + private val schema = ItemSchema(converter, KeySpec.String("suit"), KeySpec.Number("rank")) |
| 55 | + |
| 56 | + private val allCards = listOf("Spades", "Clubs", "Hearts", "Diamonds").flatMap { suit -> |
| 57 | + (2..14).map { rank -> |
| 58 | + Card(rank, suit, "This is the ${rankName(rank)} of $suit. ".repeat(2000)) |
| 59 | + } |
| 60 | + }.toSet() |
| 61 | + } |
| 62 | + |
| 63 | + @BeforeAll |
| 64 | + fun setUp() = runTest { |
| 65 | + createTable( |
| 66 | + name = TABLE_NAME, |
| 67 | + schema = schema, |
| 68 | + items = allCards.map { card -> |
| 69 | + itemOf( |
| 70 | + "rank" to card.rank, |
| 71 | + "suit" to card.suit, |
| 72 | + "description" to card.description, |
| 73 | + ) |
| 74 | + }.toTypedArray(), |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun testPaginatedScan() = runTest { |
| 80 | + val mapper = mapper() |
| 81 | + val table = mapper.getTable(TABLE_NAME, schema) |
| 82 | + var pages = 0 |
| 83 | + |
| 84 | + val actual = table |
| 85 | + .scanPaginated { } |
| 86 | + .map { |
| 87 | + pages++ |
| 88 | + it |
| 89 | + } |
| 90 | + .items() |
| 91 | + .toSet() |
| 92 | + assertEquals(allCards, actual) |
| 93 | + assertTrue(pages > 1, "Got only $pages pages but expected at least 2") |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun testPaginatedScanWithOffset() = runTest { |
| 98 | + val mapper = mapper() |
| 99 | + val table = mapper.getTable(TABLE_NAME, schema) |
| 100 | + val startKey = allCards.first() |
| 101 | + var pages = 0 |
| 102 | + |
| 103 | + val actual = table |
| 104 | + .scanPaginated { |
| 105 | + exclusiveStartKey = startKey |
| 106 | + } |
| 107 | + .map { |
| 108 | + pages++ |
| 109 | + it |
| 110 | + } |
| 111 | + .items() |
| 112 | + .toSet() |
| 113 | + assertTrue(startKey !in actual) |
| 114 | + assertTrue(pages > 1, "Got only $pages pages but expected at least 2") |
| 115 | + } |
| 116 | +} |
0 commit comments