Skip to content

Commit

Permalink
Removing fake event (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Jul 30, 2024
1 parent c01d76d commit decb25c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
2 changes: 2 additions & 0 deletions lib/mocks/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ package mocks
//counterfeiter:generate -o=dwh.mock.go ../destination DataWarehouse
//counterfeiter:generate -o=baseline.mock.go ../destination Baseline
//counterfeiter:generate -o=tableid.mock.go ../sql TableIdentifier

//counterfeiter:generate -o=event.mock.go ../cdc Event
52 changes: 8 additions & 44 deletions models/event/event_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
package event

import (
"time"

"github.com/stretchr/testify/assert"

"github.com/artie-labs/transfer/lib/config"
"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/kafkalib"
"github.com/artie-labs/transfer/lib/typing"
"github.com/artie-labs/transfer/lib/typing/columns"
)

type fakeEvent struct{}

var idMap = map[string]any{
"id": 123,
}

func (f fakeEvent) Operation() string {
return "r"
}

func (f fakeEvent) DeletePayload() bool {
return false
}

func (f fakeEvent) GetExecutionTime() time.Time {
return time.Now()
}

func (f fakeEvent) GetTableName() string {
return "foo"
}

func (f fakeEvent) GetOptionalSchema() map[string]typing.KindDetails {
return nil
}

func (f fakeEvent) GetColumns() (*columns.Columns, error) {
return &columns.Columns{}, nil
}

func (f fakeEvent) GetData(pkMap map[string]any, config *kafkalib.TopicConfig) (map[string]any, error) {
return map[string]any{constants.DeleteColumnMarker: false, constants.OnlySetDeleteColumnMarker: false}, nil
}

func (e *EventsTestSuite) TestEvent_IsValid() {
{
_evt := Event{
Expand Down Expand Up @@ -92,36 +58,34 @@ func (e *EventsTestSuite) TestEvent_IsValid() {
}

func (e *EventsTestSuite) TestEvent_TableName() {
var f fakeEvent
{
// Don't pass in tableName.
evt, err := ToMemoryEvent(f, idMap, &kafkalib.TopicConfig{}, config.Replication)
evt, err := ToMemoryEvent(e.fakeEvent, idMap, &kafkalib.TopicConfig{}, config.Replication)
assert.NoError(e.T(), err)
assert.Equal(e.T(), f.GetTableName(), evt.Table)
assert.Equal(e.T(), e.fakeEvent.GetTableName(), evt.Table)
}
{
// Now pass it in, it should override.
evt, err := ToMemoryEvent(f, idMap, &kafkalib.TopicConfig{TableName: "orders"}, config.Replication)
evt, err := ToMemoryEvent(e.fakeEvent, idMap, &kafkalib.TopicConfig{TableName: "orders"}, config.Replication)
assert.NoError(e.T(), err)
assert.Equal(e.T(), "orders", evt.Table)
}
{
// Now, if it's history mode...
evt, err := ToMemoryEvent(f, idMap, &kafkalib.TopicConfig{TableName: "orders"}, config.History)
evt, err := ToMemoryEvent(e.fakeEvent, idMap, &kafkalib.TopicConfig{TableName: "orders"}, config.History)
assert.NoError(e.T(), err)
assert.Equal(e.T(), "orders__history", evt.Table)

// Table already has history suffix, so it won't add extra.
evt, err = ToMemoryEvent(f, idMap, &kafkalib.TopicConfig{TableName: "dusty__history"}, config.History)
evt, err = ToMemoryEvent(e.fakeEvent, idMap, &kafkalib.TopicConfig{TableName: "dusty__history"}, config.History)
assert.NoError(e.T(), err)
assert.Equal(e.T(), "dusty__history", evt.Table)
}
}

func (e *EventsTestSuite) TestEvent_Columns() {
var f fakeEvent
{
evt, err := ToMemoryEvent(f, map[string]any{"id": 123}, &kafkalib.TopicConfig{}, config.Replication)
evt, err := ToMemoryEvent(e.fakeEvent, map[string]any{"id": 123}, &kafkalib.TopicConfig{}, config.Replication)
assert.NoError(e.T(), err)

assert.Equal(e.T(), 1, len(evt.Columns.GetColumns()))
Expand All @@ -130,7 +94,7 @@ func (e *EventsTestSuite) TestEvent_Columns() {
}
{
// Now it should handle escaping column names
evt, err := ToMemoryEvent(f, map[string]any{"id": 123, "CAPITAL": "foo"}, &kafkalib.TopicConfig{}, config.Replication)
evt, err := ToMemoryEvent(e.fakeEvent, map[string]any{"id": 123, "CAPITAL": "foo"}, &kafkalib.TopicConfig{}, config.Replication)
assert.NoError(e.T(), err)

assert.Equal(e.T(), 2, len(evt.Columns.GetColumns()))
Expand All @@ -142,7 +106,7 @@ func (e *EventsTestSuite) TestEvent_Columns() {
}
{
// In history mode, the deletion column markers should be removed from the event data
evt, err := ToMemoryEvent(f, map[string]any{"id": 123}, &kafkalib.TopicConfig{}, config.History)
evt, err := ToMemoryEvent(e.fakeEvent, map[string]any{"id": 123}, &kafkalib.TopicConfig{}, config.History)
assert.NoError(e.T(), err)

_, isOk := evt.Data[constants.DeleteColumnMarker]
Expand Down
17 changes: 15 additions & 2 deletions models/event/events_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package event
import (
"testing"

"github.com/artie-labs/transfer/lib/typing/columns"

"github.com/artie-labs/transfer/lib/config/constants"

"github.com/artie-labs/transfer/lib/mocks"

"github.com/artie-labs/transfer/models"

"github.com/artie-labs/transfer/lib/config"
Expand All @@ -12,8 +18,9 @@ import (

type EventsTestSuite struct {
suite.Suite
cfg config.Config
db *models.DatabaseData
cfg config.Config
db *models.DatabaseData
fakeEvent *mocks.FakeEvent
}

func (e *EventsTestSuite) SetupTest() {
Expand All @@ -23,6 +30,12 @@ func (e *EventsTestSuite) SetupTest() {
BufferRows: 1000,
}
e.db = models.NewMemoryDB()

fakeEvent := &mocks.FakeEvent{}
fakeEvent.GetDataReturns(map[string]any{constants.DeleteColumnMarker: false, constants.OnlySetDeleteColumnMarker: false}, nil)
fakeEvent.GetColumnsReturns(&columns.Columns{}, nil)
fakeEvent.GetTableNameReturns("foo")
e.fakeEvent = fakeEvent
}

func TestEventsTestSuite(t *testing.T) {
Expand Down

0 comments on commit decb25c

Please sign in to comment.