Skip to content

Commit

Permalink
[tests] Check against expected error messages (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Apr 10, 2024
1 parent 6a991da commit bd9e3c9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 87 deletions.
39 changes: 15 additions & 24 deletions lib/dynamo/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,33 @@ import (
)

func TestNewMessage(t *testing.T) {
type _tc struct {
tcs := []struct {
name string
record *dynamodbstreams.Record
tableName string

expectErr bool
expectedErr string
expectedMessage *Message
}

tcs := []_tc{
}{
{
name: "nil record",
record: nil,
tableName: "testTable",
expectErr: true,
expectedMessage: nil,
name: "nil record",
record: nil,
tableName: "testTable",
expectedErr: "record is nil or dynamodb does not exist in this event payload",
},
{
name: "nil dynamodb",
record: &dynamodbstreams.Record{},
tableName: "testTable",
expectErr: true,
expectedMessage: nil,
name: "nil dynamodb",
record: &dynamodbstreams.Record{},
tableName: "testTable",
expectedErr: "record is nil or dynamodb does not exist in this event payload",
},
{
name: "empty keys",
record: &dynamodbstreams.Record{
Dynamodb: &dynamodbstreams.StreamRecord{},
},
tableName: "testTable",
expectErr: true,
expectedMessage: nil,
tableName: "testTable",
expectedErr: "keys is nil",
},
{
name: "EventName INSERT",
Expand All @@ -59,7 +54,6 @@ func TestNewMessage(t *testing.T) {
EventName: ptr.ToString("INSERT"),
},
tableName: "testTable",
expectErr: false,
expectedMessage: &Message{
op: "c",
tableName: "testTable",
Expand All @@ -84,7 +78,6 @@ func TestNewMessage(t *testing.T) {
EventName: ptr.ToString("MODIFY"),
},
tableName: "testTable",
expectErr: false,
expectedMessage: &Message{
op: "u",
executionTime: time.Date(2023, 8, 28, 0, 0, 0, 0, time.UTC),
Expand All @@ -109,7 +102,6 @@ func TestNewMessage(t *testing.T) {
EventName: aws.String("REMOVE"),
},
tableName: "testTable",
expectErr: false,
expectedMessage: &Message{
op: "d",
tableName: "testTable",
Expand All @@ -132,7 +124,6 @@ func TestNewMessage(t *testing.T) {
EventName: aws.String("INSERT"),
},
tableName: "testTable",
expectErr: false,
expectedMessage: &Message{
op: "c",
tableName: "testTable",
Expand All @@ -145,8 +136,8 @@ func TestNewMessage(t *testing.T) {

for _, tc := range tcs {
actualMessage, actualErr := NewMessage(tc.record, tc.tableName)
if tc.expectErr {
assert.Error(t, actualErr, tc.name)
if tc.expectedErr != "" {
assert.ErrorContains(t, actualErr, tc.expectedErr, tc.name)
} else {
assert.NoError(t, actualErr, tc.name)
assert.Equal(t, tc.expectedMessage, actualMessage, tc.name)
Expand Down
32 changes: 15 additions & 17 deletions lib/dynamo/parse_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ import (
)

func Test_NewMessageFromExport(t *testing.T) {
type _tc struct {
name string
item dynamodb.ItemResponse
keys []string
tableName string
expectedError string
}

tcs := []_tc{
tcs := []struct {
name string
item dynamodb.ItemResponse
keys []string
tableName string
expectedErr string
}{
{
name: "Test with empty item",
item: dynamodb.ItemResponse{
Item: map[string]*dynamodb.AttributeValue{},
},
keys: []string{"id"},
tableName: "test",
expectedError: "item is nil or keys do not exist in this item payload",
keys: []string{"id"},
tableName: "test",
expectedErr: "item is nil or keys do not exist in this item payload",
},
{
name: "Test with empty keys",
Expand All @@ -36,9 +34,9 @@ func Test_NewMessageFromExport(t *testing.T) {
},
},
},
keys: []string{},
tableName: "test",
expectedError: "keys is nil",
keys: []string{},
tableName: "test",
expectedErr: "keys is nil",
},
{
name: "Test with valid item and keys",
Expand All @@ -56,8 +54,8 @@ func Test_NewMessageFromExport(t *testing.T) {

for _, tc := range tcs {
msg, err := NewMessageFromExport(tc.item, tc.keys, tc.tableName)
if tc.expectedError != "" {
assert.Equal(t, tc.expectedError, err.Error(), tc.name)
if tc.expectedErr != "" {
assert.Equal(t, tc.expectedErr, err.Error(), tc.name)
} else {
assert.NoError(t, err, tc.name)
assert.Equal(t, tc.tableName, msg.tableName, tc.name)
Expand Down
20 changes: 9 additions & 11 deletions lib/postgres/parse/geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
)

func TestToPoint(t *testing.T) {
type _tc struct {
tcs := []struct {
name string
input string
output *Point
expectError bool
}

tcs := []_tc{
expectedErr string
}{
{
name: "Valid point",
input: "(2.2945,48.8584)",
Expand All @@ -23,29 +21,29 @@ func TestToPoint(t *testing.T) {
{
name: "Invalid format",
input: "2.2945,48.8584",
expectError: true,
expectedErr: "invalid point format",
},
{
name: "Invalid X coordinate",
input: "(abc,48.8584)",
expectError: true,
expectedErr: "invalid X coordinate",
},
{
name: "Invalid Y coordinate",
input: "(2.2945,xyz)",
expectError: true,
expectedErr: "invalid Y coordinate:",
},
{
name: "Empty input",
input: "",
expectError: true,
expectedErr: "invalid point format",
},
}

for _, tc := range tcs {
point, err := ToPoint(tc.input)
if tc.expectError {
assert.Error(t, err, tc.name)
if tc.expectedErr != "" {
assert.ErrorContains(t, err, tc.expectedErr, tc.name)
} else {
assert.Equal(t, *tc.output, *point, tc.name)
}
Expand Down
15 changes: 6 additions & 9 deletions lib/rdbms/primary_key/primary_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ func TestNewKeys(t *testing.T) {
}

func TestPrimaryKeys_LoadValues(t *testing.T) {
type _tc struct {
testCases := []struct {
name string
startingValues []any
endingValues []any

keys []Key
expectedKeys []Key
expectedErr bool
}

testCases := []_tc{
expectedErr string
}{
{
name: "happy path (starting values)",
keys: []Key{{Name: "id"}},
Expand Down Expand Up @@ -66,13 +64,12 @@ func TestPrimaryKeys_LoadValues(t *testing.T) {
name: "bad data - no keys",
keys: []Key{},
startingValues: []any{"123", "path:123"},
expectedErr: true,
expectedErr: "keys (0), and passed in values (2) length does not match, keys: [], values: [123 path:123]",
},
{
name: "bad data - no values, so we just don't load",
keys: []Key{{Name: "id"}, {Name: "content_key"}},
startingValues: []any{},
expectedErr: false,
expectedKeys: []Key{{Name: "id"}, {Name: "content_key"}},
},
}
Expand All @@ -81,8 +78,8 @@ func TestPrimaryKeys_LoadValues(t *testing.T) {
pk := NewKeys(testCase.keys)

err := pk.LoadValues(testCase.startingValues, testCase.endingValues)
if testCase.expectedErr {
assert.Error(t, err, testCase.name)
if testCase.expectedErr != "" {
assert.ErrorContains(t, err, testCase.expectedErr, testCase.name)
} else {
assert.NoError(t, err, testCase.name)
assert.Equal(t, testCase.expectedKeys, pk.Keys(), testCase.name)
Expand Down
14 changes: 6 additions & 8 deletions lib/s3lib/s3lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import (
)

func TestBucketAndPrefixFromFilePath(t *testing.T) {
type _tc struct {
tcs := []struct {
name string
fp string
expectedBucket *string
expectedPrefix *string
expectedErr bool
}

tcs := []_tc{
expectedErr string
}{
{
name: "valid path (w/ S3 prefix)",
fp: "s3://bucket/prefix",
Expand Down Expand Up @@ -57,14 +55,14 @@ func TestBucketAndPrefixFromFilePath(t *testing.T) {
{
name: "invalid path",
fp: "s3://bucket",
expectedErr: true,
expectedErr: "invalid S3 path, missing prefix",
},
}

for _, tc := range tcs {
actualBucket, actualPrefix, actualErr := bucketAndPrefixFromFilePath(tc.fp)
if tc.expectedErr {
assert.Error(t, actualErr, tc.name)
if tc.expectedErr != "" {
assert.ErrorContains(t, actualErr, tc.expectedErr, tc.name)
} else {
assert.NoError(t, actualErr, tc.name)

Expand Down
29 changes: 11 additions & 18 deletions sources/postgres/adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,13 @@ func TestValueConverterForType_ToField(t *testing.T) {
}

func TestValueConverterForType_Convert(t *testing.T) {
type _tc struct {
tcs := []struct {
name string
col schema.Column
value any
numericValue bool
expectedValue any
expectErr bool
}

tcs := []_tc{
}{
{
name: "date (postgres.Date)",
col: schema.Column{Name: "date_col", Type: schema.Date},
Expand Down Expand Up @@ -255,20 +252,16 @@ func TestValueConverterForType_Convert(t *testing.T) {
assert.NoError(t, err, tc.name)

actualValue, actualErr := converter.Convert(tc.value)
if tc.expectErr {
assert.Error(t, actualErr, tc.name)
assert.NoError(t, actualErr, tc.name)
if tc.numericValue {
bytes, ok := actualValue.([]byte)
assert.True(t, ok)
field := converter.ToField(tc.col.Name)
val, err := field.DecodeDecimal(bytes)
assert.NoError(t, err, tc.name)
assert.Equal(t, tc.expectedValue, val.String(), tc.name)
} else {
assert.NoError(t, actualErr, tc.name)
if tc.numericValue {
bytes, ok := actualValue.([]byte)
assert.True(t, ok)
field := converter.ToField(tc.col.Name)
val, err := field.DecodeDecimal(bytes)
assert.NoError(t, err, tc.name)
assert.Equal(t, tc.expectedValue, val.String(), tc.name)
} else {
assert.Equal(t, tc.expectedValue, actualValue, tc.name)
}
assert.Equal(t, tc.expectedValue, actualValue, tc.name)
}
}
}

0 comments on commit bd9e3c9

Please sign in to comment.