File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change 55require (
66 github.com/go-pg/pg/v10 v10.9.0
77 github.com/go-redis/redis/v8 v8.8.0
8+ github.com/stretchr/testify v1.7.0
89)
Original file line number Diff line number Diff line change 1+ package pgdb
2+
3+ import (
4+ "fmt"
5+ "github.com/go-pg/pg/v10"
6+ "github.com/stretchr/testify/assert"
7+ "testing"
8+ )
9+
10+ var pgOptions = & pg.Options {
11+ Addr : "localhost:5432" ,
12+ User : "postgres" ,
13+ Password : "postgres" ,
14+ Database : "pg-db-go" ,
15+ }
16+
17+ func TestNewPgDB_WhenOptionsOk_ThenCreateClient (t * testing.T ) {
18+ got := NewPgDB (pgOptions )
19+ got .Close ()
20+
21+ assert .NotNil (t , got )
22+ assert .Equal (t , pgOptions .Addr , got .Options ().Addr )
23+ assert .Equal (t , pgOptions .User , got .Options ().User )
24+ assert .Equal (t , pgOptions .Password , got .Options ().Password )
25+ assert .Equal (t , pgOptions .Database , got .Options ().Database )
26+ }
27+
28+ func TestNewPgDB_WhenOptionsNil_ThenPanic (t * testing.T ) {
29+ defer func () {
30+ if r := recover (); r != nil {
31+ assert .Equal (t , fmt .Sprint ("runtime error: invalid memory address or nil pointer dereference" ), fmt .Sprint (r ))
32+ }
33+ }()
34+ NewPgDB (nil )
35+ }
36+
37+ func TestNewPgDB_WhenOptionsContainBadAddr_ThenPanic (t * testing.T ) {
38+ defer func () {
39+ if r := recover (); r != nil {
40+ assert .Equal (t , fmt .Sprintln ("Postgres connection error dial tcp: lookup bad: no such host" ), fmt .Sprint (r ))
41+ }
42+ }()
43+ NewPgDB (& pg.Options {
44+ Addr : "bad:6379" ,
45+ User : "postgres" ,
46+ Password : "postgres" ,
47+ Database : "pg-db-go" ,
48+ })
49+ }
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ func TestNewRedisDB_WhenOptionsNil_ThenPanic(t *testing.T) {
3535func TestNewRedisDB_WhenOptionsContainBadAddr_ThenPanic (t * testing.T ) {
3636 defer func () {
3737 if r := recover (); r != nil {
38- assert .Equal (t , fmt .Sprintln ("Redis connection error dial tcp: lookup awdawdaw : no such host" ), fmt .Sprint (r ))
38+ assert .Equal (t , fmt .Sprintln ("Redis connection error dial tcp: lookup bad : no such host" ), fmt .Sprint (r ))
3939 }
4040 }()
4141 NewRedisDB (& redis.Options {
You can’t perform that action at this time.
0 commit comments