|
| 1 | +// +build unit |
| 2 | + |
| 3 | +package postgres |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "database/sql" |
| 8 | + "testing" |
| 9 | + |
| 10 | + sqlmock "github.com/DATA-DOG/go-sqlmock" |
| 11 | + "github.com/hellofresh/goengine" |
| 12 | + driverSQL "github.com/hellofresh/goengine/driver/sql" |
| 13 | + "github.com/hellofresh/goengine/driver/sql/internal/test" |
| 14 | + "github.com/pkg/errors" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestAggregateProjectionStorage_PersistState(t *testing.T) { |
| 20 | + mockedProjectionState := "I'm a projection state" |
| 21 | + notification := &driverSQL.ProjectionNotification{ |
| 22 | + No: 1, |
| 23 | + AggregateID: "d8490e85-dd22-4c32-9cb0-a29e57949951", |
| 24 | + } |
| 25 | + projectionState := driverSQL.ProjectionState{ |
| 26 | + Position: 5, |
| 27 | + ProjectionState: mockedProjectionState, |
| 28 | + } |
| 29 | + |
| 30 | + test.RunWithMockDB(t, "Persist projection state", func(t *testing.T, db *sql.DB, dbMock sqlmock.Sqlmock) { |
| 31 | + stateEncoderCalls := 0 |
| 32 | + stateEncoder := func(i interface{}) (bytes []byte, e error) { |
| 33 | + assert.Equal(t, mockedProjectionState, i) |
| 34 | + |
| 35 | + stateEncoderCalls++ |
| 36 | + return []byte(mockedProjectionState), nil |
| 37 | + } |
| 38 | + |
| 39 | + conn, err := db.Conn(context.Background()) |
| 40 | + require.NoError(t, err) |
| 41 | + |
| 42 | + storage, err := newAggregateProjectionStorage("store_table", "projection_table", stateEncoder, goengine.NopLogger) |
| 43 | + require.NoError(t, err) |
| 44 | + |
| 45 | + dbMock.ExpectExec("^UPDATE \"projection_table\" SET"). |
| 46 | + WithArgs(notification.AggregateID, 5, []byte(mockedProjectionState)). |
| 47 | + WillReturnResult(sqlmock.NewResult(18, 1)) |
| 48 | + |
| 49 | + err = storage.PersistState(conn, notification, projectionState) |
| 50 | + assert.NoError(t, err) |
| 51 | + assert.Equal(t, 1, stateEncoderCalls) |
| 52 | + }) |
| 53 | + |
| 54 | + test.RunWithMockDB(t, "Persist projection state (using default encoder)", func(t *testing.T, db *sql.DB, dbMock sqlmock.Sqlmock) { |
| 55 | + projectionState := driverSQL.ProjectionState{ |
| 56 | + Position: 6, |
| 57 | + ProjectionState: nil, |
| 58 | + } |
| 59 | + |
| 60 | + conn, err := db.Conn(context.Background()) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + storage, err := newAggregateProjectionStorage("store_table", "projection_table", nil, goengine.NopLogger) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + dbMock.ExpectExec("^UPDATE \"projection_table\" SET"). |
| 67 | + WithArgs(notification.AggregateID, 6, []byte("{}")). |
| 68 | + WillReturnResult(sqlmock.NewResult(18, 1)) |
| 69 | + |
| 70 | + err = storage.PersistState(conn, notification, projectionState) |
| 71 | + assert.NoError(t, err) |
| 72 | + }) |
| 73 | + |
| 74 | + test.RunWithMockDB(t, "Fail when state encoding fails", func(t *testing.T, db *sql.DB, dbMock sqlmock.Sqlmock) { |
| 75 | + stateEncoderErr := errors.New("Failed to encode") |
| 76 | + stateEncoder := func(i interface{}) (bytes []byte, e error) { |
| 77 | + return nil, stateEncoderErr |
| 78 | + } |
| 79 | + |
| 80 | + conn, err := db.Conn(context.Background()) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + storage, err := newAggregateProjectionStorage("store_table", "projection_table", stateEncoder, goengine.NopLogger) |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + err = storage.PersistState(conn, notification, projectionState) |
| 87 | + assert.Equal(t, stateEncoderErr, err) |
| 88 | + }) |
| 89 | +} |
0 commit comments