From b5136c3561c89c3d4ae8ceaee0ed6b208bbac274 Mon Sep 17 00:00:00 2001 From: Chuckame Date: Tue, 17 Sep 2024 11:08:01 +0200 Subject: [PATCH] chore: Add missing tests --- .../avro4k/encoding/PrimitiveEncodingTest.kt | 29 +++++++++++++ .../avro4k/encoding/RecordEncodingTest.kt | 42 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/PrimitiveEncodingTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/PrimitiveEncodingTest.kt index d98295ca..2b8f7eda 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/PrimitiveEncodingTest.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/PrimitiveEncodingTest.kt @@ -1,6 +1,7 @@ package com.github.avrokotlin.avro4k.encoding import com.github.avrokotlin.avro4k.AvroAssertions +import com.github.avrokotlin.avro4k.SomeEnum import com.github.avrokotlin.avro4k.WrappedBoolean import com.github.avrokotlin.avro4k.WrappedByte import com.github.avrokotlin.avro4k.WrappedChar @@ -10,9 +11,13 @@ import com.github.avrokotlin.avro4k.WrappedInt import com.github.avrokotlin.avro4k.WrappedLong import com.github.avrokotlin.avro4k.WrappedShort import com.github.avrokotlin.avro4k.WrappedString +import com.github.avrokotlin.avro4k.internal.nullable import com.github.avrokotlin.avro4k.record import io.kotest.core.spec.style.StringSpec +import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.Serializable +import kotlinx.serialization.serializer +import org.apache.avro.Schema import java.nio.ByteBuffer internal class PrimitiveEncodingTest : StringSpec({ @@ -31,6 +36,30 @@ internal class PrimitiveEncodingTest : StringSpec({ .isEncodedAs(false) } + @OptIn(InternalSerializationApi::class) + listOf( + true, + false, + 1.toByte(), + 2.toShort(), + 3, + 4L, + 5.0F, + 6.0, + 'A', + SomeEnum.B + ).forEach { + "coerce ${it::class.simpleName} $it to string" { + AvroAssertions.assertThat(it, it::class.serializer()) + .isEncodedAs(it.toString(), writerSchema = Schema.create(Schema.Type.STRING)) + } + + "coerce ${it::class.simpleName} $it to nullable string" { + AvroAssertions.assertThat(it, it::class.serializer()) + .isEncodedAs(it.toString(), writerSchema = Schema.create(Schema.Type.STRING).nullable) + } + } + "read write out bytes" { AvroAssertions.assertThat(ByteTest(3)) .isEncodedAs(record(3)) diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/RecordEncodingTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/RecordEncodingTest.kt index f07816b2..d80241f4 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/RecordEncodingTest.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/encoding/RecordEncodingTest.kt @@ -183,6 +183,48 @@ internal class RecordEncodingTest : StringSpec({ AvroAssertions.assertThat(input) .isDecodedAs(MissingFields(true)) } + "support decoding from a writer schema with missing descriptor fields (just skipping, no reordering)" { + @Serializable + @SerialName("TheClass") + data class TheClass( + val a: String?, + val b: Boolean?, + val c: Int, + ) + + @Serializable + @SerialName("TheClass") + data class TheLightClass( + val b: Boolean?, + ) + + val writerSchema = + SchemaBuilder.record("TheClass").fields() + .name("a").type(Schema.create(Schema.Type.STRING).nullable).withDefault(null) + .name("b").type().booleanType().noDefault() + .name("c").type().intType().intDefault(42) + .endRecord() + + AvroAssertions.assertThat(TheClass("hello", true, 42)) + .isDecodedAs(TheLightClass(true), writerSchema = writerSchema) + } + "support encoding & decoding with additional descriptor optional fields (no reordering)" { + @Serializable + @SerialName("TheClass") + data class TheClass( + val a: String? = null, + val b: Boolean?, + val c: Int = 42, + ) + + val writerSchema = + SchemaBuilder.record("TheClass").fields() + .name("b").type().booleanType().noDefault() + .endRecord() + + AvroAssertions.assertThat(TheClass("hello", true, 17)) + .isEncodedAs(record(true), expectedDecodedValue = TheClass(null, true, 42), writerSchema = writerSchema) + } "should fail when trying to write a data class but missing the last schema field" { @Serializable @SerialName("Base")