Added support for Postgres polling sources
This commit is contained in:
@@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
pgconn "gitea.maximumdirect.net/ejr/feedkit/internal/postgres"
|
||||
)
|
||||
|
||||
type fakeResult struct {
|
||||
@@ -223,10 +223,13 @@ func TestPostgresFactoryBuildsMultipleSinksWithSameSchema(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
dbs := []*fakeDB{{}, {}}
|
||||
var gotDSNs []string
|
||||
openPostgresDB = func(dsn string) (postgresDB, error) {
|
||||
gotDSNs = append(gotDSNs, dsn)
|
||||
db := dbs[len(gotDSNs)-1]
|
||||
var gotCfgs []pgconn.ConnConfig
|
||||
openPostgresDB = func(ctx context.Context, cfg pgconn.ConnConfig) (postgresDB, error) {
|
||||
gotCfgs = append(gotCfgs, cfg)
|
||||
db := dbs[len(gotCfgs)-1]
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
@@ -252,8 +255,11 @@ func TestPostgresFactoryBuildsMultipleSinksWithSameSchema(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
if len(gotDSNs) != 2 {
|
||||
t.Fatalf("len(gotDSNs) = %d, want 2", len(gotDSNs))
|
||||
if len(gotCfgs) != 2 {
|
||||
t.Fatalf("len(gotCfgs) = %d, want 2", len(gotCfgs))
|
||||
}
|
||||
if gotCfgs[0].Username != "user" || gotCfgs[0].Password != "pass" {
|
||||
t.Fatalf("first ConnConfig = %+v", gotCfgs[0])
|
||||
}
|
||||
for i, db := range dbs {
|
||||
if db.pingCalls != 1 {
|
||||
@@ -327,9 +333,12 @@ func TestNewPostgresSinkFromConfigEagerInit(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
db := &fakeDB{}
|
||||
var gotDSN string
|
||||
openPostgresDB = func(dsn string) (postgresDB, error) {
|
||||
gotDSN = dsn
|
||||
var gotCfg pgconn.ConnConfig
|
||||
openPostgresDB = func(ctx context.Context, cfg pgconn.ConnConfig) (postgresDB, error) {
|
||||
gotCfg = cfg
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
@@ -362,16 +371,14 @@ func TestNewPostgresSinkFromConfigEagerInit(t *testing.T) {
|
||||
t.Fatalf("unexpected create index query: %s", db.execCalls[1].query)
|
||||
}
|
||||
|
||||
u, err := url.Parse(gotDSN)
|
||||
if err != nil {
|
||||
t.Fatalf("parse dsn: %v", err)
|
||||
if gotCfg.URI != "postgres://db.example.local:5432/feedkit?sslmode=disable" {
|
||||
t.Fatalf("URI = %q", gotCfg.URI)
|
||||
}
|
||||
if u.User == nil || u.User.Username() != "app_user" {
|
||||
t.Fatalf("dsn missing username: %q", gotDSN)
|
||||
if gotCfg.Username != "app_user" {
|
||||
t.Fatalf("Username = %q, want app_user", gotCfg.Username)
|
||||
}
|
||||
pass, ok := u.User.Password()
|
||||
if !ok || pass != "app_pass" {
|
||||
t.Fatalf("dsn missing password: %q", gotDSN)
|
||||
if gotCfg.Password != "app_pass" {
|
||||
t.Fatalf("Password = %q, want app_pass", gotCfg.Password)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,7 +386,7 @@ func TestNewPostgresSinkFromConfigInitFailureClosesDB(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
db := &fakeDB{execErrOnCall: 1, execErr: errors.New("ddl failed")}
|
||||
openPostgresDB = func(_ string) (postgresDB, error) {
|
||||
openPostgresDB = func(_ context.Context, _ pgconn.ConnConfig) (postgresDB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
@@ -415,7 +422,7 @@ func TestNewPostgresSinkFromConfigPruneParamAccepted(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
openPostgresDB = func(_ string) (postgresDB, error) {
|
||||
openPostgresDB = func(_ context.Context, _ pgconn.ConnConfig) (postgresDB, error) {
|
||||
return &fakeDB{}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user