-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build a recorddiffer that handles strongly-typed records
- Loading branch information
Showing
7 changed files
with
596 additions
and
16 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
121 changes: 121 additions & 0 deletions
121
airbyte-cdk/bulk/core/load/src/test/kotlin/io/airbyte/cdk/test/util/RecordDifferTest.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,121 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.test.util | ||
|
||
import java.time.OffsetDateTime | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
class RecordDifferTest { | ||
@Test | ||
fun testBasicBehavior() { | ||
val differ = | ||
RecordDiffer( | ||
primaryKey = listOf(listOf("id1"), listOf("id2")), | ||
cursor = listOf("updated_at"), | ||
) | ||
|
||
val diff = | ||
differ.diffRecords( | ||
expectedRecords = | ||
listOf( | ||
// Extra expected record | ||
OutputRecord( | ||
extractedAt = 1234, | ||
generationId = 42, | ||
mapOf( | ||
"id1" to 1, | ||
"id2" to 100, | ||
"updated_at" to OffsetDateTime.parse("1970-01-01T00:00:00Z"), | ||
"name" to "alice", | ||
"phone" to "1234" | ||
), | ||
airbyteMeta = null | ||
), | ||
// Matching records | ||
OutputRecord( | ||
extractedAt = 1234, | ||
generationId = 42, | ||
mapOf( | ||
"id1" to 1, | ||
"id2" to 100, | ||
"updated_at" to OffsetDateTime.parse("1970-01-01T00:00:01Z"), | ||
"name" to "bob", | ||
), | ||
airbyteMeta = null | ||
), | ||
// Different records | ||
OutputRecord( | ||
extractedAt = 1234, | ||
generationId = 42, | ||
mapOf( | ||
"id1" to 1, | ||
"id2" to 100, | ||
"updated_at" to OffsetDateTime.parse("1970-01-01T00:00:02Z"), | ||
"name" to "charlie", | ||
"phone" to "1234", | ||
"email" to "[email protected]" | ||
), | ||
airbyteMeta = """{"sync_id": 12}""", | ||
), | ||
), | ||
actualRecords = | ||
listOf( | ||
// Matching records | ||
OutputRecord( | ||
extractedAt = 1234, | ||
generationId = 42, | ||
mapOf( | ||
"id1" to 1, | ||
"id2" to 100, | ||
"updated_at" to OffsetDateTime.parse("1970-01-01T00:00:01Z"), | ||
"name" to "bob", | ||
), | ||
airbyteMeta = null | ||
), | ||
// Different records | ||
OutputRecord( | ||
extractedAt = 1234, | ||
generationId = 41, | ||
mapOf( | ||
"id1" to 1, | ||
"id2" to 100, | ||
"updated_at" to OffsetDateTime.parse("1970-01-01T00:00:02Z"), | ||
"name" to "charlie", | ||
"phone" to "5678", | ||
"address" to "1234 charlie street" | ||
), | ||
airbyteMeta = null | ||
), | ||
// Extra actual record | ||
OutputRecord( | ||
extractedAt = 1234, | ||
generationId = 42, | ||
mapOf( | ||
"id1" to 1, | ||
"id2" to 100, | ||
"updated_at" to OffsetDateTime.parse("1970-01-01T00:00:03Z"), | ||
"name" to "dana", | ||
), | ||
airbyteMeta = null | ||
), | ||
), | ||
) | ||
|
||
Assertions.assertEquals( | ||
""" | ||
Missing record (pk=[IntegerValue(value=1), IntegerValue(value=100)], cursor=TimestampValue(value=1970-01-01T00:00Z)): OutputRecord(rawId=null, extractedAt=1970-01-01T00:00:01.234Z, loadedAt=null, generationId=42, data=ObjectValue(values={id1=IntegerValue(value=1), id2=IntegerValue(value=100), updated_at=TimestampValue(value=1970-01-01T00:00Z), name=StringValue(value=alice), phone=StringValue(value=1234)}), airbyteMeta=null) | ||
Incorrect record (pk=[IntegerValue(value=1), IntegerValue(value=100)], cursor=TimestampValue(value=1970-01-01T00:00:02Z)): | ||
generationId: Expected 42, got 41 | ||
airbyteMeta: Expected {"sync_id":12}, got null | ||
phone: Expected StringValue(value=1234), but was StringValue(value=5678) | ||
email: Expected StringValue([email protected]), but was <unset> | ||
address: Expected <unset>, but was StringValue(value=1234 charlie street) | ||
Unexpected record (pk=[IntegerValue(value=1), IntegerValue(value=100)], cursor=TimestampValue(value=1970-01-01T00:00:03Z)): OutputRecord(rawId=null, extractedAt=1970-01-01T00:00:01.234Z, loadedAt=null, generationId=42, data=ObjectValue(values={id1=IntegerValue(value=1), id2=IntegerValue(value=100), updated_at=TimestampValue(value=1970-01-01T00:00:03Z), name=StringValue(value=dana)}), airbyteMeta=null) | ||
""".trimIndent(), | ||
diff | ||
) | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
airbyte-cdk/bulk/core/load/src/testFixtures/kotlin/io/airbyte/cdk/test/util/OutputRecord.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,63 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.test.util | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.airbyte.cdk.data.ObjectValue | ||
import java.time.Instant | ||
import java.util.UUID | ||
|
||
/** A record that we expect to exist in the destination, whether raw _or_ final. */ | ||
data class OutputRecord( | ||
val rawId: UUID?, | ||
val extractedAt: Instant, | ||
val loadedAt: Instant?, | ||
val generationId: Long?, | ||
/** | ||
* strongly-typed map, e.g. ZonedDateTime for timestamp_with_timezone. this makes destination | ||
* test implementations easier. values can be null, b/c warehouse destinations with a JSON | ||
* column type can be either SQL null, or JSON null, and we want to distinguish between those. | ||
* Destinations _must_ filter out the airbyte_* fields from this map. | ||
*/ | ||
val data: ObjectValue, | ||
val airbyteMeta: JsonNode?, | ||
) { | ||
/** Utility constructor with easier types to write by hand */ | ||
constructor( | ||
rawId: String, | ||
extractedAt: Long, | ||
loadedAt: Long?, | ||
generationId: Long?, | ||
data: Map<String, Any?>, | ||
airbyteMeta: String?, | ||
) : this( | ||
UUID.fromString(rawId), | ||
Instant.ofEpochMilli(extractedAt), | ||
loadedAt?.let { Instant.ofEpochMilli(it) }, | ||
generationId, | ||
ObjectValue.from(data), | ||
airbyteMeta?.let { ObjectMapper().readTree(it) }, | ||
) | ||
|
||
/** | ||
* Utility constructor for "expected records". [rawId] and [loadedAt] are generated by the | ||
* destination at runtime, so we don't have those when writing the test. Just generate arbitrary | ||
* values for them. | ||
*/ | ||
constructor( | ||
extractedAt: Long, | ||
generationId: Long?, | ||
data: Map<String, Any?>, | ||
airbyteMeta: String?, | ||
) : this( | ||
null, | ||
Instant.ofEpochMilli(extractedAt), | ||
loadedAt = null, | ||
generationId, | ||
ObjectValue.from(data), | ||
airbyteMeta?.let { ObjectMapper().readTree(it) }, | ||
) | ||
} |
Oops, something went wrong.