-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bugfix Json Schema, Improve json schema tests #1015
Conversation
ff076ff
to
9194d6c
Compare
@@ -50,8 +50,7 @@ | |||
"regexProperties": true, | |||
"patternProperties": { | |||
"^[^.*\"]*$": { | |||
"$ref": "mark.json", | |||
"maxLength": 1024 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This length restriction did not make sense, as it doesn't refer to the properties key but the value and the value is a mark
.
9194d6c
to
0002caa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some comments collected so far, still working through parts of the pull request
|
||
for _, d := range testData { | ||
data, err := loader.LoadData(d.File) | ||
assert.Nil(t, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.NoError
will format falied assertions nicely. This should also continue
or otherwise stop working with data
if err != nil
(or more handy: ! assert.NoError
), otherwise it will try to validate invalid data
even though the test already failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT about introducing testify/require, it implements the same interface as testify.assert
but fails fast. I think this would be perfect for assertions that should abort the whole test, like this.
tests/json_schema.go
Outdated
Absence []string | ||
// If requirements for a field apply in case of | ||
// anothers key specific values, add the key and its values. | ||
Existance map[string]interface{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Existence
tests/json_schema.go
Outdated
) | ||
|
||
type JsonSchemaTestData struct { | ||
Proc processor.Processor | ||
ValidPayloadPath string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a comment here too? I'm not clear on whether any valid payload will do or minimal or fully filled in? Probably will figure out during this review but would be handy for new devs.
tests/json_schema.go
Outdated
)) | ||
|
||
payload, err := loader.LoadData(td.ValidPayloadPath) | ||
assert.NoError(t, err, fmt.Sprintf("File %s not loaded", td.ValidPayloadPath)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should exit early if ! assert.NoError
tests/json_schema.go
Outdated
func (td *JsonSchemaTestData) testKeywordLimitation(t *testing.T) { | ||
// fetch keyword restricted field names from ES template | ||
keywordFields, err := fetchFlattenedFieldNames(td.LoadTemplatePaths, addKeywordFields) | ||
assert.NoError(t, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
tests/json_schema.go
Outdated
|
||
// load payload | ||
payload, err := loader.LoadData(td.ValidPayloadPath) | ||
assert.NoError(t, err, fmt.Sprintf("File %s not loaded", td.ValidPayloadPath)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
tests/json_schema.go
Outdated
payload = iterateMap(payload, "", fnKey, keyToChange, val, upsertFn).(obj) | ||
} | ||
err = td.Proc.Validate(payload) | ||
assert.NoError(t, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here too
tests/json_schema.go
Outdated
@@ -27,7 +281,8 @@ type Mapping struct { | |||
} | |||
|
|||
func TestPayloadAttributesInSchema(t *testing.T, name string, undocumentedAttrs *Set, schema string) { | |||
payload, _ := loader.LoadValidData(name) | |||
payload, err := loader.LoadValidData(name) | |||
assert.NoError(t, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That require
is neat - missed one or still applying those?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed that one - changing it now.
tests/json_schema.go
Outdated
|
||
type TestData struct { | ||
Key string | ||
Invalid []interface{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we lose the checks for why the data is invalid with this? Not that comparing strings wasn't great but it was actually useful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very valid input, I'll come up with something.
@graphaelli I'll add a rework to check for error messages and improve the |
I incorporated your suggestions @graphaelli:
|
Makefile
Outdated
@cp tests/data/valid/transaction/minimal_payload.json docs/data/intake-api/generated/transaction/ | ||
@cp tests/data/valid/transaction/minimal_span.json docs/data/intake-api/generated/transaction/ | ||
@cp tests/data/valid/sourcemap/bundle.js.map docs/data/intake-api/generated/sourcemap/ | ||
@cp tests/data/error/payload.json docs/data/intake-api/generated/error/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're moving things around anyway, how about tests/data
-> testdata
to give the go tool less to look at? This would take some work as sometimes we refer to paths from within the tests directory, sometimes from top level package path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea, will add it in an extra commit.
tests/json_schema.go
Outdated
type ProcessorSetup struct { | ||
Proc processor.Processor | ||
// path to payload that should be a full and valid example | ||
FullPayloadPath string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
tests/json_schema.go
Outdated
Invalid Invalid | ||
Condition Condition | ||
} | ||
type Invalid = map[string][]interface{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took me a little bit to understand what is happening here - I think this could be a more specific structure that captures msg
and values
and then include a []Invalid
in SchemaTestData
- wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, changed
tests/json_schema.go
Outdated
@@ -27,7 +281,8 @@ type Mapping struct { | |||
} | |||
|
|||
func TestPayloadAttributesInSchema(t *testing.T, name string, undocumentedAttrs *Set, schema string) { | |||
payload, _ := loader.LoadValidData(name) | |||
payload, err := loader.LoadValidData(name) | |||
assert.NoError(t, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That require
is neat - missed one or still applying those?
Nitpicking - is the 80 character wrapping new? It seems strange that some lines are wrapped at 80 and others at 120 - I think they should all be at 120. |
Thanks for the review @graphaelli. I liked your suggestions and pushed the changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice work
* Fix bugs uncovered with improved tests * Do automatic validation for keyword limitations * Remove files for invalid payloads, instead create invalid payloads out of valid payload * Allow to define simple and conditional rules for testing payloads.
d00f064
to
e0c28ef
Compare
needs a backport to 6.x ? |
This PR is based on updating
github.com/santhosh-tekuri/jsonschema
#1013 and on removing archived libraryjackfan.us.kg/fatih/set
#1014.