1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-02-03 13:01:26 +02:00

update readme and check with linters

This commit is contained in:
gedi 2016-01-21 21:09:44 +02:00
parent 081a694b0a
commit 5b568f0d25
6 changed files with 22 additions and 56 deletions

View File

@ -1,5 +1,6 @@
language: go
sudo: false
go:
- 1.2
- 1.3
@ -9,6 +10,4 @@ go:
- tip
script:
- go get -t
- go test -v
- go test -race

View File

@ -1,6 +1,6 @@
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.
Redistribution and use in source and binary forms, with or without

View File

@ -3,25 +3,22 @@
# 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. The package has no 3rd party dependencies.
**sqlmock** is a mock library implementing [sql/driver](https://godoc.org/database/sql/driver). Which has one and only
purpose - to simulate any **sql** driver behavior in tests, without needing a real database connection. It helps to
maintain correct **TDD** workflow.
**NOTE:** regarding major issues #20 and #9 the **api** has changed to support concurrency and more than
one database connection.
If you need an old version, checkout **go-sqlmock** at gopkg.in:
go get gopkg.in/DATA-DOG/go-sqlmock.v0
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.
- this library is now complete and stable. (you may not find new changes for this reason)
- supports concurrency and multiple connections.
- does not require any modifications to your source code.
- the driver allows to mock any sql driver method behavior.
- has strict by default expectation order matching.
- has no vendor dependencies.
## Install
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

View File

@ -3,8 +3,6 @@ package sqlmock
import (
"fmt"
"testing"
"github.com/jinzhu/gorm"
)
type void struct{}
@ -22,33 +20,6 @@ func ExampleNew() {
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) {
db, mock, err := New()
if err != nil {

View File

@ -73,7 +73,7 @@ func TestQueryExpectationSqlMatch(t *testing.T) {
}
}
func ExampleExpectExec() {
func ExampleExpectedExec() {
db, mock, _ := New()
result := NewErrorResult(fmt.Errorf("some error"))
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)

View File

@ -1,13 +1,12 @@
/*
Package sqlmock provides sql driver connection, which allows to test database
interactions by expected calls and simulate their results or errors.
Package sqlmock is a mock library implementing sql driver. Which has one and only
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
and mock database operations. It does not even require a real database in order
to test your application.
and mock database operations. Supports concurrency and multiple database mocking.
The driver allows to mock any sql driver method behavior. Concurrent actions
are also supported.
The driver allows to mock any sql driver method behavior.
*/
package sqlmock
@ -80,12 +79,12 @@ type sqlmock struct {
expected []expectation
}
func (s *sqlmock) open() (*sql.DB, Sqlmock, error) {
db, err := sql.Open("sqlmock", s.dsn)
func (c *sqlmock) open() (*sql.DB, Sqlmock, error) {
db, err := sql.Open("sqlmock", c.dsn)
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 {