Skip to content

Commit

Permalink
[mysql] Add integration test scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Mar 1, 2024
1 parent 6a06abf commit fb3f52a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Integration tests
run-name: Running integration tests
on: [push]
jobs:
container-job:
postgres-integration-test:
runs-on: ubuntu-latest
container: golang:1.22
services:
Expand All @@ -21,5 +21,16 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: 1.22
- name: Run integration test
run: PG_HOST=postgres make itest
- name: Run Postgres integration test
run: PG_HOST=postgres make postgres-itest
mysql-integration-test:
runs-on: ubuntu-latest
container: golang:1.22
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22
- name: Run MySQL integration test
run: MYSQL_HOST=postgres make mysql-itest
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ static:
test:
go test ./...

.PHONY: integration-test
itest:
go run sources/postgres/integration_test/main.go
.PHONY: mysql-itest
mysql-itest:
go run integration_tests/mysql/main.go


.PHONY: postgres-itest
postgres-itest:
go run integration_tests/postgres/main.go


.PHONY: race
race:
Expand Down
14 changes: 14 additions & 0 deletions integration_tests/mysql/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"log/slog"
"os"

"github.com/lmittmann/tint"
)

func main() {
os.Setenv("TZ", "UTC")
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{Level: slog.LevelInfo})))

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"maps"
"math/rand/v2"
"os"
"strings"

"github.com/artie-labs/transfer/lib/cdc/util"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/lmittmann/tint"

"github.com/artie-labs/reader/config"
"github.com/artie-labs/reader/integration_tests/utils"
"github.com/artie-labs/reader/lib"
"github.com/artie-labs/reader/lib/debezium"
"github.com/artie-labs/reader/lib/logger"
Expand Down Expand Up @@ -55,43 +54,6 @@ func main() {
}
}

func getPayload(message lib.RawMessage) util.SchemaEventPayload {
payloadTyped, ok := message.GetPayload().(util.SchemaEventPayload)
if !ok {
panic("payload is not of type util.SchemaEventPayload")
}
return payloadTyped
}

func checkDifference(name, expected, actual string) bool {
if expected == actual {
return false
}
expectedLines := strings.Split(expected, "\n")
actualLines := strings.Split(actual, "\n")
fmt.Printf("Expected %s:\n", name)
fmt.Println("--------------------------------------------------------------------------------")
for i, line := range expectedLines {
prefix := " "
if i >= len(actualLines) || line != actualLines[i] {
prefix = ">"
}
fmt.Println(prefix + line)
}
fmt.Println("--------------------------------------------------------------------------------")
fmt.Printf("Actual %s:\n", name)
fmt.Println("--------------------------------------------------------------------------------")
for i, line := range actualLines {
prefix := " "
if i >= len(expectedLines) || line != expectedLines[i] {
prefix = ">"
}
fmt.Println(prefix + line)
}
fmt.Println("--------------------------------------------------------------------------------")
return true
}

func readTable(db *sql.DB, tableName string, batchSize int) ([]lib.RawMessage, error) {
tableCfg := config.PostgreSQLTable{
Schema: "public",
Expand Down Expand Up @@ -725,12 +687,12 @@ func testTypes(db *sql.DB) error {
return fmt.Errorf("failed to marshal payload")
}

if checkDifference("partition key", `{"pk":1}`, string(keyBytes)) {
if utils.checkDifference("partition key", `{"pk":1}`, string(keyBytes)) {

Check failure on line 690 in integration_tests/postgres/main.go

View workflow job for this annotation

GitHub Actions / Go 1.22

undefined: utils.checkDifference

Check failure on line 690 in integration_tests/postgres/main.go

View workflow job for this annotation

GitHub Actions / postgres-integration-test

undefined: utils.checkDifference
return fmt.Errorf("partition key does not match")
}

expectedPayload := fmt.Sprintf(expectedPayloadTemplate, getPayload(row).Payload.Source.TsMs, tempTableName)
if checkDifference("payload", expectedPayload, string(valueBytes)) {
expectedPayload := fmt.Sprintf(expectedPayloadTemplate, utils.getPayload(row).Payload.Source.TsMs, tempTableName)

Check failure on line 694 in integration_tests/postgres/main.go

View workflow job for this annotation

GitHub Actions / Go 1.22

undefined: utils.getPayload

Check failure on line 694 in integration_tests/postgres/main.go

View workflow job for this annotation

GitHub Actions / postgres-integration-test

undefined: utils.getPayload
if utils.checkDifference("payload", expectedPayload, string(valueBytes)) {

Check failure on line 695 in integration_tests/postgres/main.go

View workflow job for this annotation

GitHub Actions / Go 1.22

undefined: utils.checkDifference (compile)

Check failure on line 695 in integration_tests/postgres/main.go

View workflow job for this annotation

GitHub Actions / postgres-integration-test

undefined: utils.checkDifference
return fmt.Errorf("payload does not match")
}

Expand Down Expand Up @@ -864,7 +826,7 @@ func testScan(db *sql.DB) error {
if !maps.Equal(row.PartitionKey, expectedPartitionKeys[i]) {
return fmt.Errorf("partition keys are different for row %d, batch size %d, %T != %T", i, batchSize, row.PartitionKey, expectedPartitionKeys[i])
}
textValue := getPayload(row).Payload.After["c_text_value"]
textValue := utils.GetPayload(row).Payload.After["c_text_value"]
if textValue != expectedValues[i] {
return fmt.Errorf("row values are different for row %d, batch size %d, %T != %T", i, batchSize, textValue, expectedPartitionKeys[i])
}
Expand Down
46 changes: 46 additions & 0 deletions integration_tests/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package utils

import (
"fmt"
"strings"

"github.com/artie-labs/reader/lib"
"github.com/artie-labs/transfer/lib/cdc/util"
)

func GetPayload(message lib.RawMessage) util.SchemaEventPayload {
payloadTyped, ok := message.GetPayload().(util.SchemaEventPayload)
if !ok {
panic("payload is not of type util.SchemaEventPayload")
}
return payloadTyped
}

func CheckDifference(name, expected, actual string) bool {
if expected == actual {
return false
}
expectedLines := strings.Split(expected, "\n")
actualLines := strings.Split(actual, "\n")
fmt.Printf("Expected %s:\n", name)
fmt.Println("--------------------------------------------------------------------------------")
for i, line := range expectedLines {
prefix := " "
if i >= len(actualLines) || line != actualLines[i] {
prefix = ">"
}
fmt.Println(prefix + line)
}
fmt.Println("--------------------------------------------------------------------------------")
fmt.Printf("Actual %s:\n", name)
fmt.Println("--------------------------------------------------------------------------------")
for i, line := range actualLines {
prefix := " "
if i >= len(expectedLines) || line != expectedLines[i] {
prefix = ">"
}
fmt.Println(prefix + line)
}
fmt.Println("--------------------------------------------------------------------------------")
return true
}

0 comments on commit fb3f52a

Please sign in to comment.