Fixed linter errors from goreportcard

Fixed simple naming, comment conventions
Fixed pointer references to a struc where there was also a pointer. Govet complain was right.
This commit is contained in:
Alexandre Vallières-Lagacé
2018-10-29 09:17:11 -04:00
parent 60864dcdee
commit 0f12e25bc0
10 changed files with 61 additions and 53 deletions
+6 -6
View File
@@ -15,12 +15,12 @@ import (
func SetupTests() *sql.DB { // or *gorm.DB
mocket.Catcher.Register() // Safe register. Allowed multiple calls to save
// GORM
db, err := gorm.Open(mocket.DRIVER_NAME, "connection_string") // Can be any connection string
db, err := gorm.Open(mocket.DriverName, "connection_string") // Can be any connection string
DB = db
// OR
// Regular sql package usage
db, err := sql.Open(mocket.DRIVER_NAME, "connection_string")
db, err := sql.Open(mocket.DriverName, "connection_string")
return db
}
@@ -158,9 +158,9 @@ t.Run("Once", func(t *testing.T) {
})
```
### Insert ID with `.WithId(int64)`
### Insert ID with `.WithID(int64)`
In order to emulate `INSERT` requests, we can mock the ID returned from the query with the `.WithId(int64)` method.
In order to emulate `INSERT` requests, we can mock the ID returned from the query with the `.WithID(int64)` method.
```go
// Somewhere in the code
@@ -169,7 +169,7 @@ func InsertRecord(db *sql.DB) int64 {
if err != nil {
return 0
}
id, _ := res.LastInsertId()
id, _ := res.LastInsertID()
return id
}
@@ -177,7 +177,7 @@ func InsertRecord(db *sql.DB) int64 {
t.Run("Last insert id", func(t *testing.T) {
var mockedId int64
mockedId = 64
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithId(mockedId)
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithID(mockedId)
returnedId := InsertRecord(DB)
if returnedId != mockedId {
t.Fatalf("Last insert id not returned. Expected: [%v] , Got: [%v]", mockedId, returnedId)
+2 -2
View File
@@ -45,11 +45,11 @@ import (
func SetupTests() {
mocket.Catcher.Register()
// GORM
db, err := gorm.Open(mocket.DRIVER_NAME, "any_string") // Could be any connection string
db, err := gorm.Open(mocket.DriverName, "any_string") // Could be any connection string
app.DB = db // Assumption that it will be used everywhere the same
//OR
// Regular sql package usage
db, err := sql.Open(mocket.DRIVER_NAME, "any_string")
db, err := sql.Open(mocket.DriverName, "any_string")
}
```
+2 -1
View File
@@ -1,4 +1,4 @@
package go_mocket
package gomocket
import (
"context"
@@ -34,6 +34,7 @@ func (c *FakeConn) Begin() (driver.Tx, error) {
return c.currTx, nil
}
// Close terminates the db object
func (c *FakeConn) Close() (err error) {
c.db = nil
return nil
+2 -1
View File
@@ -1,3 +1,5 @@
package gomocket
/*
Package go_mocket is way to mock DB for GORM and just sql package usage
First you need to activate package somewhere in your tests code like this
@@ -80,4 +82,3 @@ Somewhere in you tests:
For more information and use cases please check: https://github.com/Selvatico/go-mocket
*/
package go_mocket
+4 -2
View File
@@ -1,4 +1,4 @@
package go_mocket
package gomocket
import (
"database/sql/driver"
@@ -18,6 +18,7 @@ type FakeDriver struct {
dbs map[string]*FakeDB
}
// FakeDB represents the database
type FakeDB struct {
name string
mu sync.Mutex
@@ -25,6 +26,7 @@ type FakeDB struct {
badConn bool
}
// table represents the table
type table struct {
mu sync.Mutex
colname []string
@@ -42,7 +44,7 @@ func (t *table) columnIndex(name string) int {
}
// Open returns a new connection to the database.
func (d FakeDriver) Open(database string) (driver.Conn, error) {
func (d *FakeDriver) Open(database string) (driver.Conn, error) {
return &FakeConn{db: d.getDB(database)}, nil
}
+9 -8
View File
@@ -1,4 +1,4 @@
package go_mocket
package gomocket
import (
"database/sql"
@@ -10,7 +10,8 @@ import (
)
const (
DRIVER_NAME = "MOCK_FAKE_DRIVER"
// DriverName is the name of the fake driver
DriverName = "MOCK_FAKE_DRIVER"
)
//Catcher is global instance of Catcher used for attaching all mocks to connection
@@ -27,11 +28,11 @@ type MockCatcher struct {
func (mc *MockCatcher) Register() {
driversList := sql.Drivers()
for _, name := range driversList {
if name == DRIVER_NAME {
if name == DriverName {
return
}
}
sql.Register(DRIVER_NAME, FakeDriver{})
sql.Register(DriverName, &FakeDriver{})
}
// Attach several mocks to MockCather. Could be useful to attach mocks from some factories of mocks
@@ -91,7 +92,7 @@ type FakeResponse struct {
Triggered bool // If it was triggered at least once
Callback func(string, []driver.NamedValue) // Callback to execute when response triggered
RowsAffected int64 // Defines affected rows count
LastInsertId int64 // ID to be returned for INSERT queries
LastInsertID int64 // ID to be returned for INSERT queries
Error error // Any type of error which could happen dur
*Exceptions
}
@@ -181,9 +182,9 @@ func (fr *FakeResponse) WithRowsNum(num int64) *FakeResponse {
return fr
}
// WithId sets ID to be considered as insert ID for INSERT statements
func (fr *FakeResponse) WithId(id int64) *FakeResponse {
fr.LastInsertId = id
// WithID sets ID to be considered as insert ID for INSERT statements
func (fr *FakeResponse) WithID(id int64) *FakeResponse {
fr.LastInsertID = id
return fr
}
+10 -10
View File
@@ -1,4 +1,4 @@
package go_mocket
package gomocket
import (
"database/sql"
@@ -54,7 +54,7 @@ func InsertRecord(db *sql.DB) int64 {
func TestResponses(t *testing.T) {
Catcher.Register()
db, _ := sql.Open(DRIVER_NAME, "connection_string") // Could be any connection string
db, _ := sql.Open(DriverName, "connection_string") // Could be any connection string
DB = db
commonReply := []map[string]interface{}{{"name": "FirstLast", "age": "30"}}
@@ -84,8 +84,8 @@ func TestResponses(t *testing.T) {
if len(result) != 1 {
t.Errorf("Returned sets is not equal to 1. Received %d", len(result))
}
if result[0]["age"] != "30" {
t.Errorf("Age is not equal. Got %v", result[0]["age"])
if result[0]["name"] != "FirstLast" {
t.Errorf("Name is not equal. Got %v", result[0]["name"])
}
})
@@ -142,12 +142,12 @@ func TestResponses(t *testing.T) {
})
t.Run("Last insert id", func(t *testing.T) {
var mockedId int64
mockedId = 64
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithId(mockedId)
returnedId := InsertRecord(DB)
if returnedId != mockedId {
t.Fatalf("Last insert id not returned. Expected: [%v] , Got: [%v]", mockedId, returnedId)
var mockedID int64
mockedID = 64
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithID(mockedID)
returnedID := InsertRecord(DB)
if returnedID != mockedID {
t.Fatalf("Last insert id not returned. Expected: [%v] , Got: [%v]", mockedID, returnedID)
}
})
+3 -3
View File
@@ -1,4 +1,4 @@
package go_mocket
package gomocket
import (
"database/sql/driver"
@@ -11,8 +11,8 @@ type FakeResult struct {
}
// NewFakeResult returns result interface instance
func NewFakeResult(insertId int64, rowsAffected int64) driver.Result {
return &FakeResult{insertId, rowsAffected}
func NewFakeResult(insertID int64, rowsAffected int64) driver.Result {
return &FakeResult{insertID, rowsAffected}
}
// LastInsertId required to give sql package ability get ID of inserted record
+2 -2
View File
@@ -1,4 +1,4 @@
package go_mocket
package gomocket
import (
"database/sql"
@@ -45,7 +45,7 @@ func (rc *RowsCursor) Columns() []string {
return rc.cols
}
// RowsColumnTypeScanType may be implemented by Rows. It should return
// ColumnTypeScanType may be implemented by Rows. It should return
// the value type that can be used to scan types into.
func (rc *RowsCursor) ColumnTypeScanType(index int) reflect.Type {
return colTypeToReflectType(rc.colType[rc.posSet][index])
+20 -17
View File
@@ -1,4 +1,4 @@
package go_mocket
package gomocket
import (
"context"
@@ -27,6 +27,7 @@ func (s *FakeStmt) ColumnConverter(idx int) driver.ValueConverter {
return driver.DefaultParameterConverter
}
// Close closes the connection
func (s *FakeStmt) Close() error {
// No connection added
if s.connection == nil {
@@ -50,18 +51,18 @@ var errClosed = errors.New("fake_db_driver: statement has been closed")
// as an INSERT or UPDATE.
//
// Deprecated: Drivers should implement StmtExecContext instead (or additionally).
func (smt *FakeStmt) Exec(args []driver.Value) (driver.Result, error) {
func (s *FakeStmt) Exec(args []driver.Value) (driver.Result, error) {
panic("Using ExecContext")
}
// ExecContext executes a query that doesn't return rows, such
// as an INSERT or UPDATE.
func (smt *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if smt.closed {
func (s *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if s.closed {
return nil, errClosed
}
fResp := Catcher.FindResponse(smt.q, args)
fResp := Catcher.FindResponse(s.q, args)
// To emulate any exception during query which returns rows
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)
}
if fResp.Callback != nil {
fResp.Callback(smt.q, args)
fResp.Callback(s.q, args)
}
switch smt.command {
switch s.command {
case "INSERT":
id := fResp.LastInsertId
id := fResp.LastInsertID
if id == 0 {
id = rand.Int63()
}
@@ -89,7 +90,7 @@ func (smt *FakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
case "DELETE":
return driver.RowsAffected(fResp.RowsAffected), nil
}
return nil, fmt.Errorf("unimplemented statement Exec command type of %q", smt.command)
return nil, fmt.Errorf("unimplemented statement Exec command type of %q", s.command)
}
// 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) {
// QueryContext executes a query that may return rows, such as a
// SELECT.
func (smt *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
func (s *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
if smt.closed {
if s.closed {
return nil, errClosed
}
if len(args) > 0 {
// Replace all "?" to "%v" and replace them with the values after
for i := 0; i < len(args); i++ {
smt.q = strings.Replace(smt.q, "?", "%v", 1)
smt.q = fmt.Sprintf(smt.q, args[i].Value)
s.q = strings.Replace(s.q, "?", "%v", 1)
s.q = fmt.Sprintf(s.q, args[i].Value)
}
}
fResp := Catcher.FindResponse(smt.q, args)
fResp := Catcher.FindResponse(s.q, args)
if fResp.Exceptions != nil && fResp.Exceptions.HookQueryBadConnection != nil && fResp.Exceptions.HookQueryBadConnection() {
return nil, driver.ErrBadConn
@@ -162,7 +163,7 @@ func (smt *FakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue)
}
if fResp.Callback != nil {
fResp.Callback(smt.q, args)
fResp.Callback(s.q, args)
}
return cursor, nil
@@ -178,9 +179,10 @@ type FakeTx struct {
c *FakeConn
}
// hook to simulate broken connections
// HookBadCommit is a hook to simulate broken connections
var HookBadCommit func() bool
// Commit commits the transaction
func (tx *FakeTx) Commit() error {
tx.c.currTx = nil
if HookBadCommit != nil && HookBadCommit() {
@@ -189,9 +191,10 @@ func (tx *FakeTx) Commit() error {
return nil
}
// hook to simulate broken connections
// HookBadRollback is a hook to simulate broken connections
var HookBadRollback func() bool
// Rollback rollbacks the transaction
func (tx *FakeTx) Rollback() error {
tx.c.currTx = nil
if HookBadRollback != nil && HookBadRollback() {