Skip to content

Commit 0197c16

Browse files
authored
Update docs and examples (#3)
1 parent e823cdc commit 0197c16

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ for [`github.com/cucumber/godog`](https://github.com/cucumber/godog).
1414

1515
Databases instances should be configured with `Manager.Instances`.
1616

17-
```go
18-
dbm := dbsteps.Manager{}
19-
20-
dbm.Instances = map[string]dbsteps.Instance{
21-
"my_db": {
22-
Storage: storage,
23-
Tables: map[string]interface{}{
24-
"my_table": new(repository.MyRow),
25-
"my_another_table": new(repository.MyAnotherRow),
26-
},
17+
```
18+
// Initialize database manager with storage and table rows references.
19+
dbm := dbsteps.NewManager()
20+
dbm.Instances["my_db"] = dbsteps.Instance{
21+
Storage: storage,
22+
Tables: map[string]interface{}{
23+
"my_table": new(repository.MyRow),
24+
"my_another_table": new(repository.MyAnotherRow),
25+
},
26+
// Optionally configure statements to execute after deleting rows from table.
27+
PostCleanup: map[string][]string{
28+
"my_table": {"ALTER SEQUENCE my_table_id_seq RESTART"},
2729
},
2830
}
2931
```
@@ -53,6 +55,7 @@ tableMapper.Decoder.RegisterFunc(func(s string) (interface{}, error) {
5355
if err != nil {
5456
return nil, err
5557
}
58+
5659
return m, err
5760
}, repository.Meta{})
5861

@@ -62,9 +65,8 @@ tableMapper.Decoder.RegisterFunc(func(s string) (interface{}, error) {
6265
}, pq.StringArray{})
6366

6467
// Create database manager with custom mapper.
65-
dbm := dbsteps.Manager{
66-
TableMapper: tableMapper,
67-
}
68+
dbm := dbsteps.NewManager()
69+
dbm.TableMapper = tableMapper
6870
```
6971

7072
## Step Definitions

example_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package dbsteps_test
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/bool64/sqluct"
7+
"github.com/godogx/dbsteps"
8+
)
9+
10+
func ExampleNewManager() {
11+
// Set up a storage adapter for your database connection.
12+
var storage *sqluct.Storage
13+
14+
// Define database row structures in your repository packages.
15+
type MyRow struct {
16+
Foo string `db:"foo"`
17+
}
18+
19+
type MyAnotherRow struct {
20+
Bar int `db:"bar"`
21+
Baz float64 `db:"baz"`
22+
}
23+
24+
// ...........
25+
26+
// Initialize database manager with storage and table rows references.
27+
dbm := dbsteps.NewManager()
28+
dbm.Instances["my_db"] = dbsteps.Instance{
29+
Storage: storage,
30+
Tables: map[string]interface{}{
31+
"my_table": new(MyRow),
32+
"my_another_table": new(MyAnotherRow),
33+
},
34+
// Optionally configure statements to execute after deleting rows from table.
35+
PostCleanup: map[string][]string{
36+
"my_table": {"ALTER SEQUENCE my_table_id_seq RESTART"},
37+
},
38+
}
39+
}
40+
41+
func ExampleNewTableMapper() {
42+
type jsonData struct {
43+
Foo string `json:"foo"`
44+
}
45+
46+
tableMapper := dbsteps.NewTableMapper()
47+
48+
// Apply JSON decoding to a particular type.
49+
tableMapper.Decoder.RegisterFunc(func(s string) (interface{}, error) {
50+
data := jsonData{}
51+
err := json.Unmarshal([]byte(s), &data)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
return data, err
57+
}, jsonData{})
58+
59+
// Create database manager with custom mapper.
60+
dbm := dbsteps.NewManager()
61+
dbm.TableMapper = tableMapper
62+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/bool64/dev v0.2.5
99
github.com/bool64/shared v0.1.4
1010
github.com/bool64/sqluct v0.1.9
11-
github.com/cucumber/godog v0.12.3
11+
github.com/cucumber/godog v0.12.4
1212
github.com/godogx/resource v0.1.0
1313
github.com/jmoiron/sqlx v1.3.4
1414
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
4949
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
5050
github.com/cucumber/gherkin-go/v19 v19.0.3 h1:mMSKu1077ffLbTJULUfM5HPokgeBcIGboyeNUof1MdE=
5151
github.com/cucumber/gherkin-go/v19 v19.0.3/go.mod h1:jY/NP6jUtRSArQQJ5h1FXOUgk5fZK24qtE7vKi776Vw=
52-
github.com/cucumber/godog v0.12.3 h1:nBshklqcWho/joTFtSBfyD4KYkvftwwf0r0XpX6ajNU=
5352
github.com/cucumber/godog v0.12.3/go.mod h1:u6SD7IXC49dLpPN35kal0oYEjsXZWee4pW6Tm9t5pIc=
53+
github.com/cucumber/godog v0.12.4 h1:m+vQaDztkpwpmkBIX6jlwNFJiuCMkPjz5jkrUq4SIlM=
54+
github.com/cucumber/godog v0.12.4/go.mod h1:u6SD7IXC49dLpPN35kal0oYEjsXZWee4pW6Tm9t5pIc=
5455
github.com/cucumber/messages-go/v16 v16.0.0/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g=
5556
github.com/cucumber/messages-go/v16 v16.0.1 h1:fvkpwsLgnIm0qugftrw2YwNlio+ABe2Iu94Ap8GMYIY=
5657
github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g=

manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (m *Manager) registerAssertions(s *godog.ScenarioContext) {
224224
})
225225
}
226226

227-
// NewManager initializes instance of database Manager.
227+
// NewManager creates an instance of database Manager.
228228
func NewManager() *Manager {
229229
return &Manager{
230230
TableMapper: NewTableMapper(),
@@ -235,6 +235,8 @@ func NewManager() *Manager {
235235
}
236236

237237
// Manager owns database connections.
238+
//
239+
// Please use NewManager to create an instance.
238240
type Manager struct {
239241
lock *resource.Lock
240242

0 commit comments

Comments
 (0)