Skip to content

Commit

Permalink
Add unit test for disabled field (#2224)
Browse files Browse the repository at this point in the history
* Add unit test for disabled field

* Fix the test, the fields errors order is not guaranteed
  • Loading branch information
aleksmaus authored Nov 13, 2024
1 parent b912137 commit 1b40dc7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/fields/testdata/disabled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"disabled": {
"id": "42",
"status": "ok"
}
}
6 changes: 6 additions & 0 deletions internal/fields/testdata/enabled_not_mapped.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"enabled": {
"id": "42",
"status": "ok"
}
}
3 changes: 3 additions & 0 deletions internal/fields/testdata/fields/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@
- name: attributes
type: object
object_type: keyword
- name: disabled
type: object
enabled: false
30 changes: 30 additions & 0 deletions internal/fields/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ func TestValidate_ObjectTypeWithoutWildcard(t *testing.T) {
})
}

func TestValidate_DisabledParent(t *testing.T) {
validator, err := CreateValidatorForDirectory("testdata",
WithDisabledDependencyManagement())
require.NoError(t, err)
require.NotNil(t, validator)

t.Run("disabled", func(t *testing.T) {
e := readSampleEvent(t, "testdata/disabled.json")
errs := validator.ValidateDocumentBody(e)
require.Empty(t, errs)
})
}

func TestValidate_EnabledNotMappedError(t *testing.T) {
validator, err := CreateValidatorForDirectory("testdata",
WithDisabledDependencyManagement())
require.NoError(t, err)
require.NotNil(t, validator)

t.Run("enabled", func(t *testing.T) {
e := readSampleEvent(t, "testdata/enabled_not_mapped.json")
errs := validator.ValidateDocumentBody(e)
if assert.Len(t, errs, 2) {
for i := 0; i < 2; i++ {
assert.Contains(t, []string{`field "enabled.id" is undefined`, `field "enabled.status" is undefined`}, errs[i].Error())
}
}
})
}

func TestValidate_WithNumericKeywordFields(t *testing.T) {
validator, err := CreateValidatorForDirectory("testdata",
WithNumericKeywordFields([]string{
Expand Down

0 comments on commit 1b40dc7

Please sign in to comment.