You've already forked go-sqlmock
							
							
				mirror of
				https://github.com/DATA-DOG/go-sqlmock.git
				synced 2025-10-30 23:47:46 +02:00 
			
		
		
		
	documentation update based on lint
This commit is contained in:
		| @@ -1,10 +1,11 @@ | ||||
| [](https://travis-ci.org/DATA-DOG/go-sqlmock) | ||||
| [](https://godoc.org/github.com/DATA-DOG/go-sqlmock) | ||||
|  | ||||
| # Sql driver mock for Golang | ||||
|  | ||||
| This is a **mock** driver as **database/sql/driver** which is very flexible and pragmatic to | ||||
| manage and mock expected queries. All the expectations should be met and all queries and actions | ||||
| triggered should be mocked in order to pass a test. See exported [api on godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock) | ||||
| triggered should be mocked in order to pass a test. | ||||
|  | ||||
| ## Install | ||||
|  | ||||
| @@ -289,7 +290,7 @@ to compare them correctly, this may be improved. | ||||
|  | ||||
| ## Documentation | ||||
|  | ||||
| See it on [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock) | ||||
| Visit [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock) | ||||
|  | ||||
| ## TODO | ||||
|  | ||||
|   | ||||
| @@ -10,13 +10,13 @@ type conn struct { | ||||
| 	active       expectation | ||||
| } | ||||
|  | ||||
| // closes a mock database driver connection. It should | ||||
| // Close a mock database driver connection. It should | ||||
| // be always called to ensure that all expectations | ||||
| // were met successfully | ||||
| // were met successfully. Returns error if there is any | ||||
| func (c *conn) Close() (err error) { | ||||
| 	for _, e := range mock.conn.expectations { | ||||
| 		if !e.fulfilled() { | ||||
| 			err = fmt.Errorf("There is a remaining expectation %T which was not matched yet", e) | ||||
| 			err = fmt.Errorf("there is a remaining expectation %T which was not matched yet", e) | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
| @@ -28,12 +28,12 @@ func (c *conn) Close() (err error) { | ||||
| func (c *conn) Begin() (driver.Tx, error) { | ||||
| 	e := c.next() | ||||
| 	if e == nil { | ||||
| 		return nil, fmt.Errorf("All expectations were already fulfilled, call to Begin transaction was not expected") | ||||
| 		return nil, fmt.Errorf("all expectations were already fulfilled, call to begin transaction was not expected") | ||||
| 	} | ||||
|  | ||||
| 	etb, ok := e.(*expectedBegin) | ||||
| 	if !ok { | ||||
| 		return nil, fmt.Errorf("Call to Begin transaction, was not expected, next expectation is %T as %+v", e, e) | ||||
| 		return nil, fmt.Errorf("call to begin transaction, was not expected, next expectation is %T as %+v", e, e) | ||||
| 	} | ||||
| 	etb.triggered = true | ||||
| 	return &transaction{c}, etb.err | ||||
| @@ -53,12 +53,12 @@ func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) { | ||||
| 	e := c.next() | ||||
| 	query = stripQuery(query) | ||||
| 	if e == nil { | ||||
| 		return nil, fmt.Errorf("All expectations were already fulfilled, call to Exec '%s' query with args %+v was not expected", query, args) | ||||
| 		return nil, fmt.Errorf("all expectations were already fulfilled, call to exec '%s' query with args %+v was not expected", query, args) | ||||
| 	} | ||||
|  | ||||
| 	eq, ok := e.(*expectedExec) | ||||
| 	if !ok { | ||||
| 		return nil, fmt.Errorf("Call to Exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) | ||||
| 		return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) | ||||
| 	} | ||||
|  | ||||
| 	eq.triggered = true | ||||
| @@ -67,15 +67,15 @@ func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) { | ||||
| 	} | ||||
|  | ||||
| 	if eq.result == nil { | ||||
| 		return nil, fmt.Errorf("Exec query '%s' with args %+v, must return a database/sql/driver.Result, but it was not set for expectation %T as %+v", query, args, eq, eq) | ||||
| 		return nil, fmt.Errorf("exec query '%s' with args %+v, must return a database/sql/driver.result, but it was not set for expectation %T as %+v", query, args, eq, eq) | ||||
| 	} | ||||
|  | ||||
| 	if !eq.queryMatches(query) { | ||||
| 		return nil, fmt.Errorf("Exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String()) | ||||
| 		return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String()) | ||||
| 	} | ||||
|  | ||||
| 	if !eq.argsMatches(args) { | ||||
| 		return nil, fmt.Errorf("Exec query '%s', args %+v does not match expected %+v", query, args, eq.args) | ||||
| 		return nil, fmt.Errorf("exec query '%s', args %+v does not match expected %+v", query, args, eq.args) | ||||
| 	} | ||||
|  | ||||
| 	return eq.result, nil | ||||
| @@ -89,12 +89,12 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) { | ||||
| 	e := c.next() | ||||
| 	query = stripQuery(query) | ||||
| 	if e == nil { | ||||
| 		return nil, fmt.Errorf("All expectations were already fulfilled, call to Query '%s' with args %+v was not expected", query, args) | ||||
| 		return nil, fmt.Errorf("all expectations were already fulfilled, call to query '%s' with args %+v was not expected", query, args) | ||||
| 	} | ||||
|  | ||||
| 	eq, ok := e.(*expectedQuery) | ||||
| 	if !ok { | ||||
| 		return nil, fmt.Errorf("Call to Query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) | ||||
| 		return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) | ||||
| 	} | ||||
|  | ||||
| 	eq.triggered = true | ||||
| @@ -103,15 +103,15 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) { | ||||
| 	} | ||||
|  | ||||
| 	if eq.rows == nil { | ||||
| 		return nil, fmt.Errorf("Query '%s' with args %+v, must return a database/sql/driver.Rows, but it was not set for expectation %T as %+v", query, args, eq, eq) | ||||
| 		return nil, fmt.Errorf("query '%s' with args %+v, must return a database/sql/driver.rows, but it was not set for expectation %T as %+v", query, args, eq, eq) | ||||
| 	} | ||||
|  | ||||
| 	if !eq.queryMatches(query) { | ||||
| 		return nil, fmt.Errorf("Query '%s', does not match regex [%s]", query, eq.sqlRegex.String()) | ||||
| 		return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, eq.sqlRegex.String()) | ||||
| 	} | ||||
|  | ||||
| 	if !eq.argsMatches(args) { | ||||
| 		return nil, fmt.Errorf("Query '%s', args %+v does not match expected %+v", query, args, eq.args) | ||||
| 		return nil, fmt.Errorf("query '%s', args %+v does not match expected %+v", query, args, eq.args) | ||||
| 	} | ||||
|  | ||||
| 	return eq.rows, nil | ||||
|   | ||||
| @@ -13,22 +13,22 @@ func TestQueryExpectationArgComparison(t *testing.T) { | ||||
| 	against := []driver.Value{5} | ||||
|  | ||||
| 	if e.argsMatches(against) { | ||||
| 		t.Error("Arguments should not match, since the size is not the same") | ||||
| 		t.Error("arguments should not match, since the size is not the same") | ||||
| 	} | ||||
|  | ||||
| 	against = []driver.Value{3, "str"} | ||||
| 	if e.argsMatches(against) { | ||||
| 		t.Error("Arguments should not match, since the first argument (int value) is different") | ||||
| 		t.Error("arguments should not match, since the first argument (int value) is different") | ||||
| 	} | ||||
|  | ||||
| 	against = []driver.Value{5, "st"} | ||||
| 	if e.argsMatches(against) { | ||||
| 		t.Error("Arguments should not match, since the second argument (string value) is different") | ||||
| 		t.Error("arguments should not match, since the second argument (string value) is different") | ||||
| 	} | ||||
|  | ||||
| 	against = []driver.Value{5, "str"} | ||||
| 	if !e.argsMatches(against) { | ||||
| 		t.Error("Arguments should match, but it did not") | ||||
| 		t.Error("arguments should match, but it did not") | ||||
| 	} | ||||
|  | ||||
| 	e.args = []driver.Value{5, time.Now()} | ||||
| @@ -38,6 +38,6 @@ func TestQueryExpectationArgComparison(t *testing.T) { | ||||
|  | ||||
| 	against = []driver.Value{5, tm} | ||||
| 	if !e.argsMatches(against) { | ||||
| 		t.Error("Arguments should match (time will be compared only by type), but it did not") | ||||
| 		t.Error("arguments should match (time will be compared only by type), but it did not") | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										27
									
								
								result.go
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								result.go
									
									
									
									
									
								
							| @@ -1,27 +1,30 @@ | ||||
| package sqlmock | ||||
|  | ||||
| import ( | ||||
|     "database/sql/driver" | ||||
| ) | ||||
|  | ||||
| // Result satisfies sql driver Result, which | ||||
| // holds last insert id and rows affected | ||||
| // by Exec queries | ||||
| type Result struct { | ||||
| 	lastInsertId int64 | ||||
| type result struct { | ||||
| 	insertID int64 | ||||
| 	rowsAffected int64 | ||||
| } | ||||
|  | ||||
| // Creates a new Result for Exec based query mocks | ||||
| func NewResult(lastInsertId int64, rowsAffected int64) *Result { | ||||
| 	return &Result{ | ||||
| 		lastInsertId, | ||||
| // NewResult creates a new sql driver Result | ||||
| // for Exec based query mocks. | ||||
| func NewResult(lastInsertID int64, rowsAffected int64) driver.Result { | ||||
| 	return &result{ | ||||
| 		lastInsertID, | ||||
| 		rowsAffected, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Retrieve last inserted id | ||||
| func (res *Result) LastInsertId() (int64, error) { | ||||
| 	return res.lastInsertId, nil | ||||
| func (r *result) LastInsertId() (int64, error) { | ||||
| 	return r.insertID, nil | ||||
| } | ||||
|  | ||||
| // Retrieve number rows affected | ||||
| func (res *Result) RowsAffected() (int64, error) { | ||||
| 	return res.rowsAffected, nil | ||||
| func (r *result) RowsAffected() (int64, error) { | ||||
| 	return r.rowsAffected, nil | ||||
| } | ||||
|   | ||||
							
								
								
									
										4
									
								
								rows.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								rows.go
									
									
									
									
									
								
							| @@ -40,8 +40,8 @@ func (r *rows) Next(dest []driver.Value) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Create Rows from CSV string | ||||
| // to be used for mocked queries. Satisfies sql driver Rows interface | ||||
| // RowsFromCSVString creates Rows from CSV string | ||||
| // to be used for mocked queries. Returns sql driver Rows interface | ||||
| func RowsFromCSVString(columns []string, s string) driver.Rows { | ||||
| 	rs := &rows{} | ||||
| 	rs.cols = columns | ||||
|   | ||||
							
								
								
									
										24
									
								
								sqlmock.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								sqlmock.go
									
									
									
									
									
								
							| @@ -85,7 +85,7 @@ func init() { | ||||
| 	sql.Register("mock", mock) | ||||
| } | ||||
|  | ||||
| // Expect transaction to be started | ||||
| // ExpectBegin expects transaction to be started | ||||
| func ExpectBegin() Mock { | ||||
| 	e := &expectedBegin{} | ||||
| 	mock.conn.expectations = append(mock.conn.expectations, e) | ||||
| @@ -93,7 +93,7 @@ func ExpectBegin() Mock { | ||||
| 	return mock.conn | ||||
| } | ||||
|  | ||||
| // Expect transaction to be commited | ||||
| // ExpectCommit expects transaction to be commited | ||||
| func ExpectCommit() Mock { | ||||
| 	e := &expectedCommit{} | ||||
| 	mock.conn.expectations = append(mock.conn.expectations, e) | ||||
| @@ -101,7 +101,7 @@ func ExpectCommit() Mock { | ||||
| 	return mock.conn | ||||
| } | ||||
|  | ||||
| // Expect transaction to be rolled back | ||||
| // ExpectRollback expects transaction to be rolled back | ||||
| func ExpectRollback() Mock { | ||||
| 	e := &expectedRollback{} | ||||
| 	mock.conn.expectations = append(mock.conn.expectations, e) | ||||
| @@ -109,13 +109,13 @@ func ExpectRollback() Mock { | ||||
| 	return mock.conn | ||||
| } | ||||
|  | ||||
| // The expectation will return an error | ||||
| // WillReturnError the expectation will return an error | ||||
| func (c *conn) WillReturnError(err error) Mock { | ||||
| 	c.active.setError(err) | ||||
| 	return c | ||||
| } | ||||
|  | ||||
| // Expect database Exec to be triggered, which will match | ||||
| // ExpectExec expects database Exec to be triggered, which will match | ||||
| // the given query string as a regular expression | ||||
| func ExpectExec(sqlRegexStr string) Mock { | ||||
| 	e := &expectedExec{} | ||||
| @@ -125,7 +125,7 @@ func ExpectExec(sqlRegexStr string) Mock { | ||||
| 	return mock.conn | ||||
| } | ||||
|  | ||||
| // Expect database Query to be triggered, which will match | ||||
| // ExpectQuery database Query to be triggered, which will match | ||||
| // the given query string as a regular expression | ||||
| func ExpectQuery(sqlRegexStr string) Mock { | ||||
| 	e := &expectedQuery{} | ||||
| @@ -136,14 +136,14 @@ func ExpectQuery(sqlRegexStr string) Mock { | ||||
| 	return mock.conn | ||||
| } | ||||
|  | ||||
| // The expectation should be called with given arguments. | ||||
| // WithArgs expectation should be called with given arguments. | ||||
| // Works with Exec and Query expectations | ||||
| func (c *conn) WithArgs(args ...driver.Value) Mock { | ||||
| 	eq, ok := c.active.(*expectedQuery) | ||||
| 	if !ok { | ||||
| 		ee, ok := c.active.(*expectedExec) | ||||
| 		if !ok { | ||||
| 			panic(fmt.Sprintf("Arguments may be expected only with query based expectations, current is %T", c.active)) | ||||
| 			panic(fmt.Sprintf("arguments may be expected only with query based expectations, current is %T", c.active)) | ||||
| 		} | ||||
| 		ee.args = args | ||||
| 	} else { | ||||
| @@ -152,23 +152,23 @@ func (c *conn) WithArgs(args ...driver.Value) Mock { | ||||
| 	return c | ||||
| } | ||||
|  | ||||
| // The expectation will return a Result. | ||||
| // WillReturnResult expectation will return a Result. | ||||
| // Works only with Exec expectations | ||||
| func (c *conn) WillReturnResult(result driver.Result) Mock { | ||||
| 	eq, ok := c.active.(*expectedExec) | ||||
| 	if !ok { | ||||
| 		panic(fmt.Sprintf("driver.Result may be returned only by Exec expectations, current is %T", c.active)) | ||||
| 		panic(fmt.Sprintf("driver.result may be returned only by exec expectations, current is %T", c.active)) | ||||
| 	} | ||||
| 	eq.result = result | ||||
| 	return c | ||||
| } | ||||
|  | ||||
| // The expectation will return Rows. | ||||
| // WillReturnRows expectation will return Rows. | ||||
| // Works only with Query expectations | ||||
| func (c *conn) WillReturnRows(rows driver.Rows) Mock { | ||||
| 	eq, ok := c.active.(*expectedQuery) | ||||
| 	if !ok { | ||||
| 		panic(fmt.Sprintf("driver.Rows may be returned only by Query expectations, current is %T", c.active)) | ||||
| 		panic(fmt.Sprintf("driver.rows may be returned only by query expectations, current is %T", c.active)) | ||||
| 	} | ||||
| 	eq.rows = rows | ||||
| 	return c | ||||
|   | ||||
| @@ -2,14 +2,14 @@ package sqlmock | ||||
|  | ||||
| import ( | ||||
| 	"database/sql" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestMockQuery(t *testing.T) { | ||||
| 	db, err := sql.Open("mock", "") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when opening a stub database connection", err) | ||||
| 		t.Errorf("an error '%s' was not expected when opening a stub database connection", err) | ||||
| 	} | ||||
|  | ||||
| 	rs := RowsFromCSVString([]string{"id", "title"}, "5,hello world") | ||||
| @@ -20,11 +20,11 @@ func TestMockQuery(t *testing.T) { | ||||
|  | ||||
| 	rows, err := db.Query("SELECT (.+) FROM articles WHERE id = ?", 5) | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while retrieving mock rows", err) | ||||
| 		t.Errorf("error '%s' was not expected while retrieving mock rows", err) | ||||
| 	} | ||||
| 	defer rows.Close() | ||||
| 	if !rows.Next() { | ||||
| 		t.Error("It must have had one row as result, but got empty result set instead") | ||||
| 		t.Error("it must have had one row as result, but got empty result set instead") | ||||
| 	} | ||||
|  | ||||
| 	var id int | ||||
| @@ -32,26 +32,26 @@ func TestMockQuery(t *testing.T) { | ||||
|  | ||||
| 	err = rows.Scan(&id, &title) | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while trying to scan row", err) | ||||
| 		t.Errorf("error '%s' was not expected while trying to scan row", err) | ||||
| 	} | ||||
|  | ||||
| 	if id != 5 { | ||||
| 		t.Errorf("Expected mocked id to be 5, but got %d instead", id) | ||||
| 		t.Errorf("expected mocked id to be 5, but got %d instead", id) | ||||
| 	} | ||||
|  | ||||
| 	if title != "hello world" { | ||||
| 		t.Errorf("Expected mocked title to be 'hello world', but got '%s' instead", title) | ||||
| 		t.Errorf("expected mocked title to be 'hello world', but got '%s' instead", title) | ||||
| 	} | ||||
|  | ||||
| 	if err = db.Close(); err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while closing the database", err) | ||||
| 		t.Errorf("error '%s' was not expected while closing the database", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestTransactionExpectations(t *testing.T) { | ||||
| 	db, err := sql.Open("mock", "") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when opening a stub database connection", err) | ||||
| 		t.Errorf("an error '%s' was not expected when opening a stub database connection", err) | ||||
| 	} | ||||
|  | ||||
| 	// begin and commit | ||||
| @@ -60,12 +60,12 @@ func TestTransactionExpectations(t *testing.T) { | ||||
|  | ||||
| 	tx, err := db.Begin() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when beginning a transaction", err) | ||||
| 		t.Errorf("an error '%s' was not expected when beginning a transaction", err) | ||||
| 	} | ||||
|  | ||||
| 	err = tx.Commit() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when commiting a transaction", err) | ||||
| 		t.Errorf("an error '%s' was not expected when commiting a transaction", err) | ||||
| 	} | ||||
|  | ||||
| 	// begin and rollback | ||||
| @@ -74,31 +74,31 @@ func TestTransactionExpectations(t *testing.T) { | ||||
|  | ||||
| 	tx, err = db.Begin() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when beginning a transaction", err) | ||||
| 		t.Errorf("an error '%s' was not expected when beginning a transaction", err) | ||||
| 	} | ||||
|  | ||||
| 	err = tx.Rollback() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when rolling back a transaction", err) | ||||
| 		t.Errorf("an error '%s' was not expected when rolling back a transaction", err) | ||||
| 	} | ||||
|  | ||||
| 	// begin with an error | ||||
| 	ExpectBegin().WillReturnError(errors.New("Some err")) | ||||
| 	ExpectBegin().WillReturnError(fmt.Errorf("some err")) | ||||
|  | ||||
| 	tx, err = db.Begin() | ||||
| 	if err == nil { | ||||
| 		t.Error("An error was expected when beginning a transaction, but got none") | ||||
| 		t.Error("an error was expected when beginning a transaction, but got none") | ||||
| 	} | ||||
|  | ||||
| 	if err = db.Close(); err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while closing the database", err) | ||||
| 		t.Errorf("error '%s' was not expected while closing the database", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestPreparedQueryExecutions(t *testing.T) { | ||||
| 	db, err := sql.Open("mock", "") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when opening a stub database connection", err) | ||||
| 		t.Errorf("an error '%s' was not expected when opening a stub database connection", err) | ||||
| 	} | ||||
|  | ||||
| 	rs1 := RowsFromCSVString([]string{"id", "title"}, "5,hello world") | ||||
| @@ -113,7 +113,7 @@ func TestPreparedQueryExecutions(t *testing.T) { | ||||
|  | ||||
| 	stmt, err := db.Prepare("SELECT (.+) FROM articles WHERE id = ?") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while creating a prepared statement", err) | ||||
| 		t.Errorf("error '%s' was not expected while creating a prepared statement", err) | ||||
| 	} | ||||
|  | ||||
| 	var id int | ||||
| @@ -121,44 +121,44 @@ func TestPreparedQueryExecutions(t *testing.T) { | ||||
|  | ||||
| 	err = stmt.QueryRow(5).Scan(&id, &title) | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected querying row from statement and scanning", err) | ||||
| 		t.Errorf("error '%s' was not expected querying row from statement and scanning", err) | ||||
| 	} | ||||
|  | ||||
| 	if id != 5 { | ||||
| 		t.Errorf("Expected mocked id to be 5, but got %d instead", id) | ||||
| 		t.Errorf("expected mocked id to be 5, but got %d instead", id) | ||||
| 	} | ||||
|  | ||||
| 	if title != "hello world" { | ||||
| 		t.Errorf("Expected mocked title to be 'hello world', but got '%s' instead", title) | ||||
| 		t.Errorf("expected mocked title to be 'hello world', but got '%s' instead", title) | ||||
| 	} | ||||
|  | ||||
| 	err = stmt.QueryRow(2).Scan(&id, &title) | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected querying row from statement and scanning", err) | ||||
| 		t.Errorf("error '%s' was not expected querying row from statement and scanning", err) | ||||
| 	} | ||||
|  | ||||
| 	if id != 2 { | ||||
| 		t.Errorf("Expected mocked id to be 2, but got %d instead", id) | ||||
| 		t.Errorf("expected mocked id to be 2, but got %d instead", id) | ||||
| 	} | ||||
|  | ||||
| 	if title != "whoop" { | ||||
| 		t.Errorf("Expected mocked title to be 'whoop', but got '%s' instead", title) | ||||
| 		t.Errorf("expected mocked title to be 'whoop', but got '%s' instead", title) | ||||
| 	} | ||||
|  | ||||
| 	if err = db.Close(); err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while closing the database", err) | ||||
| 		t.Errorf("error '%s' was not expected while closing the database", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestUnexpectedOperations(t *testing.T) { | ||||
| 	db, err := sql.Open("mock", "") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when opening a stub database connection", err) | ||||
| 		t.Errorf("an error '%s' was not expected when opening a stub database connection", err) | ||||
| 	} | ||||
|  | ||||
| 	stmt, err := db.Prepare("SELECT (.+) FROM articles WHERE id = ?") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while creating a prepared statement", err) | ||||
| 		t.Errorf("error '%s' was not expected while creating a prepared statement", err) | ||||
| 	} | ||||
|  | ||||
| 	var id int | ||||
| @@ -166,21 +166,21 @@ func TestUnexpectedOperations(t *testing.T) { | ||||
|  | ||||
| 	err = stmt.QueryRow(5).Scan(&id, &title) | ||||
| 	if err == nil { | ||||
| 		t.Error("Error was expected querying row, since there was no such expectation") | ||||
| 		t.Error("error was expected querying row, since there was no such expectation") | ||||
| 	} | ||||
|  | ||||
| 	ExpectRollback() | ||||
|  | ||||
| 	err = db.Close() | ||||
| 	if err == nil { | ||||
| 		t.Error("Error was expected while closing the database, expectation was not fulfilled", err) | ||||
| 		t.Error("error was expected while closing the database, expectation was not fulfilled", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestWrongExpectations(t *testing.T) { | ||||
| 	db, err := sql.Open("mock", "") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when opening a stub database connection", err) | ||||
| 		t.Errorf("an error '%s' was not expected when opening a stub database connection", err) | ||||
| 	} | ||||
|  | ||||
| 	ExpectBegin() | ||||
| @@ -190,12 +190,12 @@ func TestWrongExpectations(t *testing.T) { | ||||
| 		WithArgs(5). | ||||
| 		WillReturnRows(rs1) | ||||
|  | ||||
| 	ExpectCommit().WillReturnError(errors.New("Deadlock occured")) | ||||
| 	ExpectCommit().WillReturnError(fmt.Errorf("deadlock occured")) | ||||
| 	ExpectRollback() // won't be triggered | ||||
|  | ||||
| 	stmt, err := db.Prepare("SELECT (.+) FROM articles WHERE id = ? FOR UPDATE") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while creating a prepared statement", err) | ||||
| 		t.Errorf("error '%s' was not expected while creating a prepared statement", err) | ||||
| 	} | ||||
|  | ||||
| 	var id int | ||||
| @@ -203,35 +203,35 @@ func TestWrongExpectations(t *testing.T) { | ||||
|  | ||||
| 	err = stmt.QueryRow(5).Scan(&id, &title) | ||||
| 	if err == nil { | ||||
| 		t.Error("Error was expected while querying row, since there Begin transaction expectation is not fulfilled") | ||||
| 		t.Error("error was expected while querying row, since there begin transaction expectation is not fulfilled") | ||||
| 	} | ||||
|  | ||||
| 	// lets go around and start transaction | ||||
| 	tx, err := db.Begin() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when beginning a transaction", err) | ||||
| 		t.Errorf("an error '%s' was not expected when beginning a transaction", err) | ||||
| 	} | ||||
|  | ||||
| 	err = stmt.QueryRow(5).Scan(&id, &title) | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while querying row, since transaction was started", err) | ||||
| 		t.Errorf("error '%s' was not expected while querying row, since transaction was started", err) | ||||
| 	} | ||||
|  | ||||
| 	err = tx.Commit() | ||||
| 	if err == nil { | ||||
| 		t.Error("A deadlock error was expected when commiting a transaction", err) | ||||
| 		t.Error("a deadlock error was expected when commiting a transaction", err) | ||||
| 	} | ||||
|  | ||||
| 	err = db.Close() | ||||
| 	if err == nil { | ||||
| 		t.Error("Error was expected while closing the database, expectation was not fulfilled", err) | ||||
| 		t.Error("error was expected while closing the database, expectation was not fulfilled", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestExecExpectations(t *testing.T) { | ||||
| 	db, err := sql.Open("mock", "") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("An error '%s' was not expected when opening a stub database connection", err) | ||||
| 		t.Errorf("an error '%s' was not expected when opening a stub database connection", err) | ||||
| 	} | ||||
|  | ||||
| 	result := NewResult(1, 1) | ||||
| @@ -241,28 +241,28 @@ func TestExecExpectations(t *testing.T) { | ||||
|  | ||||
| 	res, err := db.Exec("INSERT INTO articles (title) VALUES (?)", "hello") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected, while inserting a row", err) | ||||
| 		t.Errorf("error '%s' was not expected, while inserting a row", err) | ||||
| 	} | ||||
|  | ||||
| 	id, err := res.LastInsertId() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected, while getting a last insert id", err) | ||||
| 		t.Errorf("error '%s' was not expected, while getting a last insert id", err) | ||||
| 	} | ||||
|  | ||||
| 	affected, err := res.RowsAffected() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected, while getting affected rows", err) | ||||
| 		t.Errorf("error '%s' was not expected, while getting affected rows", err) | ||||
| 	} | ||||
|  | ||||
| 	if id != 1 { | ||||
| 		t.Errorf("Expected last insert id to be 1, but got %d instead", id) | ||||
| 		t.Errorf("expected last insert id to be 1, but got %d instead", id) | ||||
| 	} | ||||
|  | ||||
| 	if affected != 1 { | ||||
| 		t.Errorf("Expected affected rows to be 1, but got %d instead", affected) | ||||
| 		t.Errorf("expected affected rows to be 1, but got %d instead", affected) | ||||
| 	} | ||||
|  | ||||
| 	if err = db.Close(); err != nil { | ||||
| 		t.Errorf("Error '%s' was not expected while closing the database", err) | ||||
| 		t.Errorf("error '%s' was not expected while closing the database", err) | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| package sqlmock | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| ) | ||||
|  | ||||
| @@ -12,12 +11,12 @@ type transaction struct { | ||||
| func (tx *transaction) Commit() error { | ||||
| 	e := tx.conn.next() | ||||
| 	if e == nil { | ||||
| 		return errors.New("All expectations were already fulfilled, call to Commit transaction was not expected") | ||||
| 		return fmt.Errorf("all expectations were already fulfilled, call to commit transaction was not expected") | ||||
| 	} | ||||
|  | ||||
| 	etc, ok := e.(*expectedCommit) | ||||
| 	if !ok { | ||||
| 		return errors.New(fmt.Sprintf("Call to Commit transaction, was not expected, next expectation was %v", e)) | ||||
| 		return fmt.Errorf("call to commit transaction, was not expected, next expectation was %v", e) | ||||
| 	} | ||||
| 	etc.triggered = true | ||||
| 	return etc.err | ||||
| @@ -26,12 +25,12 @@ func (tx *transaction) Commit() error { | ||||
| func (tx *transaction) Rollback() error { | ||||
| 	e := tx.conn.next() | ||||
| 	if e == nil { | ||||
| 		return errors.New("All expectations were already fulfilled, call to Rollback transaction was not expected") | ||||
| 		return fmt.Errorf("all expectations were already fulfilled, call to rollback transaction was not expected") | ||||
| 	} | ||||
|  | ||||
| 	etr, ok := e.(*expectedRollback) | ||||
| 	if !ok { | ||||
| 		return errors.New(fmt.Sprintf("Call to Rollback transaction, was not expected, next expectation was %v", e)) | ||||
| 		return fmt.Errorf("call to rollback transaction, was not expected, next expectation was %v", e) | ||||
| 	} | ||||
| 	etr.triggered = true | ||||
| 	return etr.err | ||||
|   | ||||
		Reference in New Issue
	
	Block a user