-
Notifications
You must be signed in to change notification settings - Fork 8
/
result_test.go
218 lines (187 loc) · 6.22 KB
/
result_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package jsonschema
import (
"encoding/json"
"testing"
"github.com/test-go/testify/assert"
)
// Define the JSON schema
const schemaJSON = `{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "example-schema",
"type": "object",
"title": "foo object schema",
"properties": {
"foo": {
"title": "foo's title",
"description": "foo's description",
"type": "string",
"pattern": "^foo ",
"minLength": 10
}
},
"required": [ "foo" ],
"additionalProperties": false
}`
func TestValidationOutputs(t *testing.T) {
compiler := NewCompiler()
schema, err := compiler.Compile([]byte(schemaJSON))
if err != nil {
t.Fatalf("Failed to compile schema: %v", err)
}
testCases := []struct {
description string
instance interface{}
expectedValid bool
}{
{
description: "Valid input matching schema requirements",
instance: map[string]interface{}{
"foo": "foo bar baz baz",
},
expectedValid: true,
},
{
description: "Input missing required property 'foo'",
instance: map[string]interface{}{},
expectedValid: false,
},
{
description: "Invalid additional property",
instance: map[string]interface{}{
"foo": "foo valid", "extra": "data",
},
expectedValid: false,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
result := schema.Validate(tc.instance)
if result.Valid != tc.expectedValid {
t.Errorf("FlagOutput validity mismatch: expected %v, got %v", tc.expectedValid, result.Valid)
}
})
}
}
func TestToLocalizeList(t *testing.T) {
// Initialize localizer for Simplified Chinese
i18n, err := GetI18n()
assert.Nil(t, err, "Failed to initialize i18n")
localizer := i18n.NewLocalizer("zh-Hans")
// Define a schema JSON with multiple constraints
schemaJSON := `{
"type": "object",
"properties": {
"name": {"type": "string", "minLength": 3},
"age": {"type": "integer", "minimum": 20},
"email": {"type": "string", "format": "email"}
},
"required": ["name", "age", "email"]
}`
compiler := NewCompiler()
schema, err := compiler.Compile([]byte(schemaJSON))
assert.Nil(t, err, "Schema compilation should not fail")
// Test instance with multiple validation errors
instance := map[string]interface{}{
"name": "Jo",
"age": 18,
"email": "not-an-email",
}
result := schema.Validate(instance)
// Check if the validation result is as expected
assert.False(t, result.IsValid(), "Schema validation should fail for the given instance")
// Localize and output the validation errors
details, err := json.MarshalIndent(result.ToLocalizeList(localizer), "", " ")
assert.Nil(t, err, "Marshaling the localized list should not fail")
// Check if the error message for "minLength" is correctly localized
assert.Contains(t, string(details), "值应至少为 3 个字符", "The error message for 'minLength' should be correctly localized and contain the expected substring")
}
func TestToList(t *testing.T) {
// Create a sample EvaluationResult instance
evaluationResult := &EvaluationResult{
Valid: true,
EvaluationPath: "/",
SchemaLocation: "http://example.com/schema",
InstanceLocation: "http://example.com/instance",
Annotations: map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
Errors: map[string]*EvaluationError{
"required": {
Keyword: "required",
Code: "missing_required_property",
Message: "Required property {property} is missing",
Params: map[string]interface{}{
"property": "fieldName1",
},
},
"minLength": {
Keyword: "minLength",
Code: "string_too_short",
Message: "Value should be at least {min_length} characters",
Params: map[string]interface{}{
"minLength": 5,
},
},
},
Details: []*EvaluationResult{
{
Valid: false,
EvaluationPath: "/property",
Errors: map[string]*EvaluationError{
"format": {
Keyword: "format",
Code: "format_mismatch",
Message: "Value does not match format {format}",
Params: map[string]interface{}{
"format": "email",
},
},
},
},
},
}
// Test case 1: Call ToList with default parameters
list1 := evaluationResult.ToList()
// Verify that the returned list is not nil
assert.NotNil(t, list1, "ToList should return a non-nil list")
// Verify the length of the returned list
assert.Equal(t, 1, len(list1.Details), "Expected length of list.Details is 1")
// Verify the validity of each list item
for _, item := range list1.Details {
assert.Equal(t, false, item.Valid, "Expected validity of list item to match EvaluationResult validity")
}
// Test case 2: Call ToList with includeHierarchy set to false
list2 := evaluationResult.ToList(false)
// Verify that the returned list is not nil
assert.NotNil(t, list2, "ToList with includeHierarchy=false should return a non-nil list")
// Verify the length of the returned list
assert.Equal(t, 1, len(list2.Details), "Expected length of list.Details is 1")
// Verify the validity of each list item
for _, item := range list2.Details {
assert.Equal(t, false, item.Valid, "Expected validity of list item to match EvaluationResult validity")
}
}
// TestToFlag tests the ToFlag method of the EvaluationResult struct.
func TestToFlag(t *testing.T) {
// Test case 1: Valid result
evaluationResultValid := &EvaluationResult{
Valid: true,
}
// Call ToFlag method for valid result
flagValid := evaluationResultValid.ToFlag()
// Verify that the returned flag is not nil
assert.NotNil(t, flagValid, "ToFlag should return a non-nil flag for a valid result")
// Verify the validity of the returned flag
assert.Equal(t, true, flagValid.Valid, "Expected validity of flag to match EvaluationResult validity for a valid result")
// Test case 2: Invalid result
evaluationResultInvalid := &EvaluationResult{
Valid: false,
}
// Call ToFlag method for invalid result
flagInvalid := evaluationResultInvalid.ToFlag()
// Verify that the returned flag is not nil
assert.NotNil(t, flagInvalid, "ToFlag should return a non-nil flag for an invalid result")
// Verify the validity of the returned flag
assert.Equal(t, false, flagInvalid.Valid, "Expected validity of flag to match EvaluationResult validity for an invalid result")
}