-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_test.go
More file actions
227 lines (204 loc) · 6.1 KB
/
Copy pathdb_test.go
File metadata and controls
227 lines (204 loc) · 6.1 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package config_test
import (
"os"
"testing"
"github.com/weprodev/go-pkg/config"
)
func TestApplyDatabaseOverrides_NoEnv(t *testing.T) {
cfg := config.DatabaseConfig{
Host: "original-host",
Port: 5432,
User: "original-user",
DBName: "original-db",
Password: "original-pass",
MaxOpenConns: 10,
MaxIdleConns: 5,
}
result := config.ApplyDatabaseOverrides(cfg)
if result.Host != "original-host" {
t.Errorf("Host = %q, want %q", result.Host, "original-host")
}
if result.Port != 5432 {
t.Errorf("Port = %d, want 5432", result.Port)
}
if result.MaxOpenConns != 10 {
t.Errorf("MaxOpenConns = %d, want 10", result.MaxOpenConns)
}
}
func TestApplyDatabaseOverrides_DatabaseURL(t *testing.T) {
const url = "postgres://user:pass@host:5432/dbname"
t.Setenv("DATABASE_URL", url)
t.Setenv("DB_MAX_OPEN_CONNS", "25")
t.Setenv("DB_MAX_IDLE_CONNS", "12")
result := config.ApplyDatabaseOverrides(config.DatabaseConfig{
Host: "fallback",
MaxOpenConns: 5,
MaxIdleConns: 2,
})
if result.DatabaseURL != url {
t.Errorf("DatabaseURL = %q, want %q", result.DatabaseURL, url)
}
if result.MaxOpenConns != 25 {
t.Errorf("MaxOpenConns = %d, want 25", result.MaxOpenConns)
}
if result.MaxIdleConns != 12 {
t.Errorf("MaxIdleConns = %d, want 12", result.MaxIdleConns)
}
// Individual fields should not be overwritten when DATABASE_URL is set.
if result.Host != "fallback" {
t.Errorf("Host = %q, want %q (should be unchanged)", result.Host, "fallback")
}
}
func TestApplyDatabaseOverrides_IndividualFields(t *testing.T) {
t.Setenv("DB_HOST", "env-host")
t.Setenv("DB_PORT", "3306")
t.Setenv("DB_USER", "env-user")
t.Setenv("DB_NAME", "env-db")
t.Setenv("DB_PASSWORD", "env-pass")
t.Setenv("DB_SSLMODE", "require")
t.Setenv("DB_CONNECTION_NAME", "env-conn")
t.Setenv("DB_MAX_OPEN_CONNS", "20")
t.Setenv("DB_MAX_IDLE_CONNS", "10")
result := config.ApplyDatabaseOverrides(config.DatabaseConfig{})
checks := map[string]string{
result.Host: "env-host",
result.User: "env-user",
result.DBName: "env-db",
result.Password: "env-pass",
result.SSLMode: "require",
result.ConnectionName: "env-conn",
}
for got, want := range checks {
if got != want {
t.Errorf("field = %q, want %q", got, want)
}
}
if result.Port != 3306 {
t.Errorf("Port = %d, want 3306", result.Port)
}
if result.MaxOpenConns != 20 {
t.Errorf("MaxOpenConns = %d, want 20", result.MaxOpenConns)
}
}
func TestApplyDatabaseOverrides_PartialEnv(t *testing.T) {
t.Setenv("DB_HOST", "env-host")
t.Setenv("DB_USER", "env-user")
result := config.ApplyDatabaseOverrides(config.DatabaseConfig{
Host: "original-host",
Port: 5432,
User: "original-user",
DBName: "original-db",
})
if result.Host != "env-host" {
t.Errorf("Host = %q, want %q", result.Host, "env-host")
}
// Non-overidden fields must be preserved.
if result.DBName != "original-db" {
t.Errorf("DBName = %q, want %q (should be unchanged)", result.DBName, "original-db")
}
}
// ─── ValidateDatabaseConfig ───────────────────────────────────────────────────
func TestValidateDatabaseConfig_Valid(t *testing.T) {
tests := []struct {
name string
cfg config.DatabaseConfig
}{
{
name: "valid with host",
cfg: config.DatabaseConfig{
Host: "localhost", Port: 5432,
User: "u", Password: "p", DBName: "db",
},
},
{
name: "valid with connection name",
cfg: config.DatabaseConfig{
ConnectionName: "proj:region:inst", Port: 5432,
User: "u", Password: "p", DBName: "db",
},
},
{
name: "valid with DATABASE_URL",
cfg: config.DatabaseConfig{DatabaseURL: "postgres://u:p@host/db"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errs, _ := config.ValidateDatabaseConfig(tt.cfg)
if len(errs) != 0 {
t.Errorf("expected no errors, got: %v", errs)
}
})
}
}
func TestValidateDatabaseConfig_Errors(t *testing.T) {
tests := []struct {
name string
cfg config.DatabaseConfig
wantErrCount int
}{
{
name: "missing host and connection name",
cfg: config.DatabaseConfig{Port: 5432, User: "u", Password: "p", DBName: "db"},
wantErrCount: 1,
},
{
name: "invalid port — zero",
cfg: config.DatabaseConfig{Host: "h", Port: 0, User: "u", Password: "p", DBName: "db"},
wantErrCount: 1,
},
{
name: "invalid port — too high",
cfg: config.DatabaseConfig{Host: "h", Port: 99999, User: "u", Password: "p", DBName: "db"},
wantErrCount: 1,
},
{
name: "missing user",
cfg: config.DatabaseConfig{Host: "h", Port: 5432, Password: "p", DBName: "db"},
wantErrCount: 1,
},
{
name: "missing password",
cfg: config.DatabaseConfig{Host: "h", Port: 5432, User: "u", DBName: "db"},
wantErrCount: 1,
},
{
name: "missing dbname",
cfg: config.DatabaseConfig{Host: "h", Port: 5432, User: "u", Password: "p"},
wantErrCount: 1,
},
{
name: "empty config — all errors",
cfg: config.DatabaseConfig{},
wantErrCount: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errs, _ := config.ValidateDatabaseConfig(tt.cfg)
if len(errs) != tt.wantErrCount {
t.Errorf("error count = %d, want %d: %v", len(errs), tt.wantErrCount, errs)
}
})
}
}
func TestValidateDatabaseConfig_WithRealEnv(t *testing.T) {
_ = os.Setenv("DB_HOST", "real-host")
_ = os.Setenv("DB_USER", "real-user")
_ = os.Setenv("DB_PASSWORD", "real-pass")
_ = os.Setenv("DB_NAME", "real-db")
defer func() {
_ = os.Unsetenv("DB_HOST")
_ = os.Unsetenv("DB_USER")
_ = os.Unsetenv("DB_PASSWORD")
_ = os.Unsetenv("DB_NAME")
}()
cfg := config.DatabaseConfig{Host: "real-host", Port: 5432, User: "real-user", Password: "real-pass", DBName: "real-db"}
errs, warns := config.ValidateDatabaseConfig(cfg)
if len(errs) != 0 {
t.Errorf("expected no errors, got: %v", errs)
}
if len(warns) != 0 {
t.Errorf("expected no warnings, got: %v", warns)
}
}