-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_test.go
233 lines (218 loc) · 6.56 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
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
228
229
230
231
232
233
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package dbkit
import (
"bytes"
"database/sql"
"testing"
"github.com/stretchr/testify/require"
"github.com/acronis/go-appkit/config"
)
func TestConfig(t *testing.T) {
allDialects := []Dialect{DialectSQLite, DialectMySQL, DialectPostgres, DialectPgx, DialectMSSQL}
t.Run("unknown dialect", func(t *testing.T) {
cfgData := bytes.NewBufferString(`
db:
dialect: fake-dialect
`)
cfg := NewConfig(allDialects)
err := config.NewDefaultLoader("").LoadFromReader(cfgData, config.DataTypeYAML, cfg)
require.EqualError(t, err, `db.dialect: unknown value "fake-dialect", should be one of [sqlite3 mysql postgres pgx mssql]`)
})
t.Run("read mysql parameters", func(t *testing.T) {
cfgData := bytes.NewBufferString(`
db:
dialect: mysql
mysql:
host: mysql-host
port: 3307
database: mysql_db
user: mysql-user
password: mysql-password
txLevel: Repeatable Read
`)
cfg := NewConfig(allDialects)
err := config.NewDefaultLoader("").LoadFromReader(cfgData, config.DataTypeYAML, cfg)
require.NoError(t, err)
require.Equal(t, DialectMySQL, cfg.Dialect)
wantMySQLCfg := MySQLConfig{
Host: "mysql-host",
Port: 3307,
Database: "mysql_db",
User: "mysql-user",
Password: "mysql-password",
TxIsolationLevel: sql.LevelRepeatableRead,
}
require.Equal(t, wantMySQLCfg, cfg.MySQL)
})
t.Run("read postgres (lib/pq) parameters", func(t *testing.T) {
cfgData := bytes.NewBufferString(`
db:
dialect: postgres
postgres:
host: pg-host
port: 5433
database: pg_db
user: pg-user
password: pg-password
txLevel: Repeatable Read
sslMode: verify-full
searchPath: pg-search
`)
cfg := NewConfig(allDialects)
err := config.NewDefaultLoader("").LoadFromReader(cfgData, config.DataTypeYAML, cfg)
require.NoError(t, err)
require.Equal(t, DialectPostgres, cfg.Dialect)
wantPostgresCfg := PostgresConfig{
Host: "pg-host",
Port: 5433,
Database: "pg_db",
User: "pg-user",
Password: "pg-password",
TxIsolationLevel: sql.LevelRepeatableRead,
SSLMode: PostgresSSLModeVerifyFull,
SearchPath: "pg-search",
}
require.Equal(t, wantPostgresCfg, cfg.Postgres)
})
t.Run("read postgres (pgx) parameters", func(t *testing.T) {
cfgData := bytes.NewBufferString(`
db:
dialect: pgx
postgres:
host: pg-host
port: 5433
database: pg_db
user: pg-user
password: pg-password
txLevel: Repeatable Read
sslMode: verify-full
searchPath: pg-search
`)
cfg := NewConfig(allDialects)
err := config.NewDefaultLoader("").LoadFromReader(cfgData, config.DataTypeYAML, cfg)
require.NoError(t, err)
require.Equal(t, DialectPgx, cfg.Dialect)
wantPostgresCfg := PostgresConfig{
Host: "pg-host",
Port: 5433,
Database: "pg_db",
User: "pg-user",
Password: "pg-password",
TxIsolationLevel: sql.LevelRepeatableRead,
SSLMode: PostgresSSLModeVerifyFull,
SearchPath: "pg-search",
AdditionalParameters: []Parameter{{Name: "target_session_attrs", Value: "read-write"}},
}
require.Equal(t, wantPostgresCfg, cfg.Postgres)
})
t.Run("read postgres (pgx) parameters with overridden target_session_attrs", func(t *testing.T) {
cfgData := bytes.NewBufferString(`
db:
dialect: pgx
postgres:
host: pg-host
port: 5433
database: pg_db
user: pg-user
password: pg-password
txLevel: Repeatable Read
sslMode: verify-full
searchPath: pg-search
additionalParameters:
target_session_attrs: read-only
`)
cfg := NewConfig(allDialects)
err := config.NewDefaultLoader("").LoadFromReader(cfgData, config.DataTypeYAML, cfg)
require.NoError(t, err)
require.Equal(t, DialectPgx, cfg.Dialect)
wantPostgresCfg := PostgresConfig{
Host: "pg-host",
Port: 5433,
Database: "pg_db",
User: "pg-user",
Password: "pg-password",
TxIsolationLevel: sql.LevelRepeatableRead,
SSLMode: PostgresSSLModeVerifyFull,
SearchPath: "pg-search",
AdditionalParameters: []Parameter{{Name: "target_session_attrs", Value: "read-only"}},
}
require.Equal(t, wantPostgresCfg, cfg.Postgres)
})
t.Run("read mssql parameters", func(t *testing.T) {
cfgData := bytes.NewBufferString(`
db:
dialect: mssql
mssql:
host: mssql-host
port: 1433
database: mssql_db
user: mssql-user
password: mssql-password
txLevel: Repeatable Read
`)
cfg := NewConfig(allDialects)
err := config.NewDefaultLoader("").LoadFromReader(cfgData, config.DataTypeYAML, cfg)
require.NoError(t, err)
require.Equal(t, DialectMSSQL, cfg.Dialect)
wantMSSQLCfg := MSSQLConfig{
Host: "mssql-host",
Port: 1433,
Database: "mssql_db",
User: "mssql-user",
Password: "mssql-password",
TxIsolationLevel: sql.LevelRepeatableRead,
}
require.Equal(t, wantMSSQLCfg, cfg.MSSQL)
})
t.Run("read multiple connection parameters from one source", func(t *testing.T) {
cfgData := bytes.NewBufferString(`
subsystemA:
db:
dialect: mssql
mssql:
host: mssql-host
port: 1433
database: subsystem_a
user: mssql-user-a
password: mssql-password-a
txLevel: Repeatable Read
subsystemB:
db:
dialect: mssql
mssql:
host: mssql-host
port: 1433
database: subsystem_b
user: mssql-user-b
password: mssql-password-b
txLevel: Read Committed
`)
cfgA := NewConfigWithKeyPrefix("subsystemA", allDialects)
cfgB := NewConfigWithKeyPrefix("subsystemB", allDialects)
err := config.NewDefaultLoader("").LoadFromReader(cfgData, config.DataTypeYAML, cfgA, cfgB)
require.NoError(t, err)
require.Equal(t, DialectMSSQL, cfgA.Dialect)
wantSubSystemACfg := MSSQLConfig{
Host: "mssql-host",
Port: 1433,
Database: "subsystem_a",
User: "mssql-user-a",
Password: "mssql-password-a",
TxIsolationLevel: sql.LevelRepeatableRead,
}
require.Equal(t, wantSubSystemACfg, cfgA.MSSQL)
require.Equal(t, DialectMSSQL, cfgB.Dialect)
wantSubSystemBCfg := MSSQLConfig{
Host: "mssql-host",
Port: 1433,
Database: "subsystem_b",
User: "mssql-user-b",
Password: "mssql-password-b",
TxIsolationLevel: sql.LevelReadCommitted,
}
require.Equal(t, wantSubSystemBCfg, cfgB.MSSQL)
})
}