Skip to content

Commit e03c024

Browse files
author
c9845
committed
Fix bug that prevented New... funcs from working, db name wasn't being set. Add tests to catch this in the future.
1 parent a37c02d commit e03c024

File tree

6 files changed

+117
-5
lines changed

6 files changed

+117
-5
lines changed

mariadb_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestNewMariaDB(t *testing.T) {
1010

1111
c := NewMariaDB(host, dbName, user, password)
1212
if c.Type != DBTypeMariaDB {
13-
t.FailNow()
13+
t.Fatal("wrong db type", c.Type)
1414
return
1515
}
1616

mssql.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func NewMSSQL(host, dbName, user, password string) *Config {
1010
c.Type = DBTypeMSSQL
1111
c.Host = host
1212
c.Port = defaultMSSQLPort
13+
c.Name = dbName
1314
c.User = user
1415
c.Password = password
1516

mssql_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,35 @@ package sqldb
33
import "testing"
44

55
func TestNewMSSQL(t *testing.T) {
6-
c := NewMSSQL("10.0.0.1", "db_name", "user1", "password1")
6+
host := "10.0.0.1"
7+
dbName := "db_name"
8+
user := "user"
9+
password := "password"
10+
11+
c := NewMSSQL(host, dbName, user, password)
712
if c.Type != DBTypeMSSQL {
8-
t.FailNow()
13+
t.Fatal("wrong db type", c.Type)
14+
return
15+
}
16+
17+
if c.Host != host {
18+
t.Fatal("host does not match", c.Host, host)
19+
return
20+
}
21+
if c.Port != defaultMSSQLPort {
22+
t.Fatal("default port not set")
23+
return
24+
}
25+
if c.Name != dbName {
26+
t.Fatal("db name does not match", c.Name, dbName)
27+
return
28+
}
29+
if c.User != user {
30+
t.Fatal("user does not match", c.User, user)
31+
return
32+
}
33+
if c.Password != password {
34+
t.Fatal("host does not match", c.Password, password)
935
return
1036
}
1137
}

mysql.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func NewMySQL(host, dbName, user, password string) *Config {
1010
c.Type = DBTypeMySQL
1111
c.Host = host
1212
c.Port = defaultMySQLPort
13+
c.Name = dbName
1314
c.User = user
1415
c.Password = password
1516

mysql_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,35 @@ package sqldb
33
import "testing"
44

55
func TestNewMySQL(t *testing.T) {
6-
c := NewMySQL("10.0.0.1", "db_name", "user1", "password1")
6+
host := "10.0.0.1"
7+
dbName := "db_name"
8+
user := "user"
9+
password := "password"
10+
11+
c := NewMySQL(host, dbName, user, password)
712
if c.Type != DBTypeMySQL {
8-
t.FailNow()
13+
t.Fatal("wrong db type", c.Type)
14+
return
15+
}
16+
17+
if c.Host != host {
18+
t.Fatal("host does not match", c.Host, host)
19+
return
20+
}
21+
if c.Port != defaultMySQLPort {
22+
t.Fatal("default port not set")
23+
return
24+
}
25+
if c.Name != dbName {
26+
t.Fatal("db name does not match", c.Name, dbName)
27+
return
28+
}
29+
if c.User != user {
30+
t.Fatal("user does not match", c.User, user)
31+
return
32+
}
33+
if c.Password != password {
34+
t.Fatal("host does not match", c.Password, password)
935
return
1036
}
1137
}

sqldb_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ func TestConnect(t *testing.T) {
115115
t.Fatal("Connection not showing connected as it should!")
116116
return
117117
}
118+
119+
//Test with a bad path.
120+
c.Close()
121+
c.SQLitePath = "/path/is/bad.db"
122+
err = c.Connect()
123+
if err == nil {
124+
t.Fatal("error should have occured because path does not exist.")
125+
return
126+
}
118127
}
119128

120129
func TestDefaultMapperFunc(t *testing.T) {
@@ -198,6 +207,42 @@ func TestValidate(t *testing.T) {
198207
t.Fatal("Error about bad db type should have occured in validate.")
199208
return
200209
}
210+
211+
//Test using New... config builders.
212+
host := "10.0.0.1"
213+
dbName := "db_name"
214+
user := "user"
215+
password := "password"
216+
cfg := NewMariaDB(host, dbName, user, password)
217+
if err = cfg.validate(); err != nil {
218+
t.Fatal(err)
219+
return
220+
}
221+
222+
cfg = NewMySQL(host, dbName, user, password)
223+
if err = cfg.validate(); err != nil {
224+
t.Fatal(err)
225+
return
226+
}
227+
228+
cfg = NewMSSQL(host, dbName, user, password)
229+
if err = cfg.validate(); err != nil {
230+
t.Fatal(err)
231+
return
232+
}
233+
234+
cfg = NewSQLite("/path/to/sqlite.db")
235+
if err = cfg.validate(); err != nil {
236+
t.Fatal(err)
237+
return
238+
}
239+
240+
//Bad logging level.
241+
cfg.LoggingLevel = -100
242+
if err = cfg.validate(); err != nil {
243+
t.Fatal(err)
244+
return
245+
}
201246
}
202247

203248
func TestBuildConnectionString(t *testing.T) {
@@ -433,6 +478,19 @@ func TestAddConnectionOption(t *testing.T) {
433478
t.Fatal("Connection option value mismatch.", vFound, v)
434479
return
435480
}
481+
482+
//Make sure blank map is populated if map doesn't exist.
483+
c.ConnectionOptions = nil
484+
c.AddConnectionOption(k, v)
485+
vFound, ok = c.ConnectionOptions[k]
486+
if !ok {
487+
t.Fatal("Could not find key in connection options.")
488+
return
489+
}
490+
if vFound != v {
491+
t.Fatal("Connection option value mismatch.", vFound, v)
492+
return
493+
}
436494
}
437495

438496
func TestType(t *testing.T) {

0 commit comments

Comments
 (0)