Skip to content

Commit

Permalink
misc: dynamodb e2e test (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez authored Jun 18, 2024
1 parent 658a859 commit 30f3e5d
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
11 changes: 11 additions & 0 deletions services/dynamodb/e2eTest/src/DynamoDbTestUtils.kt
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
121 changes: 121 additions & 0 deletions services/dynamodb/e2eTest/src/PaginatorTest.kt
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"))
}
}

0 comments on commit 30f3e5d

Please sign in to comment.