Skip to content

Commit 473cf20

Browse files
author
Dmitriy Seredenko
committed
Safe Fake driver Register
1 parent 42de024 commit 473cf20

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ List of features in the library:
2222
#### Install
2323

2424
```
25-
go get github.com/selvatico/go-mocket
25+
go get github.com/Selvatico/go-mocket
2626
```
2727

2828
#### Usage
@@ -39,18 +39,18 @@ Somewhere in you code to setup a tests
3939
```go
4040
import (
4141
"database/sql"
42-
mocket "github.com/selvatico/go-mocket"
42+
mocket "github.com/Selvatico/go-mocket"
4343
"github.com/jinzhu/gorm"
4444
)
4545

4646
func SetupTests() {
47-
sql.Register("fake_test", mocket.FakeDriver{})
47+
mocket.Catcher.Register()
4848
// GORM
49-
db, err := gorm.Open("fake_test", "connection_string") // Could be any connection string
49+
db, err := gorm.Open(mocket.DRIVER_NAME, "any_string") // Could be any connection string
5050
app.DB = db // assumption that it will be used everywhere the same
5151
//OR
5252
// Regular sql package usage
53-
db, err := sql.Open(driver, source)
53+
db, err := sql.Open(mocket.DRIVER_NAME, "any_string")
5454
}
5555
```
5656

@@ -61,7 +61,7 @@ Now if use singleton instance of DB it will use everywhere mocked connection.
6161
###### Example of mocking by pattern
6262

6363
```go
64-
import mocket "github.com/selvatico/go-mocket"
64+
import mocket "github.com/Selvatico/go-mocket"
6565
import "net/http/httptest"
6666

6767
func TestHandler(t *testing.T) {

doc.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ First you need to activate package somewhere in your tests code like this
55
66
import (
77
"database/sql"
8-
mocket "github.com/selvatico/go-mocket"
8+
mocket "github.com/Selvatico/go-mocket"
99
"github.com/jinzhu/gorm"
1010
)
1111
1212
var DB *gorm.DB
1313
1414
func SetupTests() {
15-
sql.Register("fake_test", mocket.FakeDriver{})
15+
mocket.Catcher.Register()
1616
// GORM
1717
db, err := gorm.Open("fake_test", "connection_string") // Could be any connection string
1818
DB = db
@@ -55,11 +55,11 @@ Somewhere in you tests:
5555
"log"
5656
"testing"
5757
"database/sql"
58-
mocket "github.com/selvatico/go-mocket"
58+
mocket "github.com/Selvatico/go-mocket"
5959
)
6060
6161
func TestResponses (t *testing.T) {
62-
sql.Register("fake_test", FakeDriver{})
62+
mocket.Catcher.Register()
6363
db, _ := sql.Open("fake_test", "connection_string") // Could be any connection string
6464
DB = db
6565

response.go

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"log"
77
"reflect"
88
"strings"
9+
"database/sql"
10+
)
11+
const (
12+
DRIVER_NAME = "MOCK_FAKE_DRIVER"
913
)
1014

1115
//Catcher is global instance of Catcher used for attaching all mocks to connection
@@ -18,6 +22,17 @@ type MockCatcher struct {
1822
PanicOnEmptyResponse bool // If not response matches - do we need to panic?
1923
}
2024

25+
// Register safely register FakeDriver
26+
func (mc *MockCatcher) Register() {
27+
driversList := sql.Drivers()
28+
for _, name := range driversList {
29+
if name == DRIVER_NAME {
30+
return
31+
}
32+
}
33+
sql.Register(DRIVER_NAME, FakeDriver{})
34+
}
35+
2136
// Attach several mocks to MockCather. Could be useful to attach mocks from some factories of mocks
2237
func (mc *MockCatcher) Attach(fr []*FakeResponse) {
2338
mc.Mocks = append(mc.Mocks, fr...)

0 commit comments

Comments
 (0)