Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #357 from Financial-Times/rhys/test-schema-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys authored Apr 7, 2021
2 parents 620b821 + 2432ad6 commit 8331805
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fetch = require('node-fetch');
const { getApp } = require('..');

describe('schema polling startup', () => {
beforeAll(() => {
Expand All @@ -14,6 +13,7 @@ describe('schema polling startup', () => {
process.env.TEST_STARTUP = true;
process.env.TREECREEPER_SCHEMA_URL = 'http://example.com';
fetch.mock('*', {});
const { getApp } = require('..');
let initialised = false;
const promiseOfApp = getApp({ schemaOptions: { updateMode: 'poll' } });
expect(initialised).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fetch = require('node-fetch');
const schemaPublisher = require('@financial-times/tc-schema-publisher');
const request = require('supertest');
const { getApp } = require('..');

let getApp;

jest.useFakeTimers();
jest.mock('@financial-times/tc-schema-publisher', () => ({
Expand Down Expand Up @@ -84,6 +85,7 @@ describe('schema polling updates', () => {
{ overwriteRoutes: false },
)
.catch(200);
({ getApp } = require('..'));
app = await getApp({
republishSchemaPrefix: 'custom',
republishSchema: true,
Expand Down
50 changes: 50 additions & 0 deletions packages/tc-schema-publisher/scripts/__tests__/deploy-test.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const mockS3Instance = {
upload: jest.fn().mockReturnThis(),
promise: jest.fn(),
};

jest.mock('aws-sdk', () => {
return { S3: jest.fn(() => mockS3Instance) };
});

const { deploy } = require('../deploy');

describe('tc-schema-sdk/deploy test', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('can deploy with test properties', async () => {
require('../deploy');

await deploy({
schemaDirectory: `packages/tc-schema-publisher/scripts/__tests__/schema`,
bucketName: 'blah',
includeTestDefinitions: true,
env: 'latest',
});

expect(mockS3Instance.upload).toHaveBeenCalled();
const body = mockS3Instance.upload.mock.calls[0][0].Body;
let content = '';
body.on('data', chunk => {
content += chunk.toString('utf8');
});
await new Promise(res => {
body.on('end', res);
});
const { schema } = JSON.parse(content);
expect(schema.types.length).toEqual(2);
expect(schema.types[0].name).toEqual('TypeA');
expect(schema.types[1].name).toEqual('TypeB');
expect(Object.keys(schema.types[0].properties)).toEqual([
'code',
'stringPropertyA',
'stringPropertyB',
]);
expect(Object.keys(schema.enums)).toEqual(['AnEnum', 'ATestEnum']);
expect(Object.keys(schema.typeHierarchy)).toEqual(['prod', 'test']);
expect(schema.typeHierarchy.prod.types).toEqual(['TypeA']);
expect(schema.typeHierarchy.test.types).toEqual(['TypeB']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ AnEnum:
Another example enum
options:
First: First option
ATestEnum:
description: |
Another example enum
isTest: true
options:
First: First option
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ prod:
description: Prod types
types:
- TypeA
test:
label: Test
description: Test types
types:
- TypeB
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ properties:
type: Word
description: stringPropertyA description.
label: stringPropertyA label
stringPropertyB:
type: Word
description: stringPropertyB description.
label: stringPropertyB label
isTest: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: TypeB
description: type b
isTest: true
properties:
code:
type: Code
unique: true
canIdentify: true
description: Code description.
label: Code label
pattern: CODE
5 changes: 5 additions & 0 deletions packages/tc-schema-publisher/scripts/command.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ program
'S3 bucket name which you want to upload. (default: "process.env.TREECREEPER_SCHEMA_BUCKET")',
process.env.TREECREEPER_SCHEMA_BUCKET,
)
.option(
'--include-test-definitions <bool>',
'Whether to include definitions intended only for the test environment in the release. (default: false))',
false,
)
.option('-E, --env <env>', 'specify publish environment', 'latest')
.version(pkg.version)
.action(action)
Expand Down
9 changes: 7 additions & 2 deletions packages/tc-schema-publisher/scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const { SDK } = require('@financial-times/tc-schema-sdk');
const { sendSchemaToS3 } = require('..');

const deploy = async command => {
const { schemaDirectory, bucketName, env } = command;
const {
schemaDirectory,
bucketName,
env,
includeTestDefinitions,
} = command;

try {
if (!bucketName) {
Expand All @@ -28,7 +33,7 @@ const deploy = async command => {
if (!stat.isDirectory()) {
throw new Error('schema directory is not a directory');
}
const schema = new SDK({ schemaDirectory });
const schema = new SDK({ schemaDirectory, includeTestDefinitions });
await sendSchemaToS3(env, bucketName, schema);
console.log('successfully deployed');
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/tc-schema-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ The package exports an `init(options)` function, that takes the following option
- `updateMode` - 'poll' or 'stale'. 'poll' will start polling on an interval for schema updates, whereas 'stale' will fetch whenever a user calls the sdk's `refresh()` method and the schema is older than the `ttl`
- `logger` (default `console`) - choice of logger to use in the sdk
- `version` - used to specify the version of the schema being used. Only used in tests

One of `schemaDirectory`, `schemaData` or `schemaBaseUrl` must be defined. If `schemaBaseUrl` is defined, then `updateMode` must also be defined.
- `includeTestDefinitions` - (default: `false`) a flag to indicate whether to use schema definitions that are flagged as test only using `isTest: true`
One of `schemaDirectory`, `schemaData` or `schemaBaseUrl` must be defined. If `schemaBaseUrl` is defined, then `updateMode` must also be defined.

### Update APIs

Expand Down
142 changes: 142 additions & 0 deletions packages/tc-schema-sdk/__tests__/test-schema-properties.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const { SDK } = require('../sdk');

const schemaFixture = {
types: [
{
name: 'TypeA',
description: 'type a',
properties: {
code: {
type: 'Code',
unique: true,
canIdentify: true,
description: 'Code description.',
label: 'Code label',
pattern: 'CODE',
},
stringPropertyA: {
type: 'Word',
description: 'stringPropertyA description.',
label: 'stringPropertyA label',
},
stringPropertyB: {
type: 'Word',
description: 'stringPropertyB description.',
label: 'stringPropertyB label',
isTest: true,
},
},
},
{
name: 'TypeB',
description: 'type b',
isTest: true,
properties: {
code: {
type: 'Code',
unique: true,
canIdentify: true,
description: 'Code description.',
label: 'Code label',
pattern: 'CODE',
},
},
},
],
relationshipTypes: [],
stringPatterns: {
CODE: '^(?=.{2,64}$)[a-z0-9]+(?:-[a-z0-9]+)*$',
},
enums: {
AnEnum: {
description: 'Another example enum\n',
options: {
First: 'First option',
},
},
ATestEnum: {
description: 'Another example enum\n',
isTest: true,
options: {
First: 'First option',
},
},
},
primitiveTypes: {
Code: {
graphql: 'String',
component: 'Text',
},
Word: {
graphql: 'String',
component: 'Text',
},
Document: {
graphql: 'String',
component: 'LargeText',
},
Url: {
graphql: 'String',
component: 'Text',
},
},
typeHierarchy: {
prod: {
label: 'Prod',
description: 'Prod types',
types: ['TypeA'],
},
test: {
label: 'Test',
description: 'Test types',
types: ['TypeB'],
},
},
};

describe('test schema properties', () => {
it('excludes test properties by default', async () => {
const schema = new SDK({ schemaData: { schema: schemaFixture } });
await schema.ready();
expect(schema.getTypes().length).toEqual(1);
expect(schema.getTypes()[0].name).toEqual('TypeA');
expect(Object.keys(schema.getTypes()[0].properties)).toEqual([
'code',
'stringPropertyA',
]);
expect(Object.keys(schema.getEnums())).toEqual(['AnEnum']);
expect(Object.keys(schema.getTypes({ grouped: true }))).toEqual([
'prod',
]);
expect(schema.getTypes({ grouped: true }).prod.types[0].name).toEqual(
'TypeA',
);
});

it('includes test properties on demand', async () => {
const schema = new SDK({
schemaData: { schema: schemaFixture },
includeTestDefinitions: true,
});
await schema.ready();
expect(schema.getTypes().length).toEqual(2);
expect(schema.getTypes()[0].name).toEqual('TypeA');
expect(schema.getTypes()[1].name).toEqual('TypeB');
expect(Object.keys(schema.getTypes()[0].properties)).toEqual([
'code',
'stringPropertyA',
'stringPropertyB',
]);
expect(Object.keys(schema.getEnums())).toEqual(['AnEnum', 'ATestEnum']);
expect(Object.keys(schema.getTypes({ grouped: true }))).toEqual([
'prod',
'test',
]);
expect(schema.getTypes({ grouped: true }).prod.types[0].name).toEqual(
'TypeA',
);
expect(schema.getTypes({ grouped: true }).test.types[0].name).toEqual(
'TypeB',
);
});
});
23 changes: 15 additions & 8 deletions packages/tc-schema-sdk/data-accessors/__tests__/types.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const typeAccessor = jest.fn();
typeAccessor.mockImplementation(name => ({
name: `${name} - retrieved`,
name: `${name}`,
retrieved: true,
}));

jest.doMock('../../data-accessors/type', () => {
Expand Down Expand Up @@ -28,10 +29,12 @@ describe('get-types', () => {

expect(types).toEqual([
{
name: 'Type1 - retrieved',
name: 'Type1',
retrieved: true,
},
{
name: 'Type2 - retrieved',
name: 'Type2',
retrieved: true,
},
]);

Expand All @@ -44,7 +47,7 @@ describe('get-types', () => {
});

describe('with hierarchy', () => {
it('expects to be returned in order of type hiererchy', () => {
it('expects to be returned in order of type hierarchy', () => {
const types = new SDK({
schemaData: {
schema: {
Expand All @@ -68,10 +71,12 @@ describe('get-types', () => {

expect(types).toEqual([
{
name: 'Type2 - retrieved',
name: 'Type2',
retrieved: true,
},
{
name: 'Type1 - retrieved',
name: 'Type1',
retrieved: true,
},
]);
});
Expand Down Expand Up @@ -102,14 +107,16 @@ describe('get-types', () => {
category1: {
types: [
{
name: 'Type2 - retrieved',
name: 'Type2',
retrieved: true,
},
],
},
category2: {
types: [
{
name: 'Type1 - retrieved',
name: 'Type1',
retrieved: true,
},
],
},
Expand Down
Loading

0 comments on commit 8331805

Please sign in to comment.