Skip to content

Commit

Permalink
[tests] Check against expected error messages (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Apr 10, 2024
1 parent e72500b commit bcb52be
Show file tree
Hide file tree
Showing 28 changed files with 201 additions and 242 deletions.
4 changes: 2 additions & 2 deletions clients/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func (b *BigQueryTestSuite) TestTableRelName() {
{
// All the possible errors
_, err := tableRelName("project.dataset")
assert.Error(b.T(), err)
assert.ErrorContains(b.T(), err, "invalid fully qualified name: project.dataset")

_, err = tableRelName("project")
assert.Error(b.T(), err)
assert.ErrorContains(b.T(), err, "invalid fully qualified name: project")
}
}
6 changes: 0 additions & 6 deletions clients/bigquery/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func (b *BigQueryTestSuite) TestBackfillColumn() {
type _testCase struct {
name string
col columns.Column
expectErr bool
backfillSQL string
commentSQL string
}
Expand Down Expand Up @@ -62,11 +61,6 @@ func (b *BigQueryTestSuite) TestBackfillColumn() {
var index int
for _, testCase := range testCases {
err := shared.BackfillColumn(config.Config{}, b.store, testCase.col, fqTableName)
if testCase.expectErr {
assert.Error(b.T(), err, testCase.name)
continue
}

assert.NoError(b.T(), err, testCase.name)
if testCase.backfillSQL != "" && testCase.commentSQL != "" {
backfillSQL, _ := b.fakeStore.ExecArgsForCall(index)
Expand Down
24 changes: 11 additions & 13 deletions clients/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ import (
)

func TestObjectPrefix(t *testing.T) {
type _testCase struct {
name string
tableData *optimization.TableData
config *config.S3Settings

expectError bool
expectedFormat string
}

td := optimization.NewTableData(nil, config.Replication, nil, kafkalib.TopicConfig{
Database: "db",
TableName: "table",
Schema: "public",
}, "table")

td.LatestCDCTs = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
testCases := []_testCase{
testCases := []struct {
name string
tableData *optimization.TableData
config *config.S3Settings

expectedErr string
expectedFormat string
}{
{
name: "nil",
expectError: true,
expectedErr: "failed to validate settings: s3 settings are nil",
},
{
name: "valid #1 (no prefix)",
Expand Down Expand Up @@ -62,8 +60,8 @@ func TestObjectPrefix(t *testing.T) {

for _, tc := range testCases {
store, err := LoadStore(config.Config{S3: tc.config})
if tc.expectError {
assert.Error(t, err, tc.name)
if tc.expectedErr != "" {
assert.ErrorContains(t, err, tc.expectedErr, tc.name)
} else {
assert.NoError(t, err, tc.name)
actualObjectPrefix := store.ObjectPrefix(tc.tableData)
Expand Down
19 changes: 6 additions & 13 deletions clients/snowflake/staging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ func (s *SnowflakeTestSuite) TestCastColValStaging() {

func (s *SnowflakeTestSuite) TestBackfillColumn() {
fqTableName := "db.public.tableName"
type _testCase struct {
name string
col columns.Column
expectErr bool
backfillSQL string
commentSQL string
}

backfilledCol := columns.NewColumn("foo", typing.Boolean)
backfilledCol.SetDefaultValue(true)
Expand All @@ -75,7 +68,12 @@ func (s *SnowflakeTestSuite) TestBackfillColumn() {
needsBackfillColDefault := columns.NewColumn("default", typing.Boolean)
needsBackfillColDefault.SetDefaultValue(true)

testCases := []_testCase{
testCases := []struct {
name string
col columns.Column
backfillSQL string
commentSQL string
}{
{
name: "col that doesn't have default val",
col: columns.NewColumn("foo", typing.Invalid),
Expand All @@ -101,11 +99,6 @@ func (s *SnowflakeTestSuite) TestBackfillColumn() {
var count int
for _, testCase := range testCases {
err := shared.BackfillColumn(config.Config{}, s.stageStore, testCase.col, fqTableName)
if testCase.expectErr {
assert.Error(s.T(), err, testCase.name)
continue
}

assert.NoError(s.T(), err, testCase.name)
if testCase.backfillSQL != "" && testCase.commentSQL != "" {
backfillSQL, _ := s.fakeStageStore.ExecArgsForCall(count)
Expand Down
2 changes: 1 addition & 1 deletion lib/cdc/mongo/debezium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (p *MongoTestSuite) TestMongoDBEventCustomerBefore() {

func (p *MongoTestSuite) TestGetEventFromBytesTombstone() {
_, err := p.Debezium.GetEventFromBytes(typing.Settings{}, nil)
assert.Error(p.T(), err)
assert.ErrorContains(p.T(), err, "empty message")
}

func (p *MongoTestSuite) TestMongoDBEventWithSchema() {
Expand Down
2 changes: 1 addition & 1 deletion lib/cdc/mysql/debezium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func (m *MySQLTestSuite) TestGetEventFromBytesTombstone() {
_, err := m.GetEventFromBytes(typing.Settings{}, nil)
assert.Error(m.T(), err)
assert.ErrorContains(m.T(), err, "empty message")
}

func (m *MySQLTestSuite) TestGetEventFromBytes() {
Expand Down
2 changes: 1 addition & 1 deletion lib/cdc/postgres/debezium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var validTc = &kafkalib.TopicConfig{

func (p *PostgresTestSuite) TestGetEventFromBytesTombstone() {
_, err := p.GetEventFromBytes(typing.Settings{}, nil)
assert.Error(p.T(), err)
assert.ErrorContains(p.T(), err, "empty message")
}

func (p *PostgresTestSuite) TestGetPrimaryKey() {
Expand Down
2 changes: 1 addition & 1 deletion lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *S3Settings) Validate() error {
}

if !constants.IsValidS3OutputFormat(s.OutputFormat) {
return fmt.Errorf("invalid s3 output format, format: %v", s.OutputFormat)
return fmt.Errorf("invalid s3 output format %q", s.OutputFormat)
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions lib/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ outputSource: none
assert.Nil(t, err)

validErr := config.Validate()
assert.Error(t, validErr)
assert.ErrorContains(t, validErr, "invalid destination")
}

Expand Down Expand Up @@ -318,7 +317,7 @@ func TestReadFileNotYAML(t *testing.T) {

config, err := readFileToConfig(randomFile)
assert.Nil(t, config)
assert.Error(t, err, "failed to read config file, because it's not proper yaml.")
assert.ErrorContains(t, err, "yaml: unmarshal errors", "failed to read config file, because it's not proper yaml.")
}

func TestReadFileToConfig_Snowflake(t *testing.T) {
Expand Down Expand Up @@ -544,7 +543,7 @@ func TestConfig_Validate(t *testing.T) {

// Now that we have a valid output, let's test with S3.
cfg.Output = constants.S3
assert.Error(t, cfg.Validate())
assert.ErrorContains(t, cfg.Validate(), "s3 settings are nil")
cfg.S3 = &S3Settings{
Bucket: "foo",
AwsSecretAccessKey: "foo",
Expand Down
106 changes: 51 additions & 55 deletions lib/config/config_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,56 @@ import (
)

func TestS3Settings_Validate(t *testing.T) {
type _testCase struct {
Name string
S3 *S3Settings
ExpectErr bool
}

testCases := []_testCase{
testCases := []struct {
name string
s3 *S3Settings
expectedErr string
}{
{
Name: "nil",
ExpectErr: true,
name: "nil",
expectedErr: "s3 settings are nil",
},
{
Name: "empty",
S3: &S3Settings{},
ExpectErr: true,
name: "empty",
s3: &S3Settings{},
expectedErr: "one of s3 settings is empty",
},
{
Name: "missing bucket",
S3: &S3Settings{
name: "missing bucket",
s3: &S3Settings{
AwsSecretAccessKey: "foo",
AwsAccessKeyID: "bar",
},
ExpectErr: true,
expectedErr: "one of s3 settings is empty",
},
{
Name: "missing aws access key id",
S3: &S3Settings{
name: "missing aws access key id",
s3: &S3Settings{
AwsSecretAccessKey: "foo",
Bucket: "bucket",
},
ExpectErr: true,
expectedErr: "one of s3 settings is empty",
},
{
Name: "missing aws secret access key",
S3: &S3Settings{
name: "missing aws secret access key",
s3: &S3Settings{
AwsAccessKeyID: "bar",
Bucket: "bucket",
},
ExpectErr: true,
expectedErr: "one of s3 settings is empty",
},
{
Name: "missing output format",
S3: &S3Settings{
name: "missing output format",
s3: &S3Settings{
Bucket: "bucket",
AwsSecretAccessKey: "foo",
AwsAccessKeyID: "bar",
},
ExpectErr: true,
expectedErr: `invalid s3 output format ""`,
},
{
Name: "valid",
S3: &S3Settings{
name: "valid",
s3: &S3Settings{
Bucket: "bucket",
AwsSecretAccessKey: "foo",
AwsAccessKeyID: "bar",
Expand All @@ -70,48 +68,46 @@ func TestS3Settings_Validate(t *testing.T) {
}

for _, testCase := range testCases {
err := testCase.S3.Validate()
if testCase.ExpectErr {
assert.Error(t, err, testCase.Name)
err := testCase.s3.Validate()
if testCase.expectedErr != "" {
assert.ErrorContains(t, err, testCase.expectedErr, testCase.name)
} else {
assert.NoError(t, err, testCase.Name)
assert.NoError(t, err, testCase.name)
}
}
}

func TestCfg_ValidateRedshift(t *testing.T) {
type _testCase struct {
Name string
Redshift *Redshift
ExpectErr bool
}

testCases := []_testCase{
testCases := []struct {
name string
redshift *Redshift
expectedErr string
}{
{
Name: "nil",
Redshift: nil,
ExpectErr: true,
name: "nil",
redshift: nil,
expectedErr: "redshift cfg is nil",
},
{
Name: "redshift settings exist, but all empty",
Redshift: &Redshift{},
ExpectErr: true,
name: "redshift settings exist, but all empty",
redshift: &Redshift{},
expectedErr: "one of redshift settings is empty",
},
{
Name: "redshift settings all set (missing port)",
Redshift: &Redshift{
name: "redshift settings all set (missing port)",
redshift: &Redshift{
Host: "host",
Database: "db",
Username: "user",
Password: "pw",
Bucket: "bucket",
CredentialsClause: "creds",
},
ExpectErr: true,
expectedErr: "redshift invalid port",
},
{
Name: "redshift settings all set (neg port)",
Redshift: &Redshift{
name: "redshift settings all set (neg port)",
redshift: &Redshift{
Host: "host",
Port: -500,
Database: "db",
Expand All @@ -120,11 +116,11 @@ func TestCfg_ValidateRedshift(t *testing.T) {
Bucket: "bucket",
CredentialsClause: "creds",
},
ExpectErr: true,
expectedErr: "redshift invalid port",
},
{
Name: "redshift settings all set",
Redshift: &Redshift{
name: "redshift settings all set",
redshift: &Redshift{
Host: "host",
Port: 123,
Database: "db",
Expand All @@ -138,14 +134,14 @@ func TestCfg_ValidateRedshift(t *testing.T) {

for _, testCase := range testCases {
cfg := &Config{
Redshift: testCase.Redshift,
Redshift: testCase.redshift,
Output: constants.Redshift,
}
err := cfg.ValidateRedshift()
if testCase.ExpectErr {
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.NoError(t, err, testCase.name)
}
}
}
2 changes: 1 addition & 1 deletion lib/debezium/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func parsePartitionKeyString(keyBytes []byte) (map[string]any, error) {
for _, kvPartString := range strings.Split(parsedKeyString, ",") {
kvParts := strings.Split(kvPartString, "=")
if len(kvParts) < 2 {
return nil, fmt.Errorf("malformed key value pair: %s", kvPartString)
return nil, fmt.Errorf("malformed key value pair: %q", kvPartString)
}

retMap[kvParts[0]] = strings.Join(kvParts[1:], "=")
Expand Down
Loading

0 comments on commit bcb52be

Please sign in to comment.