Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Aug 1, 2024
1 parent 2cdd2f7 commit c9407a1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13,596 deletions.
2 changes: 1 addition & 1 deletion src/models/AsyncapiV2Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class AsyncapiV2Schema {
deprecated?: boolean;
//Extensions
[k: string]: any; // eslint-disable-line no-undef

/**
* Takes a deep copy of the input object and converts it to an instance of AsyncapiV2Schema.
*
Expand Down
17 changes: 17 additions & 0 deletions test/TestUtils/GeneralUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ export async function execCommand(
return Promise.reject(`${e.stack}; Stdout: ${e.stdout}`);
}
}
/**
* Ensure object does not contain undefined properties when matching outputs
*/
export function removeEmptyPropertiesFromObjects(obj: any): any {
if(typeof obj !== 'object') return obj;
Object.keys(obj).forEach(key => {
if (obj[key] && typeof obj[key] === 'object') {
removeEmptyPropertiesFromObjects(obj[key]);
if (Object.keys(obj[key]).length === 0) {
delete obj[key];
}
} else if (obj[key] === undefined) {
delete obj[key];
}
});
return obj;
}
3 changes: 2 additions & 1 deletion test/interpreter/Intepreter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommonModel } from '../../src';
import { Interpreter } from '../../src/interpreter/Interpreter';
import { removeEmptyPropertiesFromObjects } from '../TestUtils/GeneralUtils';

describe('Interpreter', () => {
test('should return multiple separate models', () => {
Expand All @@ -14,6 +15,6 @@ describe('Interpreter', () => {
};
const interpreter = new Interpreter();
const interpretedModel = interpreter.interpret(schema);
expect(interpretedModel).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(interpretedModel)).toMatchSnapshot();
});
});
47 changes: 0 additions & 47 deletions test/interpreter/__snapshots__/Intepreter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@ exports[`Interpreter should return multiple separate models 1`] = `
CommonModel {
"$id": "schema1",
"additionalItems": CommonModel {
"$id": undefined,
"additionalItems": undefined,
"additionalProperties": undefined,
"const": undefined,
"discriminator": undefined,
"enum": undefined,
"extend": undefined,
"format": undefined,
"items": undefined,
"originalInput": true,
"patternProperties": undefined,
"properties": undefined,
"required": undefined,
"type": Array [
"object",
"string",
Expand All @@ -26,15 +14,7 @@ CommonModel {
"null",
"integer",
],
"union": undefined,
},
"additionalProperties": undefined,
"const": undefined,
"discriminator": undefined,
"enum": undefined,
"extend": undefined,
"format": undefined,
"items": undefined,
"originalInput": Object {
"$id": "schema1",
"properties": Object {
Expand All @@ -44,25 +24,11 @@ CommonModel {
},
},
},
"patternProperties": undefined,
"properties": Object {
"testProp": CommonModel {
"$id": "schema2",
"additionalItems": undefined,
"additionalProperties": CommonModel {
"$id": undefined,
"additionalItems": undefined,
"additionalProperties": undefined,
"const": undefined,
"discriminator": undefined,
"enum": undefined,
"extend": undefined,
"format": undefined,
"items": undefined,
"originalInput": true,
"patternProperties": undefined,
"properties": undefined,
"required": undefined,
"type": Array [
"object",
"string",
Expand All @@ -72,27 +38,14 @@ CommonModel {
"null",
"integer",
],
"union": undefined,
},
"const": undefined,
"discriminator": undefined,
"enum": undefined,
"extend": undefined,
"format": undefined,
"items": undefined,
"originalInput": Object {
"$id": "schema2",
"type": "object",
},
"patternProperties": undefined,
"properties": undefined,
"required": undefined,
"type": "object",
"union": undefined,
},
},
"required": undefined,
"type": "object",
"union": undefined,
}
`;
17 changes: 9 additions & 8 deletions test/processors/AsyncAPIInputProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';
import { Parser } from '@asyncapi/parser';
import { AsyncAPIInputProcessor } from '../../src/processors/AsyncAPIInputProcessor';
import { InputMetaModel } from '../../src/models';
import { removeEmptyPropertiesFromObjects } from '../TestUtils/GeneralUtils';

const basicDocString = fs.readFileSync(
path.resolve(__dirname, './AsyncAPIInputProcessor/basic.json'),
Expand Down Expand Up @@ -148,54 +149,54 @@ describe('AsyncAPIInputProcessor', () => {
const basicDoc = JSON.parse(basicDocString);
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(basicDoc);
expect(commonInputModel).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel)).toMatchSnapshot();
});
test('should be able to process pure object for v3', async () => {
const basicDoc = JSON.parse(basicV3DocString);
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(basicDoc);
expect(commonInputModel).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel)).toMatchSnapshot();
});

test('should be able to process parsed objects', async () => {
const { document } = await parser.parse(basicDocString);
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(document);
expect(commonInputModel).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel)).toMatchSnapshot();
});

test('should be able to process YAML file', async () => {
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(yamlDocString);
expect(commonInputModel instanceof InputMetaModel).toBeTruthy();
expect(commonInputModel.models).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel.models)).toMatchSnapshot();
});
test('should be able to process file', async () => {
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(ymlFileURI);
expect(commonInputModel instanceof InputMetaModel).toBeTruthy();
expect(commonInputModel.models).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel.models)).toMatchSnapshot();
});
test('should be able to process operation with reply', async () => {
const { document } = await parser.parse(operationWithReply);
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(document);
expect(commonInputModel instanceof InputMetaModel).toBeTruthy();
expect(commonInputModel.models).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel.models)).toMatchSnapshot();
});

test('should be able to process operation with oneOf #1', async () => {
const { document } = await parser.parse(operationOneOf1DocString);
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(document);
expect(commonInputModel).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel)).toMatchSnapshot();
});

test('should be able to process operation with oneOf #2', async () => {
const { document } = await parser.parse(operationOneOf2DocString);
const processor = new AsyncAPIInputProcessor();
const commonInputModel = await processor.process(document);
expect(commonInputModel).toMatchSnapshot();
expect(removeEmptyPropertiesFromObjects(commonInputModel)).toMatchSnapshot();
});
});

Expand Down
3 changes: 2 additions & 1 deletion test/processors/JsonSchemaInputProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { JsonSchemaInputProcessor } from '../../src/processors/JsonSchemaInputProcessor';
import { AnyModel, CommonModel } from '../../src/models';
import { removeEmptyPropertiesFromObjects } from '../TestUtils/GeneralUtils';
jest.mock('../../src/utils/LoggingInterface');
jest.spyOn(JsonSchemaInputProcessor, 'convertSchemaToCommonModel');
let mockedReturnModels = [new CommonModel()];
Expand Down Expand Up @@ -302,7 +303,7 @@ describe('JsonSchemaInputProcessor', () => {
const commonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(
{}
);
expect(Object.entries(JSON.parse(JSON.stringify(commonModels)))).toHaveLength(0);
expect(Object.entries(removeEmptyPropertiesFromObjects(commonModels))).toHaveLength(0);
});
});

Expand Down
Loading

0 comments on commit c9407a1

Please sign in to comment.