Skip to content

Commit 0d27527

Browse files
authored
Merge pull request #11 from vallieres/fix-lint-errors
Fix lint errors
2 parents b7a2ceb + 0f12e25 commit 0d27527

10 files changed

+61
-53
lines changed

DOCUMENTATION.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
func SetupTests() *sql.DB { // or *gorm.DB
1616
mocket.Catcher.Register() // Safe register. Allowed multiple calls to save
1717
// GORM
18-
db, err := gorm.Open(mocket.DRIVER_NAME, "connection_string") // Can be any connection string
18+
db, err := gorm.Open(mocket.DriverName, "connection_string") // Can be any connection string
1919
DB = db
2020

2121
// OR
2222
// Regular sql package usage
23-
db, err := sql.Open(mocket.DRIVER_NAME, "connection_string")
23+
db, err := sql.Open(mocket.DriverName, "connection_string")
2424

2525
return db
2626
}
@@ -158,9 +158,9 @@ t.Run("Once", func(t *testing.T) {
158158
})
159159
```
160160

161-
### Insert ID with `.WithId(int64)`
161+
### Insert ID with `.WithID(int64)`
162162

163-
In order to emulate `INSERT` requests, we can mock the ID returned from the query with the `.WithId(int64)` method.
163+
In order to emulate `INSERT` requests, we can mock the ID returned from the query with the `.WithID(int64)` method.
164164

165165
```go
166166
// Somewhere in the code
@@ -169,15 +169,15 @@ func InsertRecord(db *sql.DB) int64 {
169169
if err != nil {
170170
return 0
171171
}
172-
id, _ := res.LastInsertId()
172+
id, _ := res.LastInsertID()
173173
return id
174174
}
175175

176176
// Test code
177177
t.Run("Last insert id", func(t *testing.T) {
178178
var mockedId int64
179179
mockedId = 64
180-
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithId(mockedId)
180+
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithID(mockedId)
181181
returnedId := InsertRecord(DB)
182182
if returnedId != mockedId {
183183
t.Fatalf("Last insert id not returned. Expected: [%v] , Got: [%v]", mockedId, returnedId)

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ import (
4545
func SetupTests() {
4646
mocket.Catcher.Register()
4747
// GORM
48-
db, err := gorm.Open(mocket.DRIVER_NAME, "any_string") // Could be any connection string
48+
db, err := gorm.Open(mocket.DriverName, "any_string") // Could be any connection string
4949
app.DB = db // Assumption that it will be used everywhere the same
5050
//OR
5151
// Regular sql package usage
52-
db, err := sql.Open(mocket.DRIVER_NAME, "any_string")
52+
db, err := sql.Open(mocket.DriverName, "any_string")
5353
}
5454
```
5555

conn.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_mocket
1+
package gomocket
22

33
import (
44
"context"
@@ -34,6 +34,7 @@ func (c *FakeConn) Begin() (driver.Tx, error) {
3434
return c.currTx, nil
3535
}
3636

37+
// Close terminates the db object
3738
func (c *FakeConn) Close() (err error) {
3839
c.db = nil
3940
return nil

doc.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package gomocket
2+
13
/*
24
Package go_mocket is way to mock DB for GORM and just sql package usage
35
First you need to activate package somewhere in your tests code like this
@@ -80,4 +82,3 @@ Somewhere in you tests:
8082
For more information and use cases please check: https://github.com/Selvatico/go-mocket
8183
8284
*/
83-
package go_mocket

driver.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_mocket
1+
package gomocket
22

33
import (
44
"database/sql/driver"
@@ -18,13 +18,15 @@ type FakeDriver struct {
1818
dbs map[string]*FakeDB
1919
}
2020

21+
// FakeDB represents the database
2122
type FakeDB struct {
2223
name string
2324
mu sync.Mutex
2425
tables map[string]*table
2526
badConn bool
2627
}
2728

29+
// table represents the table
2830
type table struct {
2931
mu sync.Mutex
3032
colname []string
@@ -42,7 +44,7 @@ func (t *table) columnIndex(name string) int {
4244
}
4345

4446
// Open returns a new connection to the database.
45-
func (d FakeDriver) Open(database string) (driver.Conn, error) {
47+
func (d *FakeDriver) Open(database string) (driver.Conn, error) {
4648
return &FakeConn{db: d.getDB(database)}, nil
4749
}
4850

response.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_mocket
1+
package gomocket
22

33
import (
44
"database/sql"
@@ -10,7 +10,8 @@ import (
1010
)
1111

1212
const (
13-
DRIVER_NAME = "MOCK_FAKE_DRIVER"
13+
// DriverName is the name of the fake driver
14+
DriverName = "MOCK_FAKE_DRIVER"
1415
)
1516

1617
//Catcher is global instance of Catcher used for attaching all mocks to connection
@@ -27,11 +28,11 @@ type MockCatcher struct {
2728
func (mc *MockCatcher) Register() {
2829
driversList := sql.Drivers()
2930
for _, name := range driversList {
30-
if name == DRIVER_NAME {
31+
if name == DriverName {
3132
return
3233
}
3334
}
34-
sql.Register(DRIVER_NAME, FakeDriver{})
35+
sql.Register(DriverName, &FakeDriver{})
3536
}
3637

3738
// Attach several mocks to MockCather. Could be useful to attach mocks from some factories of mocks
@@ -91,7 +92,7 @@ type FakeResponse struct {
9192
Triggered bool // If it was triggered at least once
9293
Callback func(string, []driver.NamedValue) // Callback to execute when response triggered
9394
RowsAffected int64 // Defines affected rows count
94-
LastInsertId int64 // ID to be returned for INSERT queries
95+
LastInsertID int64 // ID to be returned for INSERT queries
9596
Error error // Any type of error which could happen dur
9697
*Exceptions
9798
}
@@ -181,9 +182,9 @@ func (fr *FakeResponse) WithRowsNum(num int64) *FakeResponse {
181182
return fr
182183
}
183184

184-
// WithId sets ID to be considered as insert ID for INSERT statements
185-
func (fr *FakeResponse) WithId(id int64) *FakeResponse {
186-
fr.LastInsertId = id
185+
// WithID sets ID to be considered as insert ID for INSERT statements
186+
func (fr *FakeResponse) WithID(id int64) *FakeResponse {
187+
fr.LastInsertID = id
187188
return fr
188189
}
189190

response_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_mocket
1+
package gomocket
22

33
import (
44
"database/sql"
@@ -54,7 +54,7 @@ func InsertRecord(db *sql.DB) int64 {
5454

5555
func TestResponses(t *testing.T) {
5656
Catcher.Register()
57-
db, _ := sql.Open(DRIVER_NAME, "connection_string") // Could be any connection string
57+
db, _ := sql.Open(DriverName, "connection_string") // Could be any connection string
5858
DB = db
5959
commonReply := []map[string]interface{}{{"name": "FirstLast", "age": "30"}}
6060

@@ -84,8 +84,8 @@ func TestResponses(t *testing.T) {
8484
if len(result) != 1 {
8585
t.Errorf("Returned sets is not equal to 1. Received %d", len(result))
8686
}
87-
if result[0]["age"] != "30" {
88-
t.Errorf("Age is not equal. Got %v", result[0]["age"])
87+
if result[0]["name"] != "FirstLast" {
88+
t.Errorf("Name is not equal. Got %v", result[0]["name"])
8989
}
9090
})
9191

@@ -142,12 +142,12 @@ func TestResponses(t *testing.T) {
142142
})
143143

144144
t.Run("Last insert id", func(t *testing.T) {
145-
var mockedId int64
146-
mockedId = 64
147-
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithId(mockedId)
148-
returnedId := InsertRecord(DB)
149-
if returnedId != mockedId {
150-
t.Fatalf("Last insert id not returned. Expected: [%v] , Got: [%v]", mockedId, returnedId)
145+
var mockedID int64
146+
mockedID = 64
147+
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithID(mockedID)
148+
returnedID := InsertRecord(DB)
149+
if returnedID != mockedID {
150+
t.Fatalf("Last insert id not returned. Expected: [%v] , Got: [%v]", mockedID, returnedID)
151151
}
152152
})
153153

result.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_mocket
1+
package gomocket
22

33
import (
44
"database/sql/driver"
@@ -11,16 +11,16 @@ type FakeResult struct {
1111
}
1212

1313
// NewFakeResult returns result interface instance
14-
func NewFakeResult(insertId int64, rowsAffected int64) driver.Result {
15-
return &FakeResult{insertId, rowsAffected}
14+
func NewFakeResult(insertID int64, rowsAffected int64) driver.Result {
15+
return &FakeResult{insertID, rowsAffected}
1616
}
1717

1818
// LastInsertId required to give sql package ability get ID of inserted record
1919
func (fr *FakeResult) LastInsertId() (int64, error) {
2020
return fr.insertID, nil
2121
}
2222

23-
// RowsAffected returns the number of rows affected
23+
// RowsAffected returns the number of rows affected
2424
func (fr *FakeResult) RowsAffected() (int64, error) {
2525
return fr.rowsAffected, nil
2626
}

rows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_mocket
1+
package gomocket
22

33
import (
44
"database/sql"
@@ -45,7 +45,7 @@ func (rc *RowsCursor) Columns() []string {
4545
return rc.cols
4646
}
4747

48-
// RowsColumnTypeScanType may be implemented by Rows. It should return
48+
// ColumnTypeScanType may be implemented by Rows. It should return
4949
// the value type that can be used to scan types into.
5050
func (rc *RowsCursor) ColumnTypeScanType(index int) reflect.Type {
5151
return colTypeToReflectType(rc.colType[rc.posSet][index])

stmt.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_mocket
1+
package gomocket
22

33
import (
44
"context"
@@ -27,6 +27,7 @@ func (s *FakeStmt) ColumnConverter(idx int) driver.ValueConverter {
2727
return driver.DefaultParameterConverter
2828
}
2929

30+
// Close closes the connection
3031
func (s *FakeStmt) Close() error {
3132
// No connection added
3233
if s.connection == nil {
@@ -50,18 +51,18 @@ var errClosed = errors.New("fake_db_driver: statement has been closed")
5051
// as an INSERT or UPDATE.
5152
//
5253
// Deprecated: Drivers should implement StmtExecContext instead (or additionally).
53-
func (smt *FakeStmt) Exec(args []driver.Value) (driver.Result, error) {
54+
func (s *FakeStmt) Exec(args []driver.Value) (driver.Result, error) {
5455
panic("Using ExecContext")
5556
}
5657

5758
// ExecContext executes a query that doesn't return rows, such
5859
// as an INSERT or UPDATE.
59-
func (smt *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
60-
if smt.closed {
60+
func (s *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
61+
if s.closed {
6162
return nil, errClosed
6263
}
6364

64-
fResp := Catcher.FindResponse(smt.q, args)
65+
fResp := Catcher.FindResponse(s.q, args)
6566

6667
// To emulate any exception during query which returns rows
6768
if fResp.Exceptions != nil && fResp.Exceptions.HookExecBadConnection != nil && fResp.Exceptions.HookExecBadConnection() {
@@ -73,12 +74,12 @@ func (smt *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
7374
}
7475

7576
if fResp.Callback != nil {
76-
fResp.Callback(smt.q, args)
77+
fResp.Callback(s.q, args)
7778
}
7879

79-
switch smt.command {
80+
switch s.command {
8081
case "INSERT":
81-
id := fResp.LastInsertId
82+
id := fResp.LastInsertID
8283
if id == 0 {
8384
id = rand.Int63()
8485
}
@@ -89,7 +90,7 @@ func (smt *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
8990
case "DELETE":
9091
return driver.RowsAffected(fResp.RowsAffected), nil
9192
}
92-
return nil, fmt.Errorf("unimplemented statement Exec command type of %q", smt.command)
93+
return nil, fmt.Errorf("unimplemented statement Exec command type of %q", s.command)
9394
}
9495

9596
// Query executes a query that may return rows, such as a
@@ -102,21 +103,21 @@ func (s *FakeStmt) Query(args []driver.Value) (driver.Rows, error) {
102103

103104
// QueryContext executes a query that may return rows, such as a
104105
// SELECT.
105-
func (smt *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
106+
func (s *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
106107

107-
if smt.closed {
108+
if s.closed {
108109
return nil, errClosed
109110
}
110111

111112
if len(args) > 0 {
112113
// Replace all "?" to "%v" and replace them with the values after
113114
for i := 0; i < len(args); i++ {
114-
smt.q = strings.Replace(smt.q, "?", "%v", 1)
115-
smt.q = fmt.Sprintf(smt.q, args[i].Value)
115+
s.q = strings.Replace(s.q, "?", "%v", 1)
116+
s.q = fmt.Sprintf(s.q, args[i].Value)
116117
}
117118
}
118119

119-
fResp := Catcher.FindResponse(smt.q, args)
120+
fResp := Catcher.FindResponse(s.q, args)
120121

121122
if fResp.Exceptions != nil && fResp.Exceptions.HookQueryBadConnection != nil && fResp.Exceptions.HookQueryBadConnection() {
122123
return nil, driver.ErrBadConn
@@ -162,7 +163,7 @@ func (smt *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue)
162163
}
163164

164165
if fResp.Callback != nil {
165-
fResp.Callback(smt.q, args)
166+
fResp.Callback(s.q, args)
166167
}
167168

168169
return cursor, nil
@@ -178,9 +179,10 @@ type FakeTx struct {
178179
c *FakeConn
179180
}
180181

181-
// hook to simulate broken connections
182+
// HookBadCommit is a hook to simulate broken connections
182183
var HookBadCommit func() bool
183184

185+
// Commit commits the transaction
184186
func (tx *FakeTx) Commit() error {
185187
tx.c.currTx = nil
186188
if HookBadCommit != nil && HookBadCommit() {
@@ -189,9 +191,10 @@ func (tx *FakeTx) Commit() error {
189191
return nil
190192
}
191193

192-
// hook to simulate broken connections
194+
// HookBadRollback is a hook to simulate broken connections
193195
var HookBadRollback func() bool
194196

197+
// Rollback rollbacks the transaction
195198
func (tx *FakeTx) Rollback() error {
196199
tx.c.currTx = nil
197200
if HookBadRollback != nil && HookBadRollback() {

0 commit comments

Comments
 (0)