Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Mar 26, 2024
1 parent a54724c commit b0cfa56
Showing 1 changed file with 80 additions and 6 deletions.
86 changes: 80 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

func TestSettings_Validate(t *testing.T) {
dynamoCfg := &DynamoDB{
func dynamoDBCfg() *DynamoDB {
return &DynamoDB{
OffsetFile: "offset.txt",
AwsRegion: "us-east-1",
AwsAccessKeyID: "key-id",
AwsSecretAccessKey: "secret-access-key",
StreamArn: "arn:aws:dynamodb:us-east-1:123456789012:table/TableName/stream/2020-01-01T00:00:00.000",
TableName: "TableName",
}
}

func TestSettings_Validate(t *testing.T) {
type _tc struct {
name string
settings *Settings
Expand Down Expand Up @@ -47,24 +52,24 @@ func TestSettings_Validate(t *testing.T) {
},
{
name: "nil destination",
settings: &Settings{Source: SourceDynamo, DynamoDB: dynamoCfg},
settings: &Settings{Source: SourceDynamo, DynamoDB: dynamoDBCfg()},
expectedErr: "invalid destination: ''",
},
{
name: "invalid destination",
settings: &Settings{Source: SourceDynamo, DynamoDB: dynamoCfg, Destination: "foo"},
settings: &Settings{Source: SourceDynamo, DynamoDB: dynamoDBCfg(), Destination: "foo"},
expectedErr: "invalid destination: 'foo'",
},
{
name: "nil kafka",
settings: &Settings{Source: SourceDynamo, DynamoDB: dynamoCfg, Destination: DestinationKafka},
settings: &Settings{Source: SourceDynamo, DynamoDB: dynamoDBCfg(), Destination: DestinationKafka},
expectedErr: "kafka config is nil",
},
{
name: "valid",
settings: &Settings{
Source: SourceDynamo,
DynamoDB: dynamoCfg,
DynamoDB: dynamoDBCfg(),
Destination: DestinationKafka,
Kafka: &Kafka{
BootstrapServers: "localhost:9092",
Expand All @@ -84,3 +89,72 @@ func TestSettings_Validate(t *testing.T) {

}
}

func TestReadConfig(t *testing.T) {
{
// Missing file
_, err := ReadConfig(filepath.Join(t.TempDir(), "foo.yaml"))
assert.ErrorContains(t, err, "foo.yaml: no such file or directory")
}
{
// Malformed file
filename := filepath.Join(t.TempDir(), "foo.yaml")
assert.NoError(t, os.WriteFile(filename, []byte("hello"), os.ModePerm))
_, err := ReadConfig(filename)
assert.ErrorContains(t, err, "failed to unmarshal config file")
}
{
// Well-formed empty file
filename := filepath.Join(t.TempDir(), "foo.yaml")
assert.NoError(t, os.WriteFile(filename, []byte("{}"), os.ModePerm))
_, err := ReadConfig(filename)
assert.ErrorContains(t, err, "dynamodb config is nil")
}
{
// Well-formed file with source and destination omitted
filename := filepath.Join(t.TempDir(), "foo.yaml")
settings := Settings{
DynamoDB: dynamoDBCfg(),
Kafka: &Kafka{
BootstrapServers: "asdf",
TopicPrefix: "prefix",
},
}
bytes, err := yaml.Marshal(settings)
assert.NoError(t, err)
assert.NoError(t, os.WriteFile(filename, bytes, os.ModePerm))
settingsOut, err := ReadConfig(filename)
assert.NoError(t, err)
assert.Equal(t, settingsOut.Source, SourceDynamo)
assert.Equal(t, settingsOut.Destination, DestinationKafka)
}
{
// Well-formed file with explicit source and destination
filename := filepath.Join(t.TempDir(), "foo.yaml")
settings := Settings{
Source: SourcePostgreSQL,
PostgreSQL: &PostgreSQL{
Host: "host",
Port: 5432,
Username: "user",
Password: "pass",
Database: "database",
Tables: []*PostgreSQLTable{
{Name: "table", Schema: "schema"},
},
},
Destination: DestinationKafka,
Kafka: &Kafka{
BootstrapServers: "asdf",
TopicPrefix: "prefix",
},
}
bytes, err := yaml.Marshal(settings)
assert.NoError(t, err)
assert.NoError(t, os.WriteFile(filename, bytes, os.ModePerm))
settingsOut, err := ReadConfig(filename)
assert.NoError(t, err)
assert.Equal(t, settingsOut.Source, SourcePostgreSQL)
assert.Equal(t, settingsOut.Destination, DestinationKafka)
}
}

0 comments on commit b0cfa56

Please sign in to comment.