Skip to content

Commit 4d2c855

Browse files
Lairon Acosta GuardiasLairon Acosta Guardias
authored andcommitted
feat: implementing unit test for pg db client
1 parent fd61f04 commit 4d2c855

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.16
55
require (
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
)

pgdb/pg_db_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

redisdb/redis_db_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestNewRedisDB_WhenOptionsNil_ThenPanic(t *testing.T) {
3535
func 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{

0 commit comments

Comments
 (0)