-
Notifications
You must be signed in to change notification settings - Fork 55
fix: correctly convert key fields during paginated Scan and Query operations
#1614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,8 @@ | ||
| { | ||
| "id": "02bbf5e7-68d7-480d-a94c-35cd39dfe0db", | ||
| "type": "bugfix", | ||
| "description": "Correctly convert key fields during paginated `Scan` and `Query` operations", | ||
| "issues": [ | ||
| "awslabs/aws-sdk-kotlin#1596" | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
116 changes: 116 additions & 0 deletions
116
...ynamodb-mapper/jvm/test/aws/sdk/kotlin/hll/dynamodbmapper/operations/PaginatedScanTest.kt
This file contains hidden or 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,116 @@ | ||
| package aws.sdk.kotlin.hll.dynamodbmapper.operations | ||
|
|
||
| import aws.sdk.kotlin.hll.dynamodbmapper.items.* | ||
| import aws.sdk.kotlin.hll.dynamodbmapper.model.Item | ||
| import aws.sdk.kotlin.hll.dynamodbmapper.model.itemOf | ||
| import aws.sdk.kotlin.hll.dynamodbmapper.testutils.DdbLocalTest | ||
| import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.IntConverter | ||
| import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.StringConverter | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.flow.toSet | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class PaginatedScanTest : DdbLocalTest() { | ||
| companion object { | ||
| private const val TABLE_NAME = "paginated-scan-test" | ||
|
|
||
| private fun rankName(rank: Int) = when (rank) { | ||
| 2, 3, 4, 5, 6, 7, 8, 9, 10 -> rank.toString() | ||
| 11 -> "Jack" | ||
| 12 -> "Queen" | ||
| 13 -> "King" | ||
| 14 -> "Ace" | ||
| else -> "Unknown ($rank)" | ||
| } | ||
|
|
||
| private data class Card( | ||
| var rank: Int = 14, | ||
| var suit: String = "Spades", | ||
| var description: String = "This is the Ace of Spades. ".repeat(2000), | ||
| ) { | ||
| val rankName: String | ||
| get() = rankName(rank) | ||
|
|
||
| override fun toString() = "$rankName of $suit" // Easier when debugging big outputs | ||
| } | ||
|
|
||
| private val converter = object : ItemConverter<Card> { | ||
| val delegate = SimpleItemConverter( | ||
| ::Card, | ||
| { this }, | ||
| AttributeDescriptor("rank", Card::rank, Card::rank::set, IntConverter), | ||
| AttributeDescriptor("suit", Card::suit, Card::suit::set, StringConverter), | ||
| AttributeDescriptor("description", Card::description, Card::description::set, StringConverter), | ||
| ) | ||
|
|
||
| override fun convertFrom(to: Item): Card = delegate.convertFrom(to) | ||
|
|
||
| override fun convertTo(from: Card, onlyAttributes: Set<String>?): Item = | ||
| delegate.convertTo(from, null) // Ignore `onlyAttributes` arg to mock badly-behaved converter | ||
| } | ||
|
|
||
| private val schema = ItemSchema(converter, KeySpec.String("suit"), KeySpec.Number("rank")) | ||
|
|
||
| private val allCards = listOf("Spades", "Clubs", "Hearts", "Diamonds").flatMap { suit -> | ||
| (2..14).map { rank -> | ||
| Card(rank, suit, "This is the ${rankName(rank)} of $suit. ".repeat(2000)) | ||
| } | ||
| }.toSet() | ||
| } | ||
|
|
||
| @BeforeAll | ||
| fun setUp() = runTest { | ||
| createTable( | ||
| name = TABLE_NAME, | ||
| schema = schema, | ||
| items = allCards.map { card -> | ||
| itemOf( | ||
| "rank" to card.rank, | ||
| "suit" to card.suit, | ||
| "description" to card.description, | ||
| ) | ||
| }.toTypedArray(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testPaginatedScan() = runTest { | ||
| val mapper = mapper() | ||
| val table = mapper.getTable(TABLE_NAME, schema) | ||
| var pages = 0 | ||
|
|
||
| val actual = table | ||
| .scanPaginated { } | ||
| .map { | ||
| pages++ | ||
| it | ||
| } | ||
| .items() | ||
| .toSet() | ||
| assertEquals(allCards, actual) | ||
| assertTrue(pages > 1, "Got only $pages pages but expected at least 2") | ||
| } | ||
|
|
||
| @Test | ||
| fun testPaginatedScanWithOffset() = runTest { | ||
| val mapper = mapper() | ||
| val table = mapper.getTable(TABLE_NAME, schema) | ||
| val startKey = allCards.first() | ||
| var pages = 0 | ||
|
|
||
| val actual = table | ||
| .scanPaginated { | ||
| exclusiveStartKey = startKey | ||
| } | ||
| .map { | ||
| pages++ | ||
| it | ||
| } | ||
| .items() | ||
| .toSet() | ||
| assertTrue(startKey !in actual) | ||
| assertTrue(pages > 1, "Got only $pages pages but expected at least 2") | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment: No clue why this is no longer necessary. 🤷♂️