-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_test.go
89 lines (75 loc) · 3.74 KB
/
mysql_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
package mysql
import (
"github.com/go-gdbc/gdbc"
"github.com/stretchr/testify/assert"
"testing"
)
func getDSN(t *testing.T, dataSourceUrl string) (string, error) {
adapter := gdbc.GetDataSourceNameAdapter("mysql")
dataSource, err := gdbc.GetDataSource(dataSourceUrl)
assert.Nil(t, err)
return adapter.GetDataSourceName(dataSource)
}
func getDSNWithUser(t *testing.T, dataSourceUrl string, username string, password string) (string, error) {
adapter := gdbc.GetDataSourceNameAdapter("mysql")
dataSource, err := gdbc.GetDataSource(dataSourceUrl, gdbc.Username(username), gdbc.Password(password))
assert.Nil(t, err)
return adapter.GetDataSourceName(dataSource)
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithWrongDatabaseName(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql://localhost:3000/testdb/wrong?charset=utf8mb4")
assert.NotNil(t, err)
assert.Equal(t, "database name format is wrong : testdb/wrong", err.Error())
assert.Empty(t, dsn)
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithoutUser(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql://localhost:3000/testdb?charset=utf8mb4")
assert.Nil(t, err)
assert.Equal(t, dsn, DefaultUsername+"@tcp(localhost:3000)/testdb?charset=utf8mb4")
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithoutArguments(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql://localhost:3000/testdb")
assert.Nil(t, err)
assert.Equal(t, dsn, DefaultUsername+"@tcp(localhost:3000)/testdb")
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithoutUserAndPort(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql://localhost/testdb?charset=utf8mb4")
assert.Nil(t, err)
assert.Equal(t, dsn, DefaultUsername+"@tcp(localhost:"+DefaultPort+")/testdb?charset=utf8mb4")
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithoutUserAndHostAndPort(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql:testdb?charset=utf8mb4")
assert.Nil(t, err)
assert.Equal(t, dsn, DefaultUsername+"@tcp("+DefaultHost+":"+DefaultPort+")/testdb?charset=utf8mb4")
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithoutUserAndHostAndPortAndDatabase(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql:?charset=utf8mb4")
assert.Nil(t, err)
assert.Equal(t, dsn, DefaultUsername+"@tcp("+DefaultHost+":"+DefaultPort+")/?charset=utf8mb4")
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithFullFormat(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql://username:password@localhost:3000/testdb?charset=utf8mb4")
assert.Nil(t, err)
assert.Equal(t, dsn, "username:password@tcp(localhost:3000)/testdb?charset=utf8mb4")
}
func TestPostgresDataSourceNameAdapter_GetDataSourceNameWithSocket(t *testing.T) {
dsn, err := getDSN(t, "gdbc:mysql:/testdb?socket=/tmp/mysql.sock&charset=utf8mb4")
assert.Nil(t, err)
assert.Equal(t, dsn, "unix(/tmp/mysql.sock)/testdb?charset=utf8mb4")
}
func TestPostgresDataSourceNameAdapter_GetDataSourceNameWithSocketUsingUserAndPassword(t *testing.T) {
dsn, err := getDSNWithUser(t, "gdbc:mysql:/testdb?socket=/tmp/mysql.sock&charset=utf8mb4", "username", "password")
assert.Nil(t, err)
assert.Equal(t, dsn, "username:password@unix(/tmp/mysql.sock)/testdb?charset=utf8mb4")
}
func TestPostgresDataSourceNameAdapter_GetDataSourceNameWithSocketUsingUser(t *testing.T) {
dsn, err := getDSNWithUser(t, "gdbc:mysql:/testdb?socket=/tmp/mysql.sock&charset=utf8mb4", "username", "")
assert.Nil(t, err)
assert.Equal(t, dsn, "username@unix(/tmp/mysql.sock)/testdb?charset=utf8mb4")
}
func TestMySQLDataSourceNameAdapter_GetDataSourceNameWithoutUsernameUsingSocket(t *testing.T) {
dsn, err := getDSNWithUser(t, "gdbc:mysql:/testdb?socket=/tmp/mysql.sock&charset=utf8mb4", "", "test")
assert.NotNil(t, err)
assert.Equal(t, "user must not be empty when a password is specified", err.Error())
assert.Empty(t, dsn)
}