-
Notifications
You must be signed in to change notification settings - Fork 17
/
sql.go
78 lines (63 loc) 路 1.69 KB
/
sql.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sql
import (
"encoding/json"
"fmt"
"github.com/DATA-DOG/godog/gherkin"
"github.com/tomatool/tomato/conv"
"github.com/tomatool/tomato/errors"
"github.com/tomatool/tomato/resource"
)
type Resource interface {
resource.Resource
Select(tableName string, condition map[string]string) ([]map[string]string, error)
Insert(tableName string, rows []map[string]string) error
Delete(tableName string, condition map[string]string) (int, error)
}
type Handler struct {
r map[string]Resource
}
func New(r map[string]Resource) *Handler {
return &Handler{r}
}
func (h *Handler) tableCompare(resourceName, tableName string, content *gherkin.DataTable) error {
r, ok := h.r[resourceName]
if !ok {
return fmt.Errorf("%s not found", resourceName)
}
expectedRows, err := conv.GherkinTableToSliceOfMap(content)
if err != nil {
return err
}
for _, expectedRow := range expectedRows {
for k, v := range expectedRow {
if v == "*" {
delete(expectedRow, k)
}
}
out, err := r.Select(tableName, expectedRow)
if err != nil {
return err
}
if len(out) == 0 {
rows, _ := r.Select(tableName, nil)
r, _ := json.MarshalIndent(expectedRow, "", " ")
t, _ := json.MarshalIndent(rows, "", " ")
return errors.NewStep("unable to find rows in table `"+tableName+"`", map[string]string{
"expected row": string(r),
"table values": string(t),
})
}
}
return nil
}
func (h *Handler) tableInsert(resourceName, tableName string, content *gherkin.DataTable) error {
r, ok := h.r[resourceName]
if !ok {
return fmt.Errorf("%s not found", resourceName)
}
rows, err := conv.GherkinTableToSliceOfMap(content)
if err != nil {
return err
}
return r.Insert(tableName, rows)
}