-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdsn_test.go
97 lines (89 loc) · 2.56 KB
/
dsn_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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package dbkit
import (
"database/sql"
"testing"
"github.com/stretchr/testify/require"
)
func TestMakeMySQLDSN(t *testing.T) {
cfg := &MySQLConfig{
Host: "myhost",
Port: 3307,
User: "myadmin",
Password: "mypassword",
Database: "mydb",
}
wantDSN := "myadmin:mypassword@tcp(myhost:3307)/mydb?multiStatements=true&parseTime=true&autocommit=false"
gotDSN := MakeMySQLDSN(cfg)
require.Equal(t, wantDSN, gotDSN)
}
func TestMakePostgresDSN(t *testing.T) {
tests := []struct {
Name string
Cfg *PostgresConfig
WantDSN string
}{
{
Name: "search_path is used",
Cfg: &PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: PostgresSSLModeRequire,
SearchPath: "pgsearch",
AdditionalParameters: map[string]string{"param1": "foo", "param2": "bar"},
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require&search_path=pgsearch¶m1=foo¶m2=bar",
},
{
Name: "base",
Cfg: &PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: PostgresSSLModeRequire,
AdditionalParameters: map[string]string{"param1": "Lorem ipsum"},
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require¶m1=Lorem+ipsum",
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.Name, func(t *testing.T) {
require.Equal(t, MakePostgresDSN(tt.Cfg), tt.WantDSN)
})
}
}
func TestMakePgSQLDSN(t *testing.T) {
cfg := &PostgresConfig{
Host: "myhost",
TxIsolationLevel: IsolationLevel(sql.LevelReadCommitted),
Port: 5432,
User: "myadmin",
Password: "mypassword",
Database: "mydb",
}
wantDSN := "postgres://myadmin:mypassword@myhost:5432/mydb?sslmode=verify-ca"
gotDSN := MakePostgresDSN(cfg)
require.Equal(t, wantDSN, gotDSN)
}
func TestMakeMSSQLDSN(t *testing.T) {
cfg := &MSSQLConfig{
Host: "myhost",
TxIsolationLevel: IsolationLevel(sql.LevelReadCommitted),
Port: 1433,
User: "myadmin",
Password: "mypassword",
Database: "sysdb",
}
wantDSN := "sqlserver://myadmin:mypassword@myhost:1433?database=sysdb"
gotDSN := MakeMSSQLDSN(cfg)
require.Equal(t, wantDSN, gotDSN)
}