Skip to content

Commit

Permalink
test: add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ldhyen99 committed Sep 19, 2024
1 parent ba64371 commit 0223763
Show file tree
Hide file tree
Showing 12 changed files with 784 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ jest.mock(

jest.mock('../../../src/utils/common', () => ({
getPackageVersion: () => '0.0.1',
truncateString: jest.fn((str: string, maxLength: number) => {
if (str.length <= maxLength) {
return str;
}
return `${str.slice(0, maxLength)}...`;
}),
}));

const errorTestSuite = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs/promises';

import { readJsonFile } from '../../../src/interfaces/utils/common';
import { readJsonFile, truncateString } from '../../../src/interfaces/utils/common';

describe('readJsonFile', () => {
const credentialFilePath = 'config/credential.json';
Expand Down Expand Up @@ -31,3 +31,35 @@ describe('readJsonFile', () => {
expect(fileData).toBeNull();
});
});

describe('truncateString', () => {
it('should return the original string if it is shorter than the maxLength', () => {
const result = truncateString('Short text', 20);
expect(result).toBe('Short text');
});

it('should return the original string if its length is equal to the maxLength', () => {
const result = truncateString('Exact length', 12);
expect(result).toBe('Exact length');
});

it('should return the truncated string with "..." if it exceeds the maxLength', () => {
const result = truncateString('This is a very long string', 10);
expect(result).toBe('This is a ...');
});

it('should return an empty string if an empty string is provided', () => {
const result = truncateString('', 10);
expect(result).toBe('');
});

it('should handle a very short maxLength and still return a truncated string with "..."', () => {
const result = truncateString('Hello World', 3);
expect(result).toBe('Hel...');
});

it('should return the original string if maxLength is greater than string length', () => {
const result = truncateString('Hello', 10);
expect(result).toBe('Hello');
});
});
170 changes: 169 additions & 1 deletion packages/untp-test-suite/integration/cli/untpTest.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import { exec } from 'child_process';
import { IConfigContent } from '../../build/core/types';

describe("CLI 'untp test' Commands", () => {
describe("CLI 'untp test' Commands with local schema", () => {
afterEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -172,3 +172,171 @@ describe("CLI 'untp test' Commands", () => {
});
});
});

describe("CLI 'untp test' Commands with remote schema", () => {
describe('validation of `schema` in the configuration file', () => {
let credentialFileName: string;
let storePath: string;
let stdout: any;
let credentials: IConfigContent[];

const mockPath = `${process.cwd()}/integration/mock/untpTestPass`;

beforeAll((done) => {
credentialFileName = 'credentialsRemoteSchema.json';
storePath = `${mockPath}/${credentialFileName}`;
const fileContent = fs.readFileSync(storePath, 'utf8');

try {
credentials = JSON.parse(fileContent).credentials;
} catch (error) {
expect(error).toBeNull();
}

exec(`yarn untp test`, (error, result) => {
if (error) {
console.error(`execSync error: ${error}`);
return;
}

stdout = result;
done();
});
});

it('should ensure that the schema file of each credential exists', () => {
expect(stdout).not.toBeNull();
for (const credential of credentials) {
expect(fs.existsSync(`${process.cwd()}/src/schemas/${credential.type}/${credential.version}/schema.json`)).toBe(
true,
);
}
});

it('should return the content of `schema` when file is valid', () => {
expect(stdout).not.toBeNull();
for (const credential of credentials) {
const data = fs.readFileSync(
`${process.cwd()}/src/schemas/${credential.type}/${credential.version}/schema.json`,
'utf8',
);

expect(data).not.toBe('');
}
});
});

describe('process test runner and final report return PASS', () => {
let credentialFileName: string;
let storePath: string;
let stdout: any;

const mockPath = `${process.cwd()}/integration/mock/untpTestPass`;
beforeAll((done) => {
credentialFileName = 'credentialsRemoteSchema.json';
storePath = `${mockPath}/${credentialFileName}`;

exec(`yarn untp test -c ${storePath}`, (error, result) => {
if (error) {
console.error(`execSync error: ${error}`);
return;
}

stdout = result;
done();
});
});

it('should show PASS in report when all data validate', () => {
expect(stdout).toMatch(/PASS/);
expect(stdout).toContain('Your credentials are UNTP compliant');
});
});

describe('process test runner and final report return FAIL', () => {
let credentialFileName: string;
let storePath: string;
let stdout: any;

const mockPath = `${process.cwd()}/integration/mock/untpTestFail`;
beforeAll((done) => {
credentialFileName = 'credentialsRemoteSchema.json';
storePath = `${mockPath}/${credentialFileName}`;

exec(`yarn untp test -c ${storePath}`, (error, result) => {
if (error) {
console.error(`execSync error: ${error}`);
return;
}

stdout = result;
done();
});
});

it('should show FAIL in the report when data invalidate', () => {
expect(stdout).toMatch(/FAIL/);
expect(stdout).toMatch(/Your credentials are not UNTP compliant/);
expect(stdout).toContain('url field Failed to fetch data ');
expect(stdout).toContain(`field must have required property 'id'.`);
expect(stdout).toContain(`url field The URL 'in-valid-url' is not a valid URL..`);
expect(stdout).toContain(`url field The URL 'abc://example.com' must use http or https protocol..`);
});
});

describe('process test runner and final report return WARN', () => {
let credentialFileName: string;
let storePath: string;
let stdout: any;

const mockPath = `${process.cwd()}/integration/mock/untpTestWarn`;
beforeAll((done) => {
credentialFileName = 'credentialsRemoteSchema.json';
storePath = `${mockPath}/${credentialFileName}`;

exec(`yarn untp test -c ${storePath}`, (error, result) => {
if (error) {
console.error(`execSync error: ${error}`);
return;
}

stdout = result;
done();
});
});

it('should show WARN in the report when data invalidate', () => {
expect(stdout).toMatch(/WARN/);
expect(stdout).toMatch(/Your credentials are UNTP compliant, but have extended the data model/);
});
});

describe('process test runner with result combine FAIL and WARN and final report return FAIL', () => {
let credentialFileName: string;
let storePath: string;
let stdout: any;

const mockPath = `${process.cwd()}/integration/mock/untpTestFailuresAndWarnings`;
beforeAll((done) => {
credentialFileName = 'credentialsRemoteSchema.json';
storePath = `${mockPath}/${credentialFileName}`;

exec(`yarn untp test -c ${storePath}`, (error, result) => {
if (error) {
console.error(`execSync error: ${error}`);
return;
}

stdout = result;
done();
});
});

it('should show WARN in the report when data invalidate', () => {
expect(stdout).toMatch(/FAIL/);
expect(stdout).toMatch(/Your credentials are not UNTP compliant/);
expect(stdout).toContain('Additional property found');
expect(stdout).toContain('url field Failed to fetch data from the URL');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"credentials": [
{
"type": "objectEvent",
"version": "v0.0.1",
"dataPath": "integration/mock/untpTestPass/data/objectEvent.json",
"url": "in-valid-url"
},
{
"type": "",
"version": "",
"dataPath": "integration/mock/untpTestFail/data/objectEvent.json",
"url": "abc://example.com"
},
{
"type": "",
"version": "",
"dataPath": "integration/mock/untpTestFail/data/objectEvent.json",
"url": "https://jargon.sh/user/unece/traceabilityEvents/v/0.3.10/artefacts/jsonSchemas/DigitalTraceabilityEvent.js"
},
{
"type": "",
"version": "",
"dataPath": "integration/mock/untpTestFail/data/objectEvent.json",
"url": "https://jargon.sh/user/unece/traceabilityEvents/v/0.3.10/artefacts/jsonSchemas/DigitalTraceabilityEvent.json?class=DigitalTraceabilityEvent"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"credentials": [
{
"type": "",
"version": "",
"dataPath": "integration/mock/untpTestWarn/data/objectEvent.json",
"url": "https://jargon.sh/user/unece/traceabilityEvents/v/working/artefacts/jsonSchemas/AssociationEvent.json?class=AssociationEvent"
},
{
"type": "",
"version": "",
"dataPath": "integration/mock/untpTestWarn/data/associationEvent.json",
"url": "https://jargon.sh/user/unece/traceabilityEvents/v/working/artefacts/jsonSchemas/AssociationEvent.json?class=AssociationEvents"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"credentials": [
{
"type": "objectEvent",
"version": "v0.0.1",
"dataPath": "integration/mock/untpTestPass/data/digitalTraceabilityEvent.json",
"url": "https://jargon.sh/user/unece/traceabilityEvents/v/0.3.10/artefacts/jsonSchemas/DigitalTraceabilityEvent.json?class=DigitalTraceabilityEvent"
},
{
"type": "transformationEvent",
"version": "v0.0.1",
"dataPath": "integration/mock/untpTestPass/data/transformationEvent.json",
"url": ""
}
]
}
Loading

0 comments on commit 0223763

Please sign in to comment.