You've already forked go-sqlmock
mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-06-23 00:17:47 +02:00
update readme and check with linters
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
language: go
|
language: go
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.2
|
- 1.2
|
||||||
- 1.3
|
- 1.3
|
||||||
@ -9,6 +10,4 @@ go:
|
|||||||
- tip
|
- tip
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- go get -t
|
|
||||||
- go test -v
|
|
||||||
- go test -race
|
- go test -race
|
||||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
The three clause BSD license (http://en.wikipedia.org/wiki/BSD_licenses)
|
The three clause BSD license (http://en.wikipedia.org/wiki/BSD_licenses)
|
||||||
|
|
||||||
Copyright (c) 2013-2015, DataDog.lt team
|
Copyright (c) 2013-2016, DATA-DOG team
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
23
README.md
23
README.md
@ -3,25 +3,22 @@
|
|||||||
|
|
||||||
# Sql driver mock for Golang
|
# Sql driver mock for Golang
|
||||||
|
|
||||||
This is a **mock** driver as **database/sql/driver** which is very flexible and pragmatic to
|
**sqlmock** is a mock library implementing [sql/driver](https://godoc.org/database/sql/driver). Which has one and only
|
||||||
manage and mock expected queries. All the expectations should be met and all queries and actions
|
purpose - to simulate any **sql** driver behavior in tests, without needing a real database connection. It helps to
|
||||||
triggered should be mocked in order to pass a test. The package has no 3rd party dependencies.
|
maintain correct **TDD** workflow.
|
||||||
|
|
||||||
**NOTE:** regarding major issues #20 and #9 the **api** has changed to support concurrency and more than
|
- this library is now complete and stable. (you may not find new changes for this reason)
|
||||||
one database connection.
|
- supports concurrency and multiple connections.
|
||||||
|
- does not require any modifications to your source code.
|
||||||
If you need an old version, checkout **go-sqlmock** at gopkg.in:
|
- the driver allows to mock any sql driver method behavior.
|
||||||
|
- has strict by default expectation order matching.
|
||||||
go get gopkg.in/DATA-DOG/go-sqlmock.v0
|
- has no vendor dependencies.
|
||||||
|
|
||||||
Otherwise use the **v1** branch from master which should be stable afterwards, because all the issues which
|
|
||||||
were known will be fixed in this version.
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
go get gopkg.in/DATA-DOG/go-sqlmock.v1
|
go get gopkg.in/DATA-DOG/go-sqlmock.v1
|
||||||
|
|
||||||
Or take an older version:
|
If you need an old version, checkout **go-sqlmock** at gopkg.in:
|
||||||
|
|
||||||
go get gopkg.in/DATA-DOG/go-sqlmock.v0
|
go get gopkg.in/DATA-DOG/go-sqlmock.v0
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@ package sqlmock
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type void struct{}
|
type void struct{}
|
||||||
@ -22,33 +20,6 @@ func ExampleNew() {
|
|||||||
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
|
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNewWithDSN() {
|
|
||||||
_, mock, err := NewWithDSN("mydsn for gorm")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("expected no error, but got:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// use the same dsn to initialize a connection
|
|
||||||
db, err := gorm.Open("sqlmock", "mydsn for gorm")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("expected no error from gorm, but got:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
db.SetLogger(void{}) // sad - nil panics
|
|
||||||
|
|
||||||
// now we can expect operations performed on db
|
|
||||||
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
|
|
||||||
|
|
||||||
txconn := db.Begin()
|
|
||||||
for _, err := range txconn.GetErrors() {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output: `sqlmock` is not officially supported, running under compatibility mode.
|
|
||||||
// an error will occur on db.Begin() call
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestShouldOpenConnectionIssue15(t *testing.T) {
|
func TestShouldOpenConnectionIssue15(t *testing.T) {
|
||||||
db, mock, err := New()
|
db, mock, err := New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -73,7 +73,7 @@ func TestQueryExpectationSqlMatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleExpectExec() {
|
func ExampleExpectedExec() {
|
||||||
db, mock, _ := New()
|
db, mock, _ := New()
|
||||||
result := NewErrorResult(fmt.Errorf("some error"))
|
result := NewErrorResult(fmt.Errorf("some error"))
|
||||||
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
|
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
|
||||||
|
19
sqlmock.go
19
sqlmock.go
@ -1,13 +1,12 @@
|
|||||||
/*
|
/*
|
||||||
Package sqlmock provides sql driver connection, which allows to test database
|
Package sqlmock is a mock library implementing sql driver. Which has one and only
|
||||||
interactions by expected calls and simulate their results or errors.
|
purpose - to simulate any sql driver behavior in tests, without needing a real
|
||||||
|
database connection. It helps to maintain correct **TDD** workflow.
|
||||||
|
|
||||||
It does not require any modifications to your source code in order to test
|
It does not require any modifications to your source code in order to test
|
||||||
and mock database operations. It does not even require a real database in order
|
and mock database operations. Supports concurrency and multiple database mocking.
|
||||||
to test your application.
|
|
||||||
|
|
||||||
The driver allows to mock any sql driver method behavior. Concurrent actions
|
The driver allows to mock any sql driver method behavior.
|
||||||
are also supported.
|
|
||||||
*/
|
*/
|
||||||
package sqlmock
|
package sqlmock
|
||||||
|
|
||||||
@ -80,12 +79,12 @@ type sqlmock struct {
|
|||||||
expected []expectation
|
expected []expectation
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sqlmock) open() (*sql.DB, Sqlmock, error) {
|
func (c *sqlmock) open() (*sql.DB, Sqlmock, error) {
|
||||||
db, err := sql.Open("sqlmock", s.dsn)
|
db, err := sql.Open("sqlmock", c.dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return db, s, err
|
return db, c, err
|
||||||
}
|
}
|
||||||
return db, s, db.Ping()
|
return db, c, db.Ping()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *sqlmock) ExpectClose() *ExpectedClose {
|
func (c *sqlmock) ExpectClose() *ExpectedClose {
|
||||||
|
Reference in New Issue
Block a user