Skip to content

Commit

Permalink
reverse value map test
Browse files Browse the repository at this point in the history
  • Loading branch information
johnny-schmidt committed Sep 16, 2024
1 parent 4cfe34c commit 52a7b4f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ class AirbyteTypeToJsonSchemaTest {
.add(cdObj)
.add(ofType("null", false))
Assertions.assertEquals(combinedDenormed, propsOut.get("combined_denormalized"))

val unionArrayOut = JsonNodeFactory.instance.objectNode()
unionArrayOut
.put("type", "array")
.putObject("items")
.putArray("oneOf")
.add(ofType("string", false))
.add(ofType("integer", false))
Assertions.assertEquals(ofNullable(unionArrayOut), propsOut.get("union_array"))

val timeTypeFieldNames =
listOf("time", "timestamp", "time_without_timezone", "timestamp_without_timezone")
timeTypeFieldNames.forEach { fieldName ->
val expected = props.get(fieldName) as ObjectNode
if (listOf("date-time", "time").contains(expected.get("format").asText())) {
val formatName = expected.get("format").asText().replace("date-time", "timestamp")
if (!expected.has("airbyte_type")) {
expected.put("airbyte_type", "${formatName}_with_timezone")
}
}
Assertions.assertEquals(ofNullable(expected), propsOut.get(fieldName))
}
}

private fun ofType(type: String, nullable: Boolean = true): ObjectNode =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2024 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.cdk.data

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class AirbyteValueToJsonTest {
@Test
fun testRoundTrip() {
val airbyteValue =
ObjectValue(
linkedMapOf(
"name" to StringValue("hello"),
"age" to IntegerValue(42),
"is_cool" to BooleanValue(true),
"height" to NumberValue("42.0".toBigDecimal()),
"friends" to ArrayValue(listOf(StringValue("hello"), StringValue("world"))),
"address" to
ObjectValue(
linkedMapOf(
"street" to StringValue("123 Main St"),
"city" to StringValue("San Francisco")
)
),
"null_field" to NullValue,
"nullable_union" to IntegerValue(42),
"nonnullable_union" to StringValue("hello"),
"combined_null" to StringValue("hello"),
"combined_denormalized" to
ObjectValue(linkedMapOf("name" to StringValue("hello"))),
"union_array" to ArrayValue(listOf(StringValue("hello"), IntegerValue(42))),
"date" to DateValue("2021-01-01"),
"time" to TimeValue("12:00:00"),
"timestamp" to TimestampValue("2021-01-01T12:00:00Z"),
"time_without_timezone" to TimeValue("12:00:00"),
"timestamp_without_timezone" to TimestampValue("2021-01-01T12:00:00")
)
)
val schema =
ObjectType(
linkedMapOf(
"name" to FieldType(StringType, true),
"age" to FieldType(IntegerType, false),
"is_cool" to FieldType(BooleanType, false),
"height" to FieldType(NumberType, false),
"friends" to FieldType(ArrayType(FieldType(StringType, true)), false),
"address" to
FieldType(
ObjectType(
linkedMapOf(
"street" to FieldType(StringType, true),
"city" to FieldType(StringType, true)
)
),
false
),
"null_field" to FieldType(NullType, false),
"nullable_union" to
FieldType(UnionType(listOf(StringType, IntegerType, NullType)), false),
"nonnullable_union" to
FieldType(UnionType(listOf(StringType, IntegerType)), true),
"combined_null" to FieldType(UnionType(listOf(StringType, NullType)), false),
"combined_denormalized" to
FieldType(
ObjectType(linkedMapOf("name" to FieldType(StringType, true))),
false
),
"union_array" to
FieldType(
ArrayType(FieldType(UnionType(listOf(StringType, IntegerType)), true)),
true
),
"date" to FieldType(DateType, false),
"time" to FieldType(TimeType(false), false),
"timestamp" to FieldType(TimestampType(false), false),
"time_without_timezone" to FieldType(TimeType(true), false),
"timestamp_without_timezone" to FieldType(TimestampType(true), false)
)
)
val jsonValue = AirbyteValueToJson().convert(airbyteValue)
val roundTripValue = JsonToAirbyteValue().convert(jsonValue, schema)

Assertions.assertEquals(airbyteValue, roundTripValue)
}
}

0 comments on commit 52a7b4f

Please sign in to comment.