generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsql_test.go
126 lines (118 loc) · 3.33 KB
/
sql_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
package datastore
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestClient_getSourceDatabase will test the method getSourceDatabase()
func TestClient_getSourceDatabase(t *testing.T) {
t.Run("single write db", func(t *testing.T) {
source, configs := getSourceDatabase(
[]*SQLConfig{
{
CommonConfig: CommonConfig{
Debug: true,
MaxConnectionIdleTime: 10 * time.Second,
MaxConnectionTime: 10 * time.Second,
MaxIdleConnections: 1,
MaxOpenConnections: 1,
TablePrefix: "test",
},
Driver: MySQL.String(),
Host: "host-write.domain.com",
Name: "db_name",
Password: "test",
Port: defaultMySQLPort,
Replica: false,
TimeZone: defaultTimeZone,
TxTimeout: defaultDatabaseTxTimeout,
User: "test",
},
},
)
require.NotNil(t, source)
require.Empty(t, configs)
assert.False(t, source.Replica)
assert.Equal(t, "host-write.domain.com", source.Host)
})
t.Run("read vs write", func(t *testing.T) {
source, configs := getSourceDatabase(
[]*SQLConfig{
{
CommonConfig: CommonConfig{
Debug: true,
MaxConnectionIdleTime: 10 * time.Second,
MaxConnectionTime: 10 * time.Second,
MaxIdleConnections: 1,
MaxOpenConnections: 1,
TablePrefix: "test",
},
Driver: MySQL.String(),
Host: "host-write.domain.com",
Name: "db_name",
Password: "test",
Port: defaultMySQLPort,
Replica: false,
TimeZone: defaultTimeZone,
TxTimeout: defaultDatabaseTxTimeout,
User: "test",
},
{
CommonConfig: CommonConfig{
Debug: true,
MaxConnectionIdleTime: 10 * time.Second,
MaxConnectionTime: 10 * time.Second,
MaxIdleConnections: 1,
MaxOpenConnections: 1,
TablePrefix: "test",
},
Driver: MySQL.String(),
Host: "host-read.domain.com",
Name: "db_name",
Password: "test",
Port: defaultMySQLPort,
Replica: true,
TimeZone: defaultTimeZone,
TxTimeout: defaultDatabaseTxTimeout,
User: "test",
},
},
)
require.NotNil(t, source)
assert.False(t, source.Replica)
assert.Equal(t, "host-write.domain.com", source.Host)
assert.Len(t, configs, 1)
assert.True(t, configs[0].Replica)
assert.Equal(t, "host-read.domain.com", configs[0].Host)
})
t.Run("only replica, no source found", func(t *testing.T) {
source, configs := getSourceDatabase(
[]*SQLConfig{
{
CommonConfig: CommonConfig{
Debug: true,
MaxConnectionIdleTime: 10 * time.Second,
MaxConnectionTime: 10 * time.Second,
MaxIdleConnections: 1,
MaxOpenConnections: 1,
TablePrefix: "test",
},
Driver: MySQL.String(),
Host: "host-read.domain.com",
Name: "db_name",
Password: "test",
Port: defaultMySQLPort,
Replica: true,
TimeZone: defaultTimeZone,
TxTimeout: defaultDatabaseTxTimeout,
User: "test",
},
},
)
require.Nil(t, source)
assert.Len(t, configs, 1)
assert.True(t, configs[0].Replica)
assert.Equal(t, "host-read.domain.com", configs[0].Host)
})
}