Skip to content

Commit

Permalink
chore(tester): Refactor postgres naming to generic
Browse files Browse the repository at this point in the history
  • Loading branch information
fbufler committed Dec 4, 2023
1 parent 8eee3ab commit 04dbdcd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func local(cfg *LocalCfg, ctx context.Context) error {
for _, dbCfg := range cfg.Databases {
dbs = append(dbs, database.NewPostgres(dbCfg))
}
tester := tester.NewPostgres(tester.Config{
tester := tester.New(tester.Config{
Databases: dbs,
TestTimeout: cfg.TestTimeout,
TestInterval: cfg.TestInterval,
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func serve(cfg *ServeCfg, ctx context.Context) error {
for _, dbCfg := range cfg.Databases {
dbs = append(dbs, database.NewPostgres(dbCfg))
}
tester := tester.NewPostgres(tester.Config{
tester := tester.New(tester.Config{
Databases: dbs,
TestTimeout: cfg.TestTimeout,
TestInterval: cfg.TestInterval,
Expand Down
2 changes: 1 addition & 1 deletion cmd/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func setup(cfg *SetupCfg, ctx context.Context) error {
for _, dbCfg := range cfg.Databases {
dbs = append(dbs, database.NewPostgres(dbCfg))
}
tester := tester.NewPostgres(tester.Config{
tester := tester.New(tester.Config{
Databases: dbs,
})
log.Info().Msg("Setup tester")
Expand Down
14 changes: 7 additions & 7 deletions internal/tester/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import (
"github.com/rs/zerolog/log"
)

type Postgres struct {
type TesterImpl struct {
results chan Result
config Config
}

func NewPostgres(config Config) Tester {
return &Postgres{
func New(config Config) Tester {
return &TesterImpl{
results: make(chan Result),
config: config,
}
}

func (p *Postgres) Run(ctx context.Context) chan Result {
func (p *TesterImpl) Run(ctx context.Context) chan Result {
go p.run(ctx)
return p.results
}

func (p *Postgres) run(ctx context.Context) {
func (p *TesterImpl) run(ctx context.Context) {
log.Info().Msg("Starting postgres tester")
log.Debug().Msg("Starting database tests")
for {
Expand All @@ -49,7 +49,7 @@ func (p *Postgres) run(ctx context.Context) {
}
}

func (p *Postgres) runDatabaseTest(db database.Database, ctx context.Context) {
func (p *TesterImpl) runDatabaseTest(db database.Database, ctx context.Context) {
result := Result{
Database: db.Identifier(),
Connectable: false,
Expand Down Expand Up @@ -108,7 +108,7 @@ func (p *Postgres) runDatabaseTest(db database.Database, ctx context.Context) {
}
}

func (p *Postgres) Setup(ctx context.Context) error {
func (p *TesterImpl) Setup(ctx context.Context) error {
var setupErrors []error
for _, db := range p.config.Databases {
err := db.SetupTestTable(ctx)
Expand Down
32 changes: 16 additions & 16 deletions internal/tester/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestSetup(t *testing.T) {
defer ctrl.Finish()
mockDatabase := database.NewMockDatabase(ctrl)
ctx := context.Background()
postgresTester := NewPostgres(Config{
postgresTester := New(Config{
Databases: []database.Database{
mockDatabase,
},
Expand All @@ -33,7 +33,7 @@ func TestSetupError(t *testing.T) {
defer ctrl.Finish()
mockDatabase := database.NewMockDatabase(ctrl)
ctx := context.Background()
postgresTester := NewPostgres(Config{
postgresTester := New(Config{
Databases: []database.Database{
mockDatabase,
},
Expand All @@ -49,7 +49,7 @@ func TestRun(t *testing.T) {
defer ctrl.Finish()
mockDatabase := database.NewMockDatabase(ctrl)
ctx, cancel := context.WithCancel(context.Background())
postgresTester := NewPostgres(Config{
postgresTester := New(Config{
TestTimeout: 1,
TestInterval: 1,
Databases: []database.Database{
Expand All @@ -62,7 +62,7 @@ func TestRun(t *testing.T) {
mockDatabase.EXPECT().TestWrite(ctx).Return(nil)
mockDatabase.EXPECT().Close().Return(nil)
go postgresTester.Run(ctx)
result := <-postgresTester.(*Postgres).results
result := <-postgresTester.(*TesterImpl).results
cancel()
assert.Equal(t, "test", result.Database)
assert.Equal(t, true, result.Connectable)
Expand All @@ -75,7 +75,7 @@ func TestRunDatabaseTest(t *testing.T) {
defer ctrl.Finish()
mockDatabase := database.NewMockDatabase(ctrl)
ctx, cancel := context.WithCancel(context.Background())
postgresTester := NewPostgres(Config{
postgresTester := New(Config{
TestTimeout: 1,
TestInterval: 1,
Databases: []database.Database{
Expand All @@ -87,8 +87,8 @@ func TestRunDatabaseTest(t *testing.T) {
mockDatabase.EXPECT().TestRead(ctx).Return(nil)
mockDatabase.EXPECT().TestWrite(ctx).Return(nil)
mockDatabase.EXPECT().Close().Return(nil)
go postgresTester.(*Postgres).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*Postgres).results
go postgresTester.(*TesterImpl).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*TesterImpl).results
cancel()
assert.Equal(t, "test", result.Database)
assert.Equal(t, true, result.Connectable)
Expand All @@ -101,7 +101,7 @@ func TestRunDatabaseTestConnectError(t *testing.T) {
defer ctrl.Finish()
mockDatabase := database.NewMockDatabase(ctrl)
ctx, cancel := context.WithCancel(context.Background())
postgresTester := NewPostgres(Config{
postgresTester := New(Config{
TestTimeout: 1,
TestInterval: 1,
Databases: []database.Database{
Expand All @@ -110,8 +110,8 @@ func TestRunDatabaseTestConnectError(t *testing.T) {
})
mockDatabase.EXPECT().Identifier().Return("test")
mockDatabase.EXPECT().Connect().Return(errors.New("Connect error"))
go postgresTester.(*Postgres).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*Postgres).results
go postgresTester.(*TesterImpl).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*TesterImpl).results
cancel()
assert.Equal(t, "test", result.Database)
assert.Equal(t, false, result.Connectable)
Expand All @@ -124,7 +124,7 @@ func TestRunDatabaseTestReadError(t *testing.T) {
defer ctrl.Finish()
mockDatabase := database.NewMockDatabase(ctrl)
ctx, cancel := context.WithCancel(context.Background())
postgresTester := NewPostgres(Config{
postgresTester := New(Config{
TestTimeout: 1,
TestInterval: 1,
Databases: []database.Database{
Expand All @@ -135,8 +135,8 @@ func TestRunDatabaseTestReadError(t *testing.T) {
mockDatabase.EXPECT().Connect().Return(nil)
mockDatabase.EXPECT().TestRead(ctx).Return(errors.New("Read error"))
mockDatabase.EXPECT().Close().Return(nil)
go postgresTester.(*Postgres).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*Postgres).results
go postgresTester.(*TesterImpl).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*TesterImpl).results
cancel()
assert.Equal(t, "test", result.Database)
assert.Equal(t, true, result.Connectable)
Expand All @@ -149,7 +149,7 @@ func TestRunDatabaseTestWriteError(t *testing.T) {
defer ctrl.Finish()
mockDatabase := database.NewMockDatabase(ctrl)
ctx, cancel := context.WithCancel(context.Background())
postgresTester := NewPostgres(Config{
postgresTester := New(Config{
TestTimeout: 1,
TestInterval: 1,
Databases: []database.Database{
Expand All @@ -161,8 +161,8 @@ func TestRunDatabaseTestWriteError(t *testing.T) {
mockDatabase.EXPECT().TestRead(ctx).Return(nil)
mockDatabase.EXPECT().TestWrite(ctx).Return(errors.New("Write error"))
mockDatabase.EXPECT().Close().Return(nil)
go postgresTester.(*Postgres).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*Postgres).results
go postgresTester.(*TesterImpl).runDatabaseTest(mockDatabase, ctx)
result := <-postgresTester.(*TesterImpl).results
cancel()
assert.Equal(t, "test", result.Database)
assert.Equal(t, true, result.Connectable)
Expand Down

0 comments on commit 04dbdcd

Please sign in to comment.