-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
86 lines (70 loc) · 2.46 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"io"
"log/slog"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPoolConfig(t *testing.T) {
dsn := "postgres://user:password@localhost:5432/testdb"
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
config, err := PoolConfig(&dsn, logger)
t.Run("Basic Configuration", func(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, int32(4), config.MaxConns)
assert.Equal(t, int32(0), config.MinConns)
assert.Equal(t, time.Hour, config.MaxConnLifetime)
assert.Equal(t, 15*time.Minute, config.MaxConnIdleTime)
assert.Equal(t, time.Minute, config.HealthCheckPeriod)
assert.Equal(t, 5*time.Second, config.ConnConfig.ConnectTimeout)
})
t.Run("BeforeConnect Callback", func(t *testing.T) {
require.NotNil(t, config.BeforeConnect)
err := config.BeforeConnect(context.Background(), &pgx.ConnConfig{})
assert.NoError(t, err)
})
t.Run("AfterConnect Callback", func(t *testing.T) {
require.NotNil(t, config.AfterConnect)
err := config.AfterConnect(context.Background(), &pgx.Conn{})
assert.NoError(t, err)
})
t.Run("BeforeAcquire Callback", func(t *testing.T) {
require.NotNil(t, config.BeforeAcquire)
result := config.BeforeAcquire(context.Background(), &pgx.Conn{})
assert.True(t, result)
})
t.Run("AfterRelease Callback", func(t *testing.T) {
require.NotNil(t, config.AfterRelease)
result := config.AfterRelease(&pgx.Conn{})
assert.True(t, result)
})
t.Run("BeforeClose Callback", func(t *testing.T) {
require.NotNil(t, config.BeforeClose)
config.BeforeClose(&pgx.Conn{})
})
}
func TestPoolConfigWithInvalidDSN(t *testing.T) {
invalidDSN := "invalid-dsn"
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
config, err := PoolConfig(&invalidDSN, logger)
assert.Error(t, err)
assert.Nil(t, config)
assert.Contains(t, err.Error(), "failed to parse database configuration")
}
func TestPoolConfigWithNilLogger(t *testing.T) {
dsn := "postgres://user:password@localhost:5432/testdb"
config, err := PoolConfig(&dsn, nil)
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, int32(4), config.MaxConns)
assert.Equal(t, int32(0), config.MinConns)
assert.Equal(t, time.Hour, config.MaxConnLifetime)
assert.Equal(t, 15*time.Minute, config.MaxConnIdleTime)
assert.Equal(t, time.Minute, config.HealthCheckPeriod)
assert.Equal(t, 5*time.Second, config.ConnConfig.ConnectTimeout)
}