-
Notifications
You must be signed in to change notification settings - Fork 6
/
matchJSON_test.go
192 lines (161 loc) · 4.13 KB
/
matchJSON_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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package examples
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gkampitakis/go-snaps/match"
"github.com/gkampitakis/go-snaps/snaps"
)
type myMatcher struct {
age int
}
func Matcher() *myMatcher {
return &myMatcher{}
}
func (m *myMatcher) AgeGreater(a int) *myMatcher {
m.age = a
return m
}
func (m *myMatcher) JSON(s []byte) ([]byte, []match.MatcherError) {
var v struct {
User string
Age int
Email string
}
err := json.Unmarshal(s, &v)
if err != nil {
return nil, []match.MatcherError{
{
Reason: err,
Matcher: "my matcher",
Path: "",
},
}
}
if v.Age < m.age {
return nil, []match.MatcherError{
{
Reason: fmt.Errorf("%d is >= from %d", m.age, v.Age),
Matcher: "my matcher",
Path: "age",
},
}
}
// the second string is the formatted error message
return []byte(`{"value":"blue"}`), nil
}
func TestMatchJSON(t *testing.T) {
t.Run("should make a json object snapshot", func(t *testing.T) {
m := map[string]any{
"mock-0": "value",
"mock-1": 2,
"mock-2": struct{ Msg string }{"Hello World"},
"mock-3": float32(10.4),
}
snaps.MatchJSON(t, m)
})
t.Run("should create a prettyJSON snap", func(t *testing.T) {
value := `{"user":"mock-user","age":10,"email":"mock@email.com"}`
snaps.MatchJSON(t, value)
snaps.MatchJSON(t, []byte(value))
})
t.Run("should marshal struct", func(t *testing.T) {
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Keys []int `json:"keys"`
}
u := User{
Name: "mock-name",
Email: "mock@email.com",
Keys: []int{1, 2, 3, 4, 5},
}
snaps.MatchJSON(t, u)
})
}
func TestMatchers(t *testing.T) {
t.Run("Custom matcher", func(t *testing.T) {
t.Run("struct marshalling", func(t *testing.T) {
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Keys []int `json:"keys"`
}
u := User{
Name: "mock-user",
Email: "mock-user@email.com",
Keys: []int{1, 2, 3, 4, 5},
}
snaps.MatchJSON(t, u, match.Custom("keys", func(val any) (any, error) {
keys, ok := val.([]any)
if !ok {
return nil, fmt.Errorf("expected []any but got %T", val)
}
if len(keys) > 5 {
return nil, fmt.Errorf("expected less than 5 keys")
}
return val, nil
}))
})
t.Run("JSON string validation", func(t *testing.T) {
value := `{"user":"mock-user","age":2,"email":"mock@email.com"}`
snaps.MatchJSON(t, value, match.Custom("age", func(val any) (any, error) {
if valInt, ok := val.(float64); !ok || valInt >= 5 {
return nil, fmt.Errorf("expecting number less than 5")
}
return "<less than 5 age>", nil
}))
})
})
t.Run("Any matcher", func(t *testing.T) {
t.Run("should ignore fields", func(t *testing.T) {
value := fmt.Sprintf(
`{"user":"mock-user","age":10,"nested":{"now":["%s"]}}`,
time.Now(),
)
snaps.MatchJSON(t, value, match.Any("nested.now.0"))
})
t.Run("http response", func(t *testing.T) {
// mock server returning a json object
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
payload := fmt.Sprintf(
`{"data":{"message":"hello world","createdAt":"%s"}}`,
time.Now().UTC(),
)
w.Write([]byte(payload))
}))
res, err := http.Get(s.URL)
if err != nil {
t.Errorf("unexpected error %s", err)
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unexpected error %s", err)
return
}
snaps.MatchJSON(t, body, match.Any("data.createdAt"))
})
})
t.Run("my matcher", func(t *testing.T) {
t.Run("should allow using your matcher", func(t *testing.T) {
value := `{"user":"mock-user","age":10,"email":"mock@email.com"}`
snaps.MatchJSON(t, value, Matcher().AgeGreater(5))
})
})
t.Run("Type matcher", func(t *testing.T) {
t.Run("should create snapshot with type placeholder", func(t *testing.T) {
snaps.MatchJSON(t, `{"data":10}`, match.Type[float64]("data"))
snaps.MatchJSON(
t,
`{"metadata":{"timestamp":"1687108093142"}}`,
match.Type[map[string]any]("metadata"),
)
})
})
}