Skip to content

Conversation

@shimks
Copy link
Contributor

@shimks shimks commented May 14, 2018

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@shimks shimks requested review from bajtos and raymondfeng as code owners May 14, 2018 18:47
@shimks shimks force-pushed the json-schema-tests branch from 1e39c1e to 9b0061e Compare May 15, 2018 14:48
expect(modelToJsonSchema(NoModelMeta)).to.eql({});
const emptyJson = modelToJsonSchema(Empty);
const noModelMetaJson = modelToJsonSchema(NoModelMeta);
expect(emptyJson).to.eql({});
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems redundant. Won't expectValidJsonSchema check if it's {} or not?

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 guess it is redundant. On the other hand though, what if someday they decide {} is not a valid JSON Schema? In that case, we'd want this to test to fail while still maintaining that emptyJson is an empty object and that {} is not a valid JSON Schema

Copy link
Contributor

Choose a reason for hiding this comment

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

Unlikely, but I'm ok with the redundancy.

@@ -0,0 +1,63 @@
import {JsonSchema} from '../../src';
Copy link
Contributor

Choose a reason for hiding this comment

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

Move import below the Copyright notice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

Good start!

const validate = new Ajv().compile(
require('ajv/lib/refs/json-schema-draft-06.json'),
);
expect(validate(schema)).to.be.true();
Copy link
Member

Choose a reason for hiding this comment

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

+1 for creating a shared function expectValidJsonSchema.

When the validation fails because the schema is not valid, what information is provided to the user (LB4 framework developer) to help them troubleshoot the problem?

IIUC, the test prints "expected false to be true", which is not helpful at all :(

According to https://www.npmjs.com/package/ajv#validation-errors, validation errors are provided as an array in validate.errors. There is also ajv.errorsText() function that can be used to build a user-friendly message.

I am proposing to rework this check as follows:

const ajv = new Ajv();
const validate = ajv.compile(require('ajv/lib/refs/json-schema-draft-06.json'));
const isValid = validate(schema);
const result = isValid ? 'JSON Schema is valid' : ajv.errorsText(validate.errors);
expect(result).to.equal('JSON Schema is valid');

An example test failure:

      AssertionError: expected 'data.properties should be object' to be 'JSON Schema is valid'
      + expected - actual

      -data.properties should be object
      +JSON Schema is valid

describe('JsonSchema interface', () => {
/**
* The classes below are declared as tests for the Interfaces.
* The TS Compiler will complain if an interface changes with a way
Copy link
Member

Choose a reason for hiding this comment

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

"with a way" => "in a way"?

@shimks shimks force-pushed the json-schema-tests branch 2 times, most recently from 81abb30 to 71e0375 Compare May 22, 2018 15:49
});
});

function expectValidJsonSchema(schema: JsonSchema) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we consider exporting this as a util function from this package?

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 means that we'd have to have @loopback/testlab and ajv as dependencies for @loopback/repository. Looking at https://bundlephobia.com/result?p=ajv@6.5.0, would you consider ajv to be sizable?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not that big imo but I'm ok not exporting this as a utility function.

Copy link
Member

Choose a reason for hiding this comment

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

@shimks I think @virkt25 was suggesting to export expectValidJsonSchema from repository-json-schema, not testlab package?

At one hand, a readily-available helper for verifying JSON Schema may be helpful outside of this module tests too. On the other hand, exporting a test helper from a non-test-helper module is tricky as we may need to move dev-dependencies to regular dependencies. As for testlab, it should not become a kitchen-sink for everything testing related.

In the light of what I wrote above, I think we should not be exporting this utility function.

});
});

function expectValidJsonSchema(schema: JsonSchema) {
Copy link
Member

Choose a reason for hiding this comment

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

@shimks I think @virkt25 was suggesting to export expectValidJsonSchema from repository-json-schema, not testlab package?

At one hand, a readily-available helper for verifying JSON Schema may be helpful outside of this module tests too. On the other hand, exporting a test helper from a non-test-helper module is tricky as we may need to move dev-dependencies to regular dependencies. As for testlab, it should not become a kitchen-sink for everything testing related.

In the light of what I wrote above, I think we should not be exporting this utility function.

@shimks shimks force-pushed the json-schema-tests branch from 71e0375 to 29e0cac Compare May 25, 2018 00:46
@shimks shimks merged commit f5e3e94 into master May 25, 2018
@shimks shimks deleted the json-schema-tests branch May 25, 2018 02:34
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.

Add in tests for JSON Schemas being produced

4 participants