-
Notifications
You must be signed in to change notification settings - Fork 50
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
2 changed files
with
132 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.services.dynamodb | ||
|
||
/** | ||
* Checks if a Dynamo DB table exists based on its name | ||
*/ | ||
internal suspend fun DynamoDbClient.tableExists(name: String): Boolean = | ||
this.listTables {}.tableNames?.contains(name) ?: false |
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,121 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.services.dynamodb | ||
|
||
import aws.sdk.kotlin.services.dynamodb.model.* | ||
import aws.sdk.kotlin.services.dynamodb.paginators.scanPaginated | ||
import aws.sdk.kotlin.services.dynamodb.waiters.waitUntilTableExists | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.TestInstance | ||
import kotlin.random.Random | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class PaginatorTest { | ||
private val client = DynamoDbClient { region = "us-west-2" } | ||
private val table = "testTable${Random.nextInt()}" | ||
|
||
@BeforeAll | ||
private fun setUp(): Unit = runBlocking { | ||
if (!client.tableExists(table)) { | ||
client.createTable { | ||
tableName = table | ||
attributeDefinitions = listOf( | ||
AttributeDefinition { | ||
attributeName = "Artist" | ||
attributeType = ScalarAttributeType.S | ||
}, | ||
AttributeDefinition { | ||
attributeName = "SongTitle" | ||
attributeType = ScalarAttributeType.S | ||
}, | ||
) | ||
keySchema = listOf( | ||
KeySchemaElement { | ||
attributeName = "Artist" | ||
keyType = KeyType.Hash | ||
}, | ||
KeySchemaElement { | ||
attributeName = "SongTitle" | ||
keyType = KeyType.Range | ||
}, | ||
) | ||
provisionedThroughput = ProvisionedThroughput { | ||
readCapacityUnits = 5 | ||
writeCapacityUnits = 5 | ||
} | ||
tableClass = TableClass.Standard | ||
} | ||
|
||
client.waitUntilTableExists { | ||
tableName = table | ||
} | ||
} | ||
} | ||
|
||
@AfterAll | ||
private fun cleanUp(): Unit = runBlocking { | ||
if (client.tableExists(table)) { | ||
client.deleteTable { | ||
tableName = table | ||
} | ||
} | ||
client.close() | ||
} | ||
|
||
@Test | ||
fun scanPaginatedRespectsExclusiveStartKey() = runTest( | ||
timeout = 20.seconds, | ||
) { | ||
client.putItem { | ||
tableName = table | ||
item = mapOf( | ||
"Artist" to AttributeValue.S("Foo"), | ||
"SongTitle" to AttributeValue.S("Bar"), | ||
) | ||
} | ||
|
||
client.putItem { | ||
tableName = table | ||
item = mapOf( | ||
"Artist" to AttributeValue.S("Foo"), | ||
"SongTitle" to AttributeValue.S("Baz"), | ||
) | ||
} | ||
|
||
client.putItem { | ||
tableName = table | ||
item = mapOf( | ||
"Artist" to AttributeValue.S("Foo"), | ||
"SongTitle" to AttributeValue.S("Qux"), | ||
) | ||
} | ||
|
||
val results = mutableListOf<Map<String, AttributeValue>?>() | ||
|
||
client.scanPaginated { | ||
tableName = table | ||
exclusiveStartKey = mapOf( | ||
"Artist" to AttributeValue.S("Foo"), | ||
"SongTitle" to AttributeValue.S("Bar"), | ||
) | ||
limit = 1 | ||
}.collect { scan -> | ||
if (scan.items?.isNotEmpty() == true) { | ||
results.add(scan.items.single()) | ||
} | ||
} | ||
|
||
assertEquals(2, results.size) | ||
// NOTE: Items are returned in alphabetical order | ||
assertEquals((AttributeValue.S("Baz")), results[0]?.get("SongTitle")) | ||
assertEquals((AttributeValue.S("Qux")), results[1]?.get("SongTitle")) | ||
} | ||
} |