Skip to content

Commit

Permalink
chore: Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuckame committed Sep 17, 2024
1 parent ef9d446 commit 8dc5164
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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({
Expand All @@ -31,6 +36,30 @@ internal class PrimitiveEncodingTest : StringSpec({
.isEncodedAs(false)
}

@OptIn(InternalSerializationApi::class)
listOf<Any>(
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8dc5164

Please sign in to comment.