Skip to content
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

Merged
merged 2 commits into from
Jun 25, 2018

Conversation

simitt
Copy link
Contributor

@simitt simitt commented Jun 19, 2018

  • 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.

This PR is based on updating github.com/santhosh-tekuri/jsonschema #1013 and on removing archived library github.com/fatih/set #1014.

@simitt simitt requested a review from graphaelli June 19, 2018 09:39
@simitt simitt force-pushed the improve-json-spec-tests branch from ff076ff to 9194d6c Compare June 19, 2018 11:05
@@ -50,8 +50,7 @@
"regexProperties": true,
"patternProperties": {
"^[^.*\"]*$": {
"$ref": "mark.json",
"maxLength": 1024
Copy link
Contributor Author

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.

@simitt simitt force-pushed the improve-json-spec-tests branch from 9194d6c to 0002caa Compare June 20, 2018 07:32
Copy link
Member

@graphaelli graphaelli left a 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)
Copy link
Member

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.

Copy link
Contributor Author

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.

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{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existence

)

type JsonSchemaTestData struct {
Proc processor.Processor
ValidPayloadPath string
Copy link
Member

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.

))

payload, err := loader.LoadData(td.ValidPayloadPath)
assert.NoError(t, err, fmt.Sprintf("File %s not loaded", td.ValidPayloadPath))
Copy link
Member

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

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


// load payload
payload, err := loader.LoadData(td.ValidPayloadPath)
assert.NoError(t, err, fmt.Sprintf("File %s not loaded", td.ValidPayloadPath))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

payload = iterateMap(payload, "", fnKey, keyToChange, val, upsertFn).(obj)
}
err = td.Proc.Validate(payload)
assert.NoError(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

Copy link
Member

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?

Copy link
Contributor Author

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.


type TestData struct {
Key string
Invalid []interface{}
Copy link
Member

@graphaelli graphaelli Jun 21, 2018

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

Copy link
Contributor Author

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.

@simitt
Copy link
Contributor Author

simitt commented Jun 22, 2018

@graphaelli I'll add a rework to check for error messages and improve the assertions. I'll close the PR for now to avoid the noise, and reopen later.

@simitt simitt closed this Jun 22, 2018
@simitt
Copy link
Contributor Author

simitt commented Jun 22, 2018

I incorporated your suggestions @graphaelli:

  • fail fast when assertion fails for precondition
  • add expected error messages to validation failure checks
  • add more comments

@simitt simitt reopened this Jun 22, 2018
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/
Copy link
Member

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.

Copy link
Contributor Author

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.

type ProcessorSetup struct {
Proc processor.Processor
// path to payload that should be a full and valid example
FullPayloadPath string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Invalid Invalid
Condition Condition
}
type Invalid = map[string][]interface{}
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, changed

@@ -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)
Copy link
Member

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?

@graphaelli
Copy link
Member

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.

@simitt
Copy link
Contributor Author

simitt commented Jun 25, 2018

Thanks for the review @graphaelli. I liked your suggestions and pushed the changes.

Copy link
Member

@graphaelli graphaelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work

simitt added 2 commits June 25, 2018 17:18
* 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.
@simitt simitt force-pushed the improve-json-spec-tests branch from d00f064 to e0c28ef Compare June 25, 2018 15:18
@simitt simitt merged commit 8a7016a into elastic:master Jun 25, 2018
@roncohen
Copy link
Contributor

needs a backport to 6.x ?

@simitt simitt deleted the improve-json-spec-tests branch June 28, 2018 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants