Skip to content

Commit f948095

Browse files
committed
Add non-native Map (non-String key) codec coverage
mapSuite only exercised Map[String, V] (the native AttributeValue.Map path). Any non-String key type (Map[Int, V] here) falls back to a completely different Sequence-of-tuple2 encoding (isNativeMap check), which had zero coverage: encode shape, round-trip, empty map, and the three decode-error branches (non-List value, malformed 2-tuple entry, undecodable key). Coverage: 85.75% -> 86.57%.
1 parent 9562fd2 commit f948095

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

schema-dynamodb/src/test/scala/zio/dynamodb/blocks/schema/DynamoDBCodecDeriverSpec.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ object DynamoDBCodecDeriverSpec extends ZIOSpecDefault {
227227
sequenceSuite,
228228
nativeSetSuite,
229229
mapSuite,
230+
nonNativeMapSuite,
230231
dynamicValueSuite,
231232
jsonAVSuite,
232233
byteSequenceCompatSuite,
@@ -1350,6 +1351,51 @@ object DynamoDBCodecDeriverSpec extends ZIOSpecDefault {
13501351
}
13511352
)
13521353

1354+
// ---- non-native map codec (non-String keys, encoded as List of [k, v]) --
1355+
//
1356+
// isNativeMap requires the key type to be a primitive String; any other key
1357+
// type (Int here) falls back to a Sequence-of-tuple2 encoding instead of a
1358+
// native AttributeValue.Map.
1359+
1360+
private val nonNativeMapSuite = suite("map codec (non-String key, Sequence-of-tuple2 encoding)")(
1361+
test("Map[Int, String] encodes as AttributeValue.List of 2-element [key, value] lists") {
1362+
val codec = codecFor[Map[Int, String]]
1363+
codec.encoder(Map(1 -> "a")) match {
1364+
case AttributeValue.List(entries) =>
1365+
entries.toList match {
1366+
case AttributeValue.List(kv) :: Nil =>
1367+
assertTrue(kv.toList == List(AttributeValue.Number(BigDecimal(1)), AttributeValue.String("a")))
1368+
case _ => assertTrue(false)
1369+
}
1370+
case _ => assertTrue(false)
1371+
}
1372+
},
1373+
test("Map[Int, String] round-trips") {
1374+
assertTrue(roundTrip(Map(1 -> "a", 2 -> "b")) == Right(Map(1 -> "a", 2 -> "b")))
1375+
},
1376+
test("empty Map[Int, String] round-trips") {
1377+
assertTrue(roundTrip(Map.empty[Int, String]) == Right(Map.empty[Int, String]))
1378+
},
1379+
test("decode error for non-List attribute value") {
1380+
val codec = codecFor[Map[Int, String]]
1381+
assertTrue(codec.decoder(AttributeValue.String("nope")).isLeft)
1382+
},
1383+
test("decode error for a malformed entry (not a 2-element list)") {
1384+
val codec = codecFor[Map[Int, String]]
1385+
val malformed = AttributeValue.List(
1386+
List(AttributeValue.List(List(AttributeValue.Number(BigDecimal(1)))))
1387+
)
1388+
assertTrue(codec.decoder(malformed).isLeft)
1389+
},
1390+
test("decode error when the key can't be decoded") {
1391+
val codec = codecFor[Map[Int, String]]
1392+
val malformed = AttributeValue.List(
1393+
List(AttributeValue.List(List(AttributeValue.String("not-a-number"), AttributeValue.String("a"))))
1394+
)
1395+
assertTrue(codec.decoder(malformed).isLeft)
1396+
}
1397+
)
1398+
13531399
// ---- DynamicValue codec ------------------------------------------------
13541400

13551401
private val dvCodec = DynamoDBCodecDeriver.dynamicValueCodec

0 commit comments

Comments
 (0)