-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from Chuckame/test/generalize-tests
Generalize encoding/decoding tests (#168)
- Loading branch information
Showing
59 changed files
with
1,136 additions
and
2,107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/kotlin/com/github/avrokotlin/avro4k/RecordBuilderForTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.avrokotlin.avro4k | ||
|
||
import org.apache.avro.Schema | ||
import org.apache.avro.generic.GenericData | ||
import org.apache.avro.generic.GenericRecord | ||
|
||
data class RecordBuilderForTest( | ||
val fields: List<Any?> | ||
) { | ||
fun createRecord(schema: Schema): GenericRecord { | ||
val record = GenericData.Record(schema) | ||
fields.forEachIndexed { index, value -> | ||
val fieldSchema = schema.fields[index].schema() | ||
record.put(index, convertValue(value, fieldSchema)) | ||
} | ||
return record | ||
} | ||
|
||
private fun convertValue( | ||
value: Any?, | ||
schema: Schema | ||
): Any? { | ||
return when (value) { | ||
is RecordBuilderForTest -> value.createRecord(schema) | ||
is Map<*, *> -> createMap(schema, value) | ||
is List<*> -> createList(schema, value) | ||
else -> value | ||
} | ||
} | ||
|
||
fun createList(schema: Schema, value: List<*>): List<*> { | ||
val valueSchema = schema.elementType | ||
return value.map { convertValue(it, valueSchema) } | ||
} | ||
|
||
fun <K, V> createMap(schema: Schema, value: Map<K, V>): Map<K, *> { | ||
val valueSchema = schema.valueType | ||
return value.mapValues { convertValue(it.value, valueSchema) } | ||
} | ||
} | ||
|
||
fun record(vararg fields: Any?): RecordBuilderForTest { | ||
return RecordBuilderForTest(listOf(*fields)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.