From 0f12e25bc0f93cbc61aeb2fb185800bfc417ecf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Valli=C3=A8res-Lagac=C3=A9?= Date: Fri, 26 Oct 2018 09:14:39 -0400 Subject: [PATCH] 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. --- DOCUMENTATION.md | 12 ++++++------ README.md | 4 ++-- conn.go | 3 ++- doc.go | 3 ++- driver.go | 6 ++++-- response.go | 17 +++++++++-------- response_test.go | 20 ++++++++++---------- result.go | 8 ++++---- rows.go | 4 ++-- stmt.go | 37 ++++++++++++++++++++----------------- 10 files changed, 61 insertions(+), 53 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 48cbc3a..607b86c 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -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) diff --git a/README.md b/README.md index 76835a0..f7f1f94 100644 --- a/README.md +++ b/README.md @@ -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") } ``` diff --git a/conn.go b/conn.go index 41bad8d..3f5fb3d 100644 --- a/conn.go +++ b/conn.go @@ -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 diff --git a/doc.go b/doc.go index 36f79a3..5a006d1 100644 --- a/doc.go +++ b/doc.go @@ -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 diff --git a/driver.go b/driver.go index d09c7d6..680bf26 100644 --- a/driver.go +++ b/driver.go @@ -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 } diff --git a/response.go b/response.go index 086134b..1b51a30 100644 --- a/response.go +++ b/response.go @@ -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 } diff --git a/response_test.go b/response_test.go index 5d0da27..dcc83b6 100644 --- a/response_test.go +++ b/response_test.go @@ -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) } }) diff --git a/result.go b/result.go index 16648bd..81e6923 100644 --- a/result.go +++ b/result.go @@ -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 @@ -20,7 +20,7 @@ func (fr *FakeResult) LastInsertId() (int64, error) { return fr.insertID, nil } -// RowsAffected returns the number of rows affected +// RowsAffected returns the number of rows affected func (fr *FakeResult) RowsAffected() (int64, error) { return fr.rowsAffected, nil } diff --git a/rows.go b/rows.go index 9582877..28c7526 100644 --- a/rows.go +++ b/rows.go @@ -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]) diff --git a/stmt.go b/stmt.go index cea5d31..cfac6bd 100644 --- a/stmt.go +++ b/stmt.go @@ -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() {