Skip to content

Commit 30f3e5d

Browse files
authored
misc: dynamodb e2e test (#1332)
1 parent 658a859 commit 30f3e5d

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.services.dynamodb
6+
7+
/**
8+
* Checks if a Dynamo DB table exists based on its name
9+
*/
10+
internal suspend fun DynamoDbClient.tableExists(name: String): Boolean =
11+
this.listTables {}.tableNames?.contains(name) ?: false
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.services.dynamodb
6+
7+
import aws.sdk.kotlin.services.dynamodb.model.*
8+
import aws.sdk.kotlin.services.dynamodb.paginators.scanPaginated
9+
import aws.sdk.kotlin.services.dynamodb.waiters.waitUntilTableExists
10+
import kotlinx.coroutines.runBlocking
11+
import kotlinx.coroutines.test.runTest
12+
import org.junit.jupiter.api.AfterAll
13+
import org.junit.jupiter.api.BeforeAll
14+
import org.junit.jupiter.api.TestInstance
15+
import kotlin.random.Random
16+
import kotlin.test.Test
17+
import kotlin.test.assertEquals
18+
import kotlin.time.Duration.Companion.seconds
19+
20+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
21+
class PaginatorTest {
22+
private val client = DynamoDbClient { region = "us-west-2" }
23+
private val table = "testTable${Random.nextInt()}"
24+
25+
@BeforeAll
26+
private fun setUp(): Unit = runBlocking {
27+
if (!client.tableExists(table)) {
28+
client.createTable {
29+
tableName = table
30+
attributeDefinitions = listOf(
31+
AttributeDefinition {
32+
attributeName = "Artist"
33+
attributeType = ScalarAttributeType.S
34+
},
35+
AttributeDefinition {
36+
attributeName = "SongTitle"
37+
attributeType = ScalarAttributeType.S
38+
},
39+
)
40+
keySchema = listOf(
41+
KeySchemaElement {
42+
attributeName = "Artist"
43+
keyType = KeyType.Hash
44+
},
45+
KeySchemaElement {
46+
attributeName = "SongTitle"
47+
keyType = KeyType.Range
48+
},
49+
)
50+
provisionedThroughput = ProvisionedThroughput {
51+
readCapacityUnits = 5
52+
writeCapacityUnits = 5
53+
}
54+
tableClass = TableClass.Standard
55+
}
56+
57+
client.waitUntilTableExists {
58+
tableName = table
59+
}
60+
}
61+
}
62+
63+
@AfterAll
64+
private fun cleanUp(): Unit = runBlocking {
65+
if (client.tableExists(table)) {
66+
client.deleteTable {
67+
tableName = table
68+
}
69+
}
70+
client.close()
71+
}
72+
73+
@Test
74+
fun scanPaginatedRespectsExclusiveStartKey() = runTest(
75+
timeout = 20.seconds,
76+
) {
77+
client.putItem {
78+
tableName = table
79+
item = mapOf(
80+
"Artist" to AttributeValue.S("Foo"),
81+
"SongTitle" to AttributeValue.S("Bar"),
82+
)
83+
}
84+
85+
client.putItem {
86+
tableName = table
87+
item = mapOf(
88+
"Artist" to AttributeValue.S("Foo"),
89+
"SongTitle" to AttributeValue.S("Baz"),
90+
)
91+
}
92+
93+
client.putItem {
94+
tableName = table
95+
item = mapOf(
96+
"Artist" to AttributeValue.S("Foo"),
97+
"SongTitle" to AttributeValue.S("Qux"),
98+
)
99+
}
100+
101+
val results = mutableListOf<Map<String, AttributeValue>?>()
102+
103+
client.scanPaginated {
104+
tableName = table
105+
exclusiveStartKey = mapOf(
106+
"Artist" to AttributeValue.S("Foo"),
107+
"SongTitle" to AttributeValue.S("Bar"),
108+
)
109+
limit = 1
110+
}.collect { scan ->
111+
if (scan.items?.isNotEmpty() == true) {
112+
results.add(scan.items.single())
113+
}
114+
}
115+
116+
assertEquals(2, results.size)
117+
// NOTE: Items are returned in alphabetical order
118+
assertEquals((AttributeValue.S("Baz")), results[0]?.get("SongTitle"))
119+
assertEquals((AttributeValue.S("Qux")), results[1]?.get("SongTitle"))
120+
}
121+
}

0 commit comments

Comments
 (0)