-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidatorWrapped_test.go
More file actions
194 lines (166 loc) · 7.36 KB
/
validatorWrapped_test.go
File metadata and controls
194 lines (166 loc) · 7.36 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
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
193
194
package validator
import (
"bytes"
"net/http"
"net/url"
"testing"
"github.com/siherrmann/validator/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWrappedValidate(t *testing.T) {
s := testStruct{Name: "apple", Age: 2}
err := Validate(&s)
assert.NoError(t, err, "Expected no error on validate")
}
func TestWrappedValidateWithValidation(t *testing.T) {
jsonInput := map[string]any{"name": "apple", "age": 2}
validations := []model.Validation{
{Key: "name", Requirement: "equapple"},
{Key: "age", Requirement: "min2"},
}
result, err := ValidateWithValidation(jsonInput, validations)
assert.NoError(t, err, "Expected no error on validate with validation")
assert.Equal(t, "apple", result["name"], "Expected name to be 'apple'")
assert.Equal(t, 2, result["age"], "Expected age to be 2")
}
func TestWrappedValidateAndUpdate(t *testing.T) {
jsonInput := map[string]any{"name": "apple", "age": 2}
var s testStruct
err := ValidateAndUpdate(jsonInput, &s)
assert.NoError(t, err, "Expected no error on validate and update")
assert.Equal(t, "apple", s.Name, "Expected name to be 'apple'")
assert.Equal(t, 2, s.Age, "Expected age to be 2")
}
func TestWrappedValidateAndUpdateWithValidation(t *testing.T) {
jsonInput := map[string]any{"name": "apple", "age": 2}
mapToUpdate := map[string]any{}
validations := []model.Validation{
{Key: "name", Requirement: "equapple"},
{Key: "age", Requirement: "min2"},
}
err := ValidateAndUpdateWithValidation(jsonInput, &mapToUpdate, validations)
assert.NoError(t, err, "Expected no error on validate and update with validation")
assert.Equal(t, "apple", mapToUpdate["name"], "Expected name to be 'apple'")
assert.Equal(t, 2, mapToUpdate["age"], "Expected age to be 2")
}
func TestWrappedUnmapOrUnmarshalAndValidate(t *testing.T) {
form := url.Values{}
form.Set("name", "apple")
form.Set("age", "2")
req, _ := http.NewRequest("POST", "/", bytes.NewBufferString(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var s testStruct
err := UnmapOrUnmarshalAndValidate(req, &s)
assert.NoError(t, err, "Expected no error on unmap or unmarshal and validate")
assert.Equal(t, "apple", s.Name, "Expected name to be 'apple'")
assert.Equal(t, 2, s.Age, "Expected age to be 2")
}
func TestWrappedUnmapAndValidate(t *testing.T) {
values := url.Values{}
values.Set("name", "apple")
values.Set("age", "2")
var s testStruct
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
require.NoError(t, err, "Expected no error creating request")
err = UnmapAndValidate(req, &s)
assert.NoError(t, err, "Expected no error on unmap and validate")
assert.Equal(t, "apple", s.Name, "Expected name to be 'apple'")
assert.Equal(t, 2, s.Age, "Expected age to be 2")
}
func TestWrappedUnmarshalAndValidate(t *testing.T) {
data := []byte(`{"name":"apple","age":2}`)
var s testStruct
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
require.NoError(t, err, "Expected no error creating request")
err = UnmarshalAndValidate(req, &s)
assert.NoError(t, err, "Expected no error on unmarshal and validate")
assert.Equal(t, "apple", s.Name, "Expected name to be 'apple'")
assert.Equal(t, 2, s.Age, "Expected age to be 2")
}
func TestWrappedUnmapOrUnmarshalValidateAndUpdate(t *testing.T) {
form := url.Values{}
form.Set("name", "apple")
form.Set("age", "2")
req, _ := http.NewRequest("POST", "/", bytes.NewBufferString(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var s testStruct
err := UnmapOrUnmarshalValidateAndUpdate(req, &s)
assert.NoError(t, err, "Expected no error on unmap or unmarshal validate and update")
assert.Equal(t, "apple", s.Name, "Expected name to be 'apple'")
assert.Equal(t, 2, s.Age, "Expected age to be 2")
}
func TestWrappedUnmapValidateAndUpdate(t *testing.T) {
values := url.Values{}
values.Set("name", "apple")
values.Set("age", "2")
var s testStruct
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
require.NoError(t, err, "Expected no error creating request")
err = UnmapValidateAndUpdate(req, &s)
assert.NoError(t, err, "Expected no error on unmap validate and update")
assert.Equal(t, "apple", s.Name, "Expected name to be 'apple'")
assert.Equal(t, 2, s.Age, "Expected age to be 2")
}
func TestWrappedUnmarshalValidateAndUpdate(t *testing.T) {
data := []byte(`{"name":"apple","age":2}`)
var s testStruct
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
require.NoError(t, err, "Expected no error creating request")
err = UnmarshalValidateAndUpdate(req, &s)
assert.NoError(t, err, "Expected no error on unmarshal validate and update")
assert.Equal(t, "apple", s.Name, "Expected name to be 'apple'")
assert.Equal(t, 2, s.Age, "Expected age to be 2")
}
func TestWrappedUnmapOrUnmarshalValidateAndUpdateWithValidation(t *testing.T) {
form := url.Values{}
form.Set("name", "apple")
form.Set("age", "2")
req, _ := http.NewRequest("POST", "/", bytes.NewBufferString(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
mapToUpdate := map[string]any{}
validations := []model.Validation{
{Key: "name", Requirement: "equapple"},
{Key: "age", Requirement: "min2"},
}
err := UnmapOrUnmarshalValidateAndUpdateWithValidation(req, &mapToUpdate, validations)
assert.NoError(t, err, "Expected no error on unmap or unmarshal validate and update with validation")
assert.Equal(t, "apple", mapToUpdate["name"], "Expected name to be 'apple'")
assert.Equal(t, float64(2), mapToUpdate["age"], "Expected age to be 2")
}
func TestWrappedUnmapValidateAndUpdateWithValidation(t *testing.T) {
values := url.Values{}
values.Set("name", "apple")
values.Set("age", "2")
mapToUpdate := map[string]any{}
validations := []model.Validation{
{Key: "name", Requirement: "equapple"},
{Key: "age", Requirement: "min2"},
}
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
require.NoError(t, err, "Expected no error creating request")
err = UnmapValidateAndUpdateWithValidation(req, &mapToUpdate, validations)
assert.NoError(t, err, "Expected no error on unmap validate and update with validation")
assert.Equal(t, "apple", mapToUpdate["name"], "Expected name to be 'apple'")
assert.Equal(t, float64(2), mapToUpdate["age"], "Expected age to be 2")
}
func TestWrappedUnmarshalValidateAndUpdateWithValidation(t *testing.T) {
data := []byte(`{"name":"apple","age":2}`)
mapToUpdate := map[string]any{}
validations := []model.Validation{
{Key: "name", Requirement: "equapple"},
{Key: "age", Requirement: "min2"},
}
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
require.NoError(t, err, "Expected no error creating request")
err = UnmarshalValidateAndUpdateWithValidation(req, &mapToUpdate, validations)
assert.NoError(t, err, "Expected no error on unmarshal validate and update with validation")
assert.Equal(t, "apple", mapToUpdate["name"], "Expected name to be 'apple'")
assert.Equal(t, float64(2), mapToUpdate["age"], "Expected age to be 2")
}