-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
94 lines (76 loc) · 1.96 KB
/
example_test.go
File metadata and controls
94 lines (76 loc) · 1.96 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package dbsteps_test
import (
"database/sql"
"encoding/json"
"github.com/bool64/sqluct"
"github.com/cucumber/godog"
"github.com/godogx/dbsteps"
"github.com/godogx/vars"
)
func ExampleNewManager() {
var db *sql.DB
vs := &vars.Steps{}
// Initialize database manager.
dbm := dbsteps.NewManager()
dbm.VS = vs // Setup shared vars steps.
dbm.AddDB(db)
suite := godog.TestSuite{
Name: "DatabaseContext",
TestSuiteInitializer: nil,
ScenarioInitializer: func(s *godog.ScenarioContext) {
dbm.RegisterSteps(s)
vs.Register(s)
},
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
Strict: true,
},
}
status := suite.Run()
println(status)
}
func ExampleNewManager_advanced() {
// 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]any{
"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) (any, 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
}