-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceholder_test.go
156 lines (138 loc) · 4.57 KB
/
placeholder_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
package jm
import (
"fmt"
"regexp"
"testing"
"time"
)
const (
nestedActualJSON = `{
"sample": {
"an_int": 12,
"a_float": 32.3,
"nested": {
"a_bool": false,
"nothing": null,
"double": {
"arr_1": ["A", "B"],
"arr_2": ["A", 2.9998, [{},[2]]]}}}}`
nestedExpectedJSON = `{
"sample": {
"an_int": 12,
"a_float": 32.3,
"nested": {
"a_bool": false,
"nothing": null,
"double": {
"arr_1": ["A", "B"],
"arr_2": ["A", "$GTE_3", [{},[2]]]}}}}`
)
func TestNestedJSONAndCustomPlaceholder(t *testing.T) {
gte3 := func() (string, func(interface{}) error) {
return "$GTE_3", func(val interface{}) error {
valFloat, ok := val.(float64)
if !ok {
return fmt.Errorf("expected value to compare to be an float64 but got: %T", val)
}
if valFloat >= 3 {
return nil
}
return fmt.Errorf("%f is not greater or equal than 3", valFloat)
}
}
expectedErrStr := "placeholder $GTE_3 match failed: 2.999800 is not greater or equal than 3"
err := Match([]byte(nestedExpectedJSON), []byte(nestedActualJSON), gte3)
if err == nil {
t.Errorf("expected an error, not none")
} else if err.Error() != expectedErrStr {
t.Errorf("expected error string %q, but got %q", expectedErrStr, err.Error())
}
}
func TestWithNotEmptyPlaceholderWhenMatch(t *testing.T) {
if err := Match(
[]byte(`{"name": "$NOT_EMPTY"}`),
[]byte(`{"name": "John"}`),
WithNotEmpty("$NOT_EMPTY"),
); err != nil {
t.Errorf("unexpected error %s, expected JSONs to be equal", err)
}
}
func TestWithNotEmptyPlaceholderWhenEmpty(t *testing.T) {
expectedErrStr := "placeholder $NOT_EMPTY match failed: expected value to be not empty, but it was"
err := Match(
[]byte(`{"name": "$NOT_EMPTY"}`),
[]byte(`{"name": ""}`),
WithNotEmpty("$NOT_EMPTY"),
)
if err == nil {
t.Error("expected an error, got none")
} else if err.Error() != expectedErrStr {
t.Errorf("expected error string %q, but got %q", expectedErrStr, err.Error())
}
}
func TestWithTimeLayoutWhenMatch(t *testing.T) {
if err := Match(
[]byte(`{"created_at": "$TIME_RFC3339"}`),
[]byte(`{"created_at": "2009-11-10T23:00:00Z"}`),
WithTimeLayout("$TIME_RFC3339", time.RFC3339),
); err != nil {
t.Errorf("unexpected error %s, expected JSONs to be equal", err)
}
}
func TestWithTimeLayoutWhenInvalidTimeString(t *testing.T) {
expectedErrStr := "placeholder $TIME_RFC3339 match failed: cannot parse time, value is of type bool - not a string"
err := Match(
[]byte(`{"created_at": "$TIME_RFC3339"}`),
[]byte(`{"created_at": false}`),
WithTimeLayout("$TIME_RFC3339", time.RFC3339),
)
if err == nil {
t.Error("expected an error, got none")
} else if err.Error() != expectedErrStr {
t.Errorf("expected error string %q, but got %q", expectedErrStr, err.Error())
}
}
func TestWithTimeLayoutWhenInvalidTimeFormat(t *testing.T) {
expectedErrStr := "placeholder $TIME_RFC3339 match failed: cannot parse layout \"2006-01-02T15:04:05Z07:00\" with \"hello\""
err := Match(
[]byte(`{"created_at": "$TIME_RFC3339"}`),
[]byte(`{"created_at": "hello"}`),
WithTimeLayout("$TIME_RFC3339", time.RFC3339),
)
if err == nil {
t.Error("expected an error, got none")
} else if err.Error() != expectedErrStr {
t.Errorf("expected error string %q, but got %q", expectedErrStr, err.Error())
}
}
func TestWithRegexpWhenMatch(t *testing.T) {
re := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
if err := Match([]byte(`{"uuid": "$UUID"}`), []byte(`{"uuid": "4d6d4d98-996a-4364-8514-e936f1db552b"}`),
WithRegexp("$UUID", re),
); err != nil {
t.Errorf("unexpected error %s, expected JSONs to be equal", err)
}
}
func TestWithRegexpWhenMatchWithInvalidRegexString(t *testing.T) {
re := regexp.MustCompile("^2")
expectedErrStr := "placeholder $UUID match failed: cannot match, value is of type float64 - not a string"
err := Match([]byte(`{"uuid": "$UUID"}`), []byte(`{"uuid": 1}`), WithRegexp("$UUID", re))
if err == nil {
t.Error("expected an error, got none")
} else if err.Error() != expectedErrStr {
t.Errorf("expected error string %q, but got %q", expectedErrStr, err.Error())
}
}
func TestWithRegexpWhenMatchWithNoMatch(t *testing.T) {
re := regexp.MustCompile("^2")
expectedErrStr := "placeholder $UUID match failed: value z does not match with regexp ^2"
err := Match(
[]byte(`{"uuid": "$UUID"}`),
[]byte(`{"uuid": "z"}`),
WithRegexp("$UUID", re))
if err == nil {
t.Error("expected an error, got none")
} else if err.Error() != expectedErrStr {
t.Errorf("expected error string %q, but got %q", expectedErrStr, err.Error())
}
}