/
data_source_test.go
101 lines (84 loc) · 1.92 KB
/
data_source_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
package cyqldog
import (
"strings"
"testing"
)
func TestGetDataSourceName(t *testing.T) {
cases := []struct {
driver string
ok bool
}{
{
driver: "postgres",
ok: true,
},
{
driver: "mysql",
ok: true,
},
{
driver: "unknown",
ok: false,
},
}
for _, tc := range cases {
s := &DataSourceConfig{
Driver: tc.driver,
}
_, err := s.getDataSourceName()
if tc.ok && err != nil {
t.Errorf("getDataSourceName() with driver = %s returns unexpected error: %+v", tc.driver, err)
}
if !tc.ok && err == nil {
t.Errorf("expected getDataSourceName() with driver = %s returns error, but err == nil", tc.driver)
}
}
}
func TestGetDataSourceNamePostgres(t *testing.T) {
s := DataSourceConfig{
Driver: "postgres",
Options: DataSourceOptions{
"host": "db.example.com",
"port": "5432",
"user": "cyqldog",
"password": "secret",
"dbname": "cyqldogdb",
"sslmode": "disable",
},
}
out, _ := s.getDataSourceNamePostgres()
// A map interation order is random, so the output is unstable.
// We only check if the key=value is included in the output.
for k, v := range s.Options {
if ok := strings.Contains(out, k+"="+v); !ok {
t.Errorf("getDataSourceNamePostgres() does not contains %s=%s", k, v)
}
}
}
func TestGetDataSourceNameMySQL(t *testing.T) {
cases := []struct {
options DataSourceOptions
out string
}{
{
options: DataSourceOptions{
"host": "db.example.com",
"port": "3306",
"user": "cyqldog",
"password": "secret",
"dbname": "cyqldogdb",
},
out: "cyqldog:secret@tcp(db.example.com:3306)/cyqldogdb",
},
}
for _, tc := range cases {
s := DataSourceConfig{
Driver: "mysql",
Options: tc.options,
}
got, _ := s.getDataSourceNameMySQL()
if got != tc.out {
t.Errorf("getDataSourceNameMySQL() with options = %v returns %s, but want = %s", tc.options, got, tc.out)
}
}
}