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