-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_schema.go
110 lines (92 loc) · 2.65 KB
/
match_schema.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gcsv
import (
"encoding/csv"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/onsi/gomega/types"
)
const errorMsg = "(field at position %d of row %d should have been of type %s)"
type RepresentSchemaOption func(*representSchemaMatcher)
func IgnoreHeaderRow() RepresentSchemaOption {
return func(matcher *representSchemaMatcher) {
matcher.ignoreHeaderRow = true
}
}
func WithHeaders(headers ...string) RepresentSchemaOption {
return func(matcher *representSchemaMatcher) {
matcher.withHeaders = headers
}
}
func RepresentSchema(expected []interface{}, opts ...RepresentSchemaOption) types.GomegaMatcher {
m := &representSchemaMatcher{
expected: expected,
}
for _, v := range opts {
v(m)
}
return m
}
type representSchemaMatcher struct {
expected []interface{}
ignoreHeaderRow bool
withHeaders []string
lastError string
}
func (matcher *representSchemaMatcher) Match(actual interface{}) (success bool, err error) {
response, ok := actual.(string)
if !ok {
return false, fmt.Errorf("representSchema matcher expects a string, got %T instead", actual)
}
lines, err := csv.NewReader(strings.NewReader(response)).ReadAll()
if err != nil {
return false, err
}
for r, v := range lines {
if r == 0 {
switch {
case matcher.ignoreHeaderRow:
continue
case len(matcher.withHeaders) > 0:
if reflect.DeepEqual(v, matcher.withHeaders) {
continue
}
matcher.lastError = "headers do not match"
return false, nil
}
}
for i, e := range matcher.expected {
switch e.(type) {
case string:
case int:
_, err := strconv.Atoi(v[i])
if err != nil {
matcher.lastError = fmt.Sprintf(errorMsg, i+1, r+1, "int")
return false, nil
}
case bool:
_, err := strconv.ParseBool(v[i])
if err != nil {
matcher.lastError = fmt.Sprintf(errorMsg, i+1, r+1, "bool")
return false, nil
}
case float64:
_, err := strconv.ParseFloat(v[i], 64)
if err != nil {
matcher.lastError = fmt.Sprintf(errorMsg, i+1, r+1, "float64")
return false, nil
}
default:
return false, fmt.Errorf("type of field at position %d of actual is unable to be handled", i)
}
}
}
return true, nil
}
func (matcher *representSchemaMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n\t%#v\nto contain a CSV matching the schema of\n\t%#v\n%s", actual, matcher.expected, matcher.lastError)
}
func (matcher *representSchemaMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n\t%#v\nnot to contain a CSV matching the schema of\n\t%#v\n%s", actual, matcher.expected, matcher.lastError)
}