Skip to content

Commit 8b02b88

Browse files
committed
misc: set up initial tests
1 parent 612c39b commit 8b02b88

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

codegen/smithy-kotlin-codegen-testutils/src/main/kotlin/software/amazon/smithy/kotlin/codegen/test/CodegenTestUtils.kt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ fun codegenSerializerForShape(
6363
val resolvedSettings = settings ?: model.defaultSettings(TestModelDefault.SERVICE_NAME, TestModelDefault.NAMESPACE)
6464
val ctx = model.newTestContext(settings = resolvedSettings)
6565

66-
val op = ctx.generationCtx.model.expectShape(ShapeId.from(shapeId))
67-
return testRender(ctx.requestMembers(op, location)) { members, writer ->
66+
val shape = ctx.generationCtx.model.expectShape(ShapeId.from(shapeId))
67+
return testRender(ctx.shapeMembers(shape, location)) { members, writer ->
6868
SerializeStructGenerator(
6969
ctx.generationCtx,
7070
members,
@@ -121,15 +121,30 @@ fun TestContext.responseMembers(shape: Shape, location: HttpBinding.Location = H
121121
.map { it.member }
122122
}
123123

124-
/** Retrieves Request Document members for HttpTrait-enabled protocols */
125-
fun TestContext.requestMembers(shape: Shape, location: HttpBinding.Location = HttpBinding.Location.DOCUMENT): List<MemberShape> {
126-
val bindingIndex = HttpBindingIndex.of(this.generationCtx.model)
127-
val responseBindings = bindingIndex.getRequestBindings(shape)
128-
129-
return responseBindings.values
130-
.filter { it.location == location }
131-
.sortedBy { it.memberName }
132-
.map { it.member }
124+
/**
125+
* If the shape is an operation:
126+
* Attempts to retrieve request document members for HttpTrait-enabled protocols.
127+
*
128+
* Otherwise:
129+
* Retrieves shape members
130+
*
131+
* TODO: Make this nicer
132+
* */
133+
fun TestContext.shapeMembers(shape: Shape, location: HttpBinding.Location = HttpBinding.Location.DOCUMENT): List<MemberShape> {
134+
when (shape) {
135+
is OperationShape -> {
136+
val bindingIndex = HttpBindingIndex.of(this.generationCtx.model)
137+
val responseBindings = bindingIndex.getRequestBindings(shape)
138+
139+
return responseBindings.values
140+
.filter { it.location == location }
141+
.sortedBy { it.memberName }
142+
.map { it.member }
143+
}
144+
else -> {
145+
return shape.members().toList()
146+
}
147+
}
133148
}
134149

135150
fun TestContext.toGenerationContext(): GenerationContext =

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/SerializeStructGeneratorTest.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,4 +2027,52 @@ class SerializeStructGeneratorTest {
20272027

20282028
actual.shouldContainOnlyOnceWithDiff(expected)
20292029
}
2030+
2031+
@Test
2032+
fun `it serializes an un-nested idempotency token`() {
2033+
val model = (
2034+
modelPrefix + """
2035+
structure FooRequest {
2036+
@idempotencyToken
2037+
bar: String
2038+
}
2039+
"""
2040+
).toSmithyModel()
2041+
2042+
val expected = """
2043+
serializer.serializeStruct(OBJ_DESCRIPTOR) {
2044+
input.bar?.let { field(BAR_DESCRIPTOR, it) } ?: field(BAR_DESCRIPTOR, context.idempotencyTokenProvider.generateToken())
2045+
}
2046+
""".trimIndent()
2047+
2048+
val actual = codegenSerializerForShape(model, "com.test#Foo").stripCodegenPrefix()
2049+
2050+
actual.shouldContainOnlyOnceWithDiff(expected)
2051+
}
2052+
2053+
@Test
2054+
fun `it serializes a nested idempotency token`() {
2055+
val model = (
2056+
modelPrefix + """
2057+
structure FooRequest {
2058+
bar: Bar
2059+
}
2060+
2061+
structure Bar {
2062+
@idempotencyToken
2063+
baz: String
2064+
}
2065+
"""
2066+
).toSmithyModel()
2067+
2068+
val expected = """
2069+
serializer.serializeStruct(OBJ_DESCRIPTOR) {
2070+
input.baz?.let { field(BAZ_DESCRIPTOR, it) } ?: field(BAZ_DESCRIPTOR, context.idempotencyTokenProvider.generateToken())
2071+
}
2072+
""".trimIndent()
2073+
2074+
val actual = codegenSerializerForShape(model, "com.test#Bar").stripCodegenPrefix()
2075+
2076+
actual.shouldContainOnlyOnceWithDiff(expected)
2077+
}
20302078
}

0 commit comments

Comments
 (0)