-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added test dependencies * Added unittests for validating generated schemas * Added Prettier config file * Updated readme * Added workflows for build and test actions * Update test gh action name * upport for typescript on tests * Refactored schema tests
- Loading branch information
1 parent
aff22eb
commit 2703419
Showing
8 changed files
with
2,082 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: Build | ||
|
||
# Run on pushes to main or PRs | ||
on: | ||
# Pull request hook without any config. Launches for every pull request | ||
pull_request: | ||
# Launches for pushes to main or dev | ||
push: | ||
branches: | ||
- main | ||
# Launches build when release is published | ||
release: | ||
types: [ published ] | ||
|
||
jobs: | ||
build: | ||
name: Build Package | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Cancel Previous Runs | ||
uses: styfle/[email protected] | ||
with: | ||
access_token: ${{ github.token }} | ||
|
||
- name: Remove broken apt repos [Ubuntu] | ||
if: ${{ matrix.os }} == 'ubuntu-latest' | ||
run: | | ||
for apt_file in `grep -lr microsoft /etc/apt/sources.list.d/`; do sudo rm $apt_file; done | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
|
||
- uses: actions/cache@v2 | ||
with: | ||
path: '**/node_modules' | ||
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }} | ||
|
||
- run: | | ||
# Due to some dependencies yarn may randomly throw an error about invalid cache | ||
# This approach is taken from https://github.com/yarnpkg/yarn/issues/7212#issuecomment-506155894 to fix the issue | ||
# Another approach is to install with flag --network-concurrency 1, but this will make the installation pretty slow (default value is 8) | ||
mkdir .yarncache | ||
yarn install --cache-folder ./.yarncache --frozen-lockfile | ||
rm -rf .yarncache | ||
yarn cache clean | ||
- name: Build app | ||
run: yarn build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Unit tests | ||
on: [ push, pull_request ] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Cancel Previous Runs | ||
uses: styfle/[email protected] | ||
with: | ||
access_token: ${{ github.token }} | ||
|
||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
|
||
- name: Yarn cache | ||
uses: actions/cache@v2 | ||
with: | ||
path: '**/node_modules' | ||
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }} | ||
|
||
- name: Yarn install | ||
run: | | ||
mkdir .yarncache | ||
yarn install --cache-folder ./.yarncache --frozen-lockfile | ||
rm -rf .yarncache | ||
yarn cache clean | ||
- name: Run tests | ||
run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"printWidth": 120 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
import Ajv, { ValidateFunction } from 'ajv' | ||
|
||
import schemaV0_1_0 from '../schemas/v0.1.0.json' | ||
import schemaV0_2_0 from '../schemas/v0.2.0.json' | ||
import schemaV0_3_0 from '../schemas/v0.3.0.json' | ||
import schemaV0_4_0 from '../schemas/v0.4.0.json' | ||
|
||
const ADDRESS = '0xb6BAd41ae76A11D10f7b0E664C5007b908bC77C9' | ||
const REFERRER_V0_1_0 = { address: ADDRESS, version: '0.1.0' } | ||
const QUOTE_V0_1_0 = { sellAmount: '123123', buyAmount: '1314123', version: '0.1.0' } | ||
const QUOTE_V0_2_0 = { slippageBips: '1', version: '0.2.0' } | ||
|
||
const MISSING_VERSION_ERROR = [ | ||
{ | ||
instancePath: '', | ||
keyword: 'required', | ||
message: "must have required property 'version'", | ||
params: { missingProperty: 'version' }, | ||
schemaPath: '#/required', | ||
}, | ||
] | ||
|
||
describe('Schema v0.1.0', () => { | ||
const ajv = new Ajv() | ||
const validator = ajv.compile(schemaV0_1_0) | ||
|
||
const BASE_DOCUMENT = { | ||
version: '0.1.0', | ||
metadata: {}, | ||
} | ||
|
||
test('Minimal valid schema', _buildAssertValidFn(validator, BASE_DOCUMENT)) | ||
|
||
test('Missing required fields', _buildAssertInvalidFn(validator, {}, MISSING_VERSION_ERROR)) | ||
|
||
test( | ||
'With referrer metadata', | ||
_buildAssertValidFn(validator, { | ||
...BASE_DOCUMENT, | ||
appCode: 'MyApp', | ||
metadata: { | ||
referrer: REFERRER_V0_1_0, | ||
}, | ||
}) | ||
) | ||
|
||
test( | ||
'With invalid referrer metadata', | ||
_buildAssertInvalidFn( | ||
validator, | ||
{ | ||
...BASE_DOCUMENT, | ||
appCode: 'MyApp', | ||
metadata: { | ||
referrer: { address: '0xas', version: '0.1.0' }, | ||
}, | ||
}, | ||
[ | ||
{ | ||
instancePath: '/metadata/referrer/address', | ||
keyword: 'pattern', | ||
message: 'must match pattern "^0x[a-fA-F0-9]{40}$"', | ||
params: { pattern: '^0x[a-fA-F0-9]{40}$' }, | ||
schemaPath: '#/properties/metadata/properties/referrer/properties/address/pattern', | ||
}, | ||
] | ||
) | ||
) | ||
}) | ||
|
||
describe('Schema v0.2.0', () => { | ||
const ajv = new Ajv() | ||
const validator = ajv.compile(schemaV0_2_0) | ||
|
||
const BASE_DOCUMENT = { | ||
version: '0.2.0', | ||
metadata: {}, | ||
} | ||
|
||
test('Minimal valid schema', _buildAssertValidFn(validator, BASE_DOCUMENT)) | ||
|
||
test('Missing required fields', _buildAssertInvalidFn(validator, {}, MISSING_VERSION_ERROR)) | ||
|
||
test( | ||
'With referrer v0.1.0 and quote v0.1.0 metadata', | ||
_buildAssertValidFn(validator, { | ||
...BASE_DOCUMENT, | ||
appCode: 'MyApp', | ||
metadata: { | ||
referrer: REFERRER_V0_1_0, | ||
quote: QUOTE_V0_1_0, | ||
}, | ||
}) | ||
) | ||
|
||
test( | ||
'With invalid quote metadata v0.1.0', | ||
_buildAssertInvalidFn( | ||
validator, | ||
{ | ||
...BASE_DOCUMENT, | ||
appCode: 'MyApp', | ||
metadata: { | ||
referrer: REFERRER_V0_1_0, | ||
quote: { sellAmount: 'xx12d', buyAmount: 0.13, version: '0.1.0' }, | ||
}, | ||
}, | ||
[ | ||
{ | ||
instancePath: '/metadata/quote/sellAmount', | ||
keyword: 'pattern', | ||
message: 'must match pattern "^\\d+$"', | ||
params: { pattern: '^\\d+$' }, | ||
schemaPath: '#/properties/metadata/properties/quote/properties/buyAmount/pattern', | ||
}, | ||
] | ||
) | ||
) | ||
}) | ||
|
||
describe('Schema v0.3.0', () => { | ||
const ajv = new Ajv() | ||
const validator = ajv.compile(schemaV0_3_0) | ||
|
||
const BASE_DOCUMENT = { | ||
version: '0.3.0', | ||
metadata: {}, | ||
} | ||
|
||
test('Minimal valid schema', _buildAssertValidFn(validator, BASE_DOCUMENT)) | ||
|
||
test('Missing required fields', _buildAssertInvalidFn(validator, {}, MISSING_VERSION_ERROR)) | ||
|
||
test( | ||
'With environment and full metadata (both v0.1.0)', | ||
_buildAssertValidFn(validator, { | ||
...BASE_DOCUMENT, | ||
appCode: 'MyApp', | ||
environment: 'prod', | ||
metadata: { | ||
referrer: REFERRER_V0_1_0, | ||
quote: QUOTE_V0_1_0, | ||
}, | ||
}) | ||
) | ||
}) | ||
|
||
describe('Schema v0.4.0', () => { | ||
const ajv = new Ajv() | ||
const validator = ajv.compile(schemaV0_4_0) | ||
|
||
const BASE_DOCUMENT = { | ||
version: '0.4.0', | ||
metadata: {}, | ||
} | ||
|
||
test('Minimal valid schema', _buildAssertValidFn(validator, BASE_DOCUMENT)) | ||
|
||
test('Missing required fields', _buildAssertInvalidFn(validator, {}, MISSING_VERSION_ERROR)) | ||
|
||
test( | ||
'With quote metadata v0.2.0', | ||
_buildAssertValidFn(validator, { | ||
...BASE_DOCUMENT, | ||
appCode: 'MyApp', | ||
environment: 'prod', | ||
metadata: { | ||
referrer: REFERRER_V0_1_0, | ||
quote: QUOTE_V0_2_0, | ||
}, | ||
}) | ||
) | ||
|
||
test( | ||
'With invalid quote metadata v0.2.0', | ||
_buildAssertInvalidFn( | ||
validator, | ||
{ | ||
...BASE_DOCUMENT, | ||
appCode: 'MyApp', | ||
environment: 'prod', | ||
metadata: { | ||
referrer: REFERRER_V0_1_0, | ||
quote: QUOTE_V0_1_0, | ||
}, | ||
}, | ||
[ | ||
{ | ||
instancePath: '/metadata/quote', | ||
keyword: 'required', | ||
message: "must have required property 'slippageBips'", | ||
params: { missingProperty: 'slippageBips' }, | ||
schemaPath: '#/properties/metadata/properties/quote/required', | ||
}, | ||
] | ||
) | ||
) | ||
}) | ||
|
||
function _buildAssertValidFn(validator: ValidateFunction, doc: any) { | ||
return () => { | ||
// when | ||
const actual = validator(doc) | ||
// then | ||
expect(actual).toBeTruthy() | ||
} | ||
} | ||
|
||
function _buildAssertInvalidFn(validator: ValidateFunction, doc: any, errors: any) { | ||
return () => { | ||
// when | ||
const actual = validator(doc) | ||
// then | ||
expect(actual).toBeFalsy() | ||
expect(validator.errors).toEqual(errors) | ||
} | ||
} |
Oops, something went wrong.