Skip to content

Commit f4f3ed5

Browse files
committed
add tests
1 parent 9ae39d8 commit f4f3ed5

3 files changed

Lines changed: 24 additions & 19 deletions

File tree

internal/cmdopts/cmdsource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (cmd *SourcePingCommand) Execute(args []string) error {
5555
_, e = sources.ResolveDatabasesFromPostgres(s)
5656
default:
5757
mdb := &sources.SourceConn{Source: s}
58-
e = mdb.Ping(context.Background())
58+
e = mdb.Connect(context.Background(), cmd.owner.Sources)
5959
}
6060
if e != nil {
6161
fmt.Printf("FAIL:\t%s (%s)\n", s.Name, e)

internal/sources/conn.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type (
2222
SourceConns []*SourceConn
2323
)
2424

25-
// ping will try to ping the server to ensure the connection is still alive
26-
func (md *SourceConn) ping(ctx context.Context) (err error) {
25+
// Ping will try to ping the server to ensure the connection is still alive
26+
func (md *SourceConn) Ping(ctx context.Context) (err error) {
2727
if md.Kind == SourcePgBouncer {
2828
// pgbouncer is very picky about the queries it accepts
2929
_, err = md.Conn.Exec(ctx, "SHOW VERSION")
@@ -32,17 +32,6 @@ func (md *SourceConn) ping(ctx context.Context) (err error) {
3232
return md.Conn.Ping(ctx)
3333
}
3434

35-
// Ping will try to establish a brand new connection to the server and return any error
36-
func (md *SourceConn) Ping(ctx context.Context) error {
37-
c, err := pgxpool.New(ctx, md.ConnStr)
38-
if err != nil {
39-
return err
40-
}
41-
defer c.Close()
42-
t := &SourceConn{Conn: c, Source: md.Source}
43-
return t.ping(ctx)
44-
}
45-
4635
// Connect will establish a connection to the database if it's not already connected.
4736
// If the connection is already established, it pings the server to ensure it's still alive.
4837
func (md *SourceConn) Connect(ctx context.Context, opts CmdOpts) (err error) {
@@ -61,7 +50,7 @@ func (md *SourceConn) Connect(ctx context.Context, opts CmdOpts) (err error) {
6150
return err
6251
}
6352
}
64-
return md.ping(ctx)
53+
return md.Ping(ctx)
6554
}
6655

6756
// ParseConfig will parse the connection string and store the result in the connection config

internal/sources/conn_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"time"
66

7+
"github.com/jackc/pgx/v5/pgconn"
78
"github.com/pashagolub/pgxmock/v4"
89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
@@ -16,7 +17,7 @@ import (
1617

1718
const ImageName = "docker.io/postgres:17-alpine"
1819

19-
func TestMonitoredDatabase_Connect(t *testing.T) {
20+
func TestSourceConn_Connect(t *testing.T) {
2021
pgContainer, err := postgres.Run(ctx,
2122
ImageName,
2223
testcontainers.WithWaitStrategy(
@@ -40,7 +41,7 @@ func TestMonitoredDatabase_Connect(t *testing.T) {
4041
err = md.Connect(ctx, sources.CmdOpts{})
4142
assert.NoError(t, err)
4243
}
43-
func TestMonitoredDatabase_GetDatabaseName(t *testing.T) {
44+
func TestSourceConn_GetDatabaseName(t *testing.T) {
4445
md := &sources.SourceConn{}
4546
md.ConnStr = "postgres://user:password@localhost:5432/mydatabase"
4647
expected := "mydatabase"
@@ -58,7 +59,7 @@ func TestMonitoredDatabase_GetDatabaseName(t *testing.T) {
5859
assert.Equal(t, expected, got, "GetDatabaseName() = %v, want %v", got, expected)
5960
}
6061

61-
func TestMonitoredDatabase_SetDatabaseName(t *testing.T) {
62+
func TestSourceConn_SetDatabaseName(t *testing.T) {
6263
md := &sources.SourceConn{}
6364
md.ConnStr = "postgres://user:password@localhost:5432/mydatabase"
6465
expected := "mydatabase"
@@ -80,7 +81,7 @@ func TestMonitoredDatabase_SetDatabaseName(t *testing.T) {
8081
assert.Equal(t, expected, got, "GetDatabaseName() = %v, want %v", got, expected)
8182
}
8283

83-
func TestMonitoredDatabase_IsPostgresSource(t *testing.T) {
84+
func TestSourceConn_IsPostgresSource(t *testing.T) {
8485
md := &sources.SourceConn{}
8586
md.Kind = sources.SourcePostgres
8687
assert.True(t, md.IsPostgresSource(), "IsPostgresSource() = false, want true")
@@ -95,6 +96,21 @@ func TestMonitoredDatabase_IsPostgresSource(t *testing.T) {
9596
assert.True(t, md.IsPostgresSource(), "IsPostgresSource() = false, want true")
9697
}
9798

99+
func TestSourceConn_Ping(t *testing.T) {
100+
db, err := pgxmock.NewPool()
101+
require.NoError(t, err)
102+
md := &sources.SourceConn{Conn: db}
103+
104+
db.ExpectPing()
105+
md.Kind = sources.SourcePostgres
106+
assert.NoError(t, md.Ping(ctx), "Ping() = error, want nil")
107+
108+
db.ExpectExec("SHOW VERSION").WillReturnResult(pgconn.NewCommandTag("SELECT 1"))
109+
md.Conn = db
110+
md.Kind = sources.SourcePgBouncer
111+
assert.NoError(t, md.Ping(ctx), "Ping() = error, want nil")
112+
}
113+
98114
type testSourceReader struct {
99115
sources.Sources
100116
error

0 commit comments

Comments
 (0)