Skip to content

Commit 1b40dc7

Browse files
authored
Add unit test for disabled field (#2224)
* Add unit test for disabled field * Fix the test, the fields errors order is not guaranteed
1 parent b912137 commit 1b40dc7

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"disabled": {
3+
"id": "42",
4+
"status": "ok"
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"enabled": {
3+
"id": "42",
4+
"status": "ok"
5+
}
6+
}

internal/fields/testdata/fields/fields.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,6 @@
9595
- name: attributes
9696
type: object
9797
object_type: keyword
98+
- name: disabled
99+
type: object
100+
enabled: false

internal/fields/validate_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,36 @@ func TestValidate_ObjectTypeWithoutWildcard(t *testing.T) {
8383
})
8484
}
8585

86+
func TestValidate_DisabledParent(t *testing.T) {
87+
validator, err := CreateValidatorForDirectory("testdata",
88+
WithDisabledDependencyManagement())
89+
require.NoError(t, err)
90+
require.NotNil(t, validator)
91+
92+
t.Run("disabled", func(t *testing.T) {
93+
e := readSampleEvent(t, "testdata/disabled.json")
94+
errs := validator.ValidateDocumentBody(e)
95+
require.Empty(t, errs)
96+
})
97+
}
98+
99+
func TestValidate_EnabledNotMappedError(t *testing.T) {
100+
validator, err := CreateValidatorForDirectory("testdata",
101+
WithDisabledDependencyManagement())
102+
require.NoError(t, err)
103+
require.NotNil(t, validator)
104+
105+
t.Run("enabled", func(t *testing.T) {
106+
e := readSampleEvent(t, "testdata/enabled_not_mapped.json")
107+
errs := validator.ValidateDocumentBody(e)
108+
if assert.Len(t, errs, 2) {
109+
for i := 0; i < 2; i++ {
110+
assert.Contains(t, []string{`field "enabled.id" is undefined`, `field "enabled.status" is undefined`}, errs[i].Error())
111+
}
112+
}
113+
})
114+
}
115+
86116
func TestValidate_WithNumericKeywordFields(t *testing.T) {
87117
validator, err := CreateValidatorForDirectory("testdata",
88118
WithNumericKeywordFields([]string{

0 commit comments

Comments
 (0)