Skip to content

Commit 22c4fd4

Browse files
committed
Add DSN test
1 parent 81a5049 commit 22c4fd4

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

utils_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2013 Julien Schmidt. All rights reserved.
4+
// http://www.julienschmidt.com
5+
//
6+
// This Source Code Form is subject to the terms of the Mozilla Public
7+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
8+
// You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
package mysql
11+
12+
import (
13+
"fmt"
14+
"testing"
15+
)
16+
17+
var testDSNs = []struct {
18+
in string
19+
out string
20+
}{
21+
{"username:password@protocol(address)/dbname?param=value", "&{user:username passwd:password net:protocol addr:address dbname:dbname params:map[param:value]}"},
22+
{"user@unix(/path/to/socket)/dbname?charset=utf8", "&{user:user passwd: net:unix addr:/path/to/socket dbname:dbname params:map[charset:utf8]}"},
23+
{"user:password@tcp(localhost:5555)/dbname?charset=utf8", "&{user:user passwd:password net:tcp addr:localhost:5555 dbname:dbname params:map[charset:utf8]}"},
24+
{"user:password@/dbname", "&{user:user passwd:password net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[]}"},
25+
{"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname", "&{user:user passwd:p@ss(word) net:tcp addr:[de:ad:be:ef::ca:fe]:80 dbname:dbname params:map[]}"},
26+
{"/dbname", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[]}"},
27+
{"/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[]}"},
28+
{"user:p@/ssword@/", "&{user:user passwd:p@/ssword net:tcp addr:127.0.0.1:3306 dbname: params:map[]}"},
29+
}
30+
31+
func TestDSNParser(t *testing.T) {
32+
var cfg *config
33+
var res string
34+
35+
for i, tst := range testDSNs {
36+
cfg = parseDSN(tst.in)
37+
res = fmt.Sprintf("%+v", cfg)
38+
if res != tst.out {
39+
t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, tst.out)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)