Skip to content

Commit e49dd07

Browse files
committed
Added basic failed test for makeComments with simple model with tableName tab and one field. Added testify mock.
1 parent e1acfab commit e49dd07

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

database/pg.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package database
33
import (
44
"context"
55
"fmt"
6-
76
"github.com/dipdup-net/go-lib/config"
87
pg "github.com/go-pg/pg/v10"
98
"github.com/pkg/errors"
@@ -14,7 +13,63 @@ type PgGoConnection interface {
1413
}
1514

1615
type PgDB interface {
16+
/*
17+
Begin() (*pg.Tx, error)
18+
BeginContext(ctx context.Context) (*pg.Tx, error)
19+
RunInTransaction(ctx context.Context, fn func(*pg.Tx) error) error
20+
AddQueryHook(hook pg.QueryHook)
21+
beforeQuery(ctx context.Context, ormDB orm.DB, model interface{}, query interface{}, params []interface{}, fmtedQuery []byte) (context.Context, *QueryEvent, error)
22+
afterQuery(ctx context.Context, event *pg.QueryEvent, res pg.Result, err error) error
23+
afterQueryFromIndex(ctx context.Context, event *pg.QueryEvent, hookIndex int) error
24+
//startup(c context.Context, cn *pool.Conn, user string, password string, database string, appName string) error
25+
//enableSSL(c context.Context, cn *pool.Conn, tlsConf *tls.Config) error
26+
//auth(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, user string, password string) error
27+
//logStartupNotice(rd *pool.ReaderContext) error
28+
//authCleartext(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, password string) error
29+
//authMD5(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, user string, password string) error
30+
//authSASL(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, user string, password string) error
31+
PoolStats() *pg.PoolStats
32+
//clone() *baseDB
33+
//withPool(p pool.Pooler) *baseDB
34+
//WithTimeout(d time.Duration) *baseDB
35+
//WithParam(param string, value interface{}) *baseDB
36+
Param(param string) interface{}
37+
retryBackoff(retry int) time.Duration
38+
//getConn(ctx context.Context) (*pool.Conn, error)
39+
//initConn(ctx context.Context, cn *pool.Conn) error
40+
//releaseConn(ctx context.Context, cn *pool.Conn, err error)
41+
//withConn(ctx context.Context, fn func(context.Context, *pool.Conn) error) error
42+
shouldRetry(err error) bool
43+
Close() error
44+
Exec(query interface{}, params ...interface{}) (res pg.Result, err error)
45+
*/
1746
ExecContext(c context.Context, query interface{}, params ...interface{}) (pg.Result, error)
47+
/*
48+
//exec(ctx context.Context, query interface{}, params ...interface{}) (pg.Result, error)
49+
ExecOne(query interface{}, params ...interface{}) (pg.Result, error)
50+
ExecOneContext(ctx context.Context, query interface{}, params ...interface{}) (pg.Result, error)
51+
execOne(c context.Context, query interface{}, params ...interface{}) (pg.Result, error)
52+
Query(model interface{}, query interface{}, params ...interface{}) (res pg.Result, err error)
53+
QueryContext(c context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
54+
query(ctx context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
55+
QueryOne(model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
56+
QueryOneContext(ctx context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
57+
queryOne(ctx context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
58+
CopyFrom(r io.Reader, query interface{}, params ...interface{}) (res pg.Result, err error)
59+
//copyFrom(ctx context.Context, cn *pool.Conn, r io.Reader, query interface{}, params ...interface{}) (res Result, err error)
60+
CopyTo(w io.Writer, query interface{}, params ...interface{}) (res pg.Result, err error)
61+
//copyTo(ctx context.Context, cn *pool.Conn, w io.Writer, query interface{}, params ...interface{}) (res Result, err error)
62+
Ping(ctx context.Context) error
63+
Model(model ...interface{}) *pg.Query
64+
ModelContext(c context.Context, model ...interface{}) *pg.Query
65+
Formatter() orm.QueryFormatter
66+
//cancelRequest(processID int32, secretKey int32) error
67+
//simpleQuery(c context.Context, cn *pool.Conn, wb *pool.WriteBuffer) (*result, error)
68+
//simpleQueryData(c context.Context, cn *pool.Conn, model interface{}, wb *pool.WriteBuffer) (*result, error)
69+
Prepare(q string) (*pg.Stmt, error)
70+
//prepare(c context.Context, cn *pool.Conn, q string) (string, []types.ColumnInfo, error)
71+
//closeStmt(c context.Context, cn *pool.Conn, name string) error
72+
*/
1873
}
1974

2075
// PgGo -
@@ -28,7 +83,7 @@ func NewPgGo() *PgGo {
2883
}
2984

3085
// DB -
31-
func (db *PgGo) DB() *pg.DB {
86+
func (db *PgGo) DB() PgDB {
3287
return db.conn
3388
}
3489

database/pgComment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010
)
1111

12-
func makeComments(ctx context.Context, conn *PgGo, model interface{}) error {
12+
func makeComments(ctx context.Context, conn PgGoConnection, model interface{}) error {
1313
typ := reflect.TypeOf(model)
1414

1515
// 1. go through fields

database/pgComment_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
11
package database
2+
3+
import (
4+
"context"
5+
"github.com/go-pg/pg/v10"
6+
"github.com/stretchr/testify/mock"
7+
"testing"
8+
)
9+
10+
type PgGoMock struct {
11+
conn *PgDBMock
12+
}
13+
14+
func (p *PgGoMock) DB() PgDB {
15+
return p.conn
16+
}
17+
18+
func newPgGoMock() *PgGoMock {
19+
return &PgGoMock{
20+
conn: &PgDBMock{},
21+
}
22+
}
23+
24+
type PgDBMock struct {
25+
mock.Mock
26+
}
27+
28+
func (db *PgDBMock) ExecContext(_ context.Context, query interface{}, params ...interface{}) (pg.Result, error) {
29+
args := db.Called(query, params)
30+
31+
return nil, args.Error(0)
32+
}
33+
34+
func TestMakeCommentsWithTableName(t *testing.T) {
35+
type Ballot struct {
36+
//nolint
37+
tableName struct{} `pg:"ballots"`
38+
Ballot string `json:"ballot"`
39+
}
40+
41+
pgGo := newPgGoMock()
42+
ctx := context.Background()
43+
pgGo.conn.On("ExecContext",
44+
ctx, mock.Anything, mock.Anything).Return(nil)
45+
model := Ballot{}
46+
47+
makeComments(ctx, pgGo, model)
48+
49+
// assert params of ExecContext
50+
pgGo.conn.AssertCalled(t, "ExecContext", ctx, `COMMENT ON TABLE ? IS ?`, "ballot", "")
51+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ require (
5555
github.com/prometheus/client_model v0.2.0 // indirect
5656
github.com/prometheus/common v0.26.0 // indirect
5757
github.com/prometheus/procfs v0.6.0 // indirect
58+
github.com/stretchr/objx v0.2.0 // indirect
5859
github.com/tidwall/match v1.1.1 // indirect
5960
github.com/tidwall/pretty v1.2.0 // indirect
6061
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
248248
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
249249
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
250250
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
251+
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
251252
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
252253
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
253254
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

0 commit comments

Comments
 (0)