Skip to content

Commit

Permalink
feat: tests for data-model class
Browse files Browse the repository at this point in the history
  • Loading branch information
R11manish committed May 10, 2024
1 parent 24eb2d7 commit 878b0ba
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 0 deletions.
55 changes: 55 additions & 0 deletions __mocks__/v3/data-model.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { DataModel } from '../../src/v3/data-model/data-model';
import {
dataModelStub,
dataModelMetaDataStub,
dataModelCreateStub,
} from '../../test/stubs/v3/data-model.stub';

export const DataModelMockService = (dataModel: DataModel) => ({
createDataModelMock: jest
.spyOn(dataModel.sdk, 'createDataModel_mutation')
.mockResolvedValue({
createDataModel: dataModelStub(),
}),

getDataModelMock: jest
.spyOn(dataModel.sdk, 'dataModel_query')
.mockResolvedValue({
dataModel: dataModelStub(),
}),

getDataModelsMock: jest
.spyOn(dataModel.sdk, 'dataModels_query')
.mockResolvedValue({
dataModels: [dataModelStub()],
}),

getDataModelsCountMock: jest
.spyOn(dataModel.sdk, 'dataModelsCount_query')
.mockResolvedValue({
dataModelsCount: 10,
}),

getDataModelsMetaDataMock: jest
.spyOn(dataModel.sdk, 'dataModelsMetadata_query')
.mockResolvedValue({
dataModelsMetadata: dataModelMetaDataStub(),
}),

getIssuersByDataModelMock: jest
.spyOn(dataModel.sdk, 'issuersByDataModel_query')
.mockResolvedValue({
issuersByDataModel: [{ count: 10 }],
}),

getIssuersDataModelCountMock: jest
.spyOn(dataModel.sdk, 'issuersByDataModelCount_query')
.mockResolvedValue({
issuersByDataModelCount: 10,
}),
getTotalofIssuersByDataModelMock: jest
.spyOn(dataModel.sdk, 'getTotalofIssuersByDataModel_query')
.mockResolvedValue({
getTotalofIssuersByDataModel: 10,
}),
});
46 changes: 46 additions & 0 deletions src/v3/data-model/data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,50 @@ export class DataModel {
throw new Error(errorHandler(error));

Check warning on line 99 in src/v3/data-model/data-model.ts

View check run for this annotation

Codecov / codecov/patch

src/v3/data-model/data-model.ts#L99

Added line #L99 was not covered by tests
}
}

/**
* The function `getIssuersByDataModel` retrieves issuers based on a given data model ID using an SDK.
* @param {string} id - A string representing the ID of the data model.
* @returns the result of the `issuersByDataModel_query` method call.
*/
async getIssuersByDataModel(id: string) {
try {
isUUIDValid(id);
return await this.sdk.issuersByDataModel_query({ id: id });
} catch (error: any) {
throw new Error(errorHandler(error));
}
}

/**
* The function `getIssuersByDataModelCount` retrieves the count of issuers based on a given data model
* ID.
* @param {string} dataModelId - The dataModelId parameter is a string that represents the ID of a data
* model.
* @returns the result of the `issuersByDataModelCount_query` method call.
*/
async getIssuersByDataModelCount(dataModelId: string) {
try {
isUUIDValid(dataModelId);
return await this.sdk.issuersByDataModelCount_query({ id: dataModelId });
} catch (error: any) {
throw new Error(errorHandler(error));
}
}

/**
* The function `getTotalofIssuersByDataModel` retrieves the total number of issuers based on a given
* data model ID.
* @param {string} dataModelId - The dataModelId parameter is a string that represents the identifier
* of a data model. It is used to query the total number of issuers associated with that data model.
* @returns the result of the `getTotalofIssuersByDataModel_query` method call.
*/
async getTotalofIssuersByDataModel(dataModelId: string) {
try {
isUUIDValid(dataModelId);
return await this.sdk.getTotalofIssuersByDataModel_query({ dataModelId });
} catch (error: any) {
throw new Error(errorHandler(error));
}
}
}
131 changes: 131 additions & 0 deletions test/stubs/v3/data-model.stub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {
DataModel,
CreateDataModelInput,
DataModelsMetadata,
} from '../../../gatewaySdk/sources/GatewayV3';

export const dataModelStub = (
overrideDataModel?: Partial<DataModel>,
): DataModel => ({
PDAs: [],
allowedOrganizations: [],
allowedUsers: [],
createdAt: '2023-11-29T18:00:55.650Z',
description:
'Net promoter score (NPS) is a market research metric that is based on a single survey question asking respondents to rate the likelihood that they would recommend a company, product, or a service to a friend or colleague. The NPS is a proprietary instrument developed by Fred Reichheld, who owns the registered NPS trademark in conjunction with Bain & Company and Satmetrix. Its popularity and broad use have been attributed to its simplicity and transparent methodology.',
featured: false,
group: {
id: 'd4339c71-f414-4dfd-9213-003317e06862',
createdAt: '2023-11-29T18:00:55.650Z',
dataModels: [],
official: false,
},
verified: false,
uniqueIssuersCount: 1,
title: 'Net promoter score ',
tags: ['Research', 'Testing'],
schema: {
type: 'object',
default: {},
title: 'Root Schema',
required: ['score'],
properties: {
score: {
type: 'integer',
default: 10,
title: 'Score',
examples: ['Fill from 0 up to 10'],
},
testimony: {
type: 'string',
title: 'Testimony',
examples: ['Tell us what your experience was like'],
},
recommendation: {
type: 'boolean',
title: 'Would you recommend this?',
},
keywords: {
type: 'array',
title: 'Keywords about your experience',
},
},
},

id: 'c22d65f0-b7b1-48a9-8c13-a01e32df1f1f',
organization: null,
pdas: [],
pdasIssuedCount: 2,
...overrideDataModel,
});

export const dataModelCreateStub = (
overrideDataModelCreateStub?: CreateDataModelInput,
): CreateDataModelInput => ({
data: {
description:
'Net promoter score (NPS) is a market research metric that is based on a single survey question asking respondents to rate the likelihood that they would recommend a company, product, or a service to a friend or colleague. The NPS is a proprietary instrument developed by Fred Reichheld, who owns the registered NPS trademark in conjunction with Bain & Company and Satmetrix. Its popularity and broad use have been attributed to its simplicity and transparent methodology.',

title: 'Net promoter score ',
schema: {
type: 'object',
default: {},
title: 'Root Schema',
required: ['score'],
properties: {
score: {
type: 'integer',
default: 10,
title: 'Score',
examples: ['Fill from 0 up to 10'],
},
testimony: {
type: 'string',
title: 'Testimony',
examples: ['Tell us what your experience was like'],
},
recommendation: {
type: 'boolean',
title: 'Would you recommend this?',
},
keywords: {
type: 'array',
title: 'Keywords about your experience',
},
},
},
},

signature: '0xd2FEB1aCf86d28cdc21dd733066d158737DaCa8d',
signingKey: 'test',
signingCipher: 'ED25519',
...overrideDataModelCreateStub,
});

export const dataModelMetaDataStub = (
overideDataModelMetaData?: DataModelsMetadata,
): DataModelsMetadata => ({
tags: [
'Research',
'Testing',
'Developers',
'Education',
'Music',
'Listening',
'Consumer',
'Finance',
'Banking',
'Credit Card',
'Social',
'Score',
'Earn',
'Marketing',
'Infrastructure',
'Tests',
],
issuedCount: {
min: 0,
max: 14,
},
...overideDataModelMetaData,
});
147 changes: 147 additions & 0 deletions test/v3/data-model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { getSdk } from '../../gatewaySdk/sources/GatewayV3';
import { DataModel } from '../../src/v3/data-model/data-model';
import {
dataModelCreateStub,
dataModelStub,
dataModelMetaDataStub,
} from '../stubs/v3/data-model.stub';
import { DataModelMockService } from '../../__mocks__/v3/data-model.mock';
import { GraphQLClient } from 'graphql-request';

let dataModel: DataModel;

beforeAll(() => {
dataModel = new DataModel(getSdk(new GraphQLClient('')));
});

afterAll(() => {
jest.resetAllMocks();
});

describe('DATA MODEL CLASS METHODS TESTING', () => {
it('create data model ', async () => {
const { createDataModelMock } = DataModelMockService(dataModel);

const { createDataModel } = await dataModel.createDataModel(
dataModelCreateStub(),
);

expect(createDataModel.title).toEqual(dataModelCreateStub().data.title);
expect(createDataModelMock).toHaveBeenCalled();
});

it('create data model -> throw error ', async () => {
const { createDataModelMock } = DataModelMockService(dataModel);

expect(
async () =>
await dataModel.createDataModel({
data: {
title: '',
description: '',
schema: undefined,
},
signature: '',
signingKey: '',
signingCipher: 'ED25519',
}),
).rejects.toThrow(' should be atleast 2 length');
expect(createDataModelMock).toHaveBeenCalled();
});

it('get data model', async () => {
const { getDataModelMock } = DataModelMockService(dataModel);

const { dataModel: rdataModel } = await dataModel.getDataModel(
dataModelStub().id,
);
expect(rdataModel).toEqual(dataModelStub());
expect(getDataModelMock).toHaveBeenCalled();
});

it('get data model -> throw error', async () => {
const { getDataModelMock } = DataModelMockService(dataModel);

expect(async () => await dataModel.getDataModel('wrong')).rejects.toThrow(
'wrong is not valid',
);
expect(getDataModelMock).toHaveBeenCalled();
});

it('get data models', async () => {
const { getDataModelsMock } = DataModelMockService(dataModel);

const { dataModels } = await dataModel.getDataModels();

expect(dataModels.length).toBeGreaterThanOrEqual(0);
expect(getDataModelsMock).toHaveBeenCalled();
});

it('get data models count', async () => {
const { getDataModelsCountMock } = DataModelMockService(dataModel);
const { dataModelsCount } = await dataModel.getDataModelsCount();
expect(dataModelsCount).toBeGreaterThanOrEqual(0);
expect(getDataModelsCountMock).toHaveBeenCalled();
});

it('get meta data of data models', async () => {
const { getDataModelsMetaDataMock } = DataModelMockService(dataModel);
const { dataModelsMetadata } = await dataModel.getDataModelsMetaData();
expect(dataModelsMetadata).toEqual(dataModelMetaDataStub());
expect(getDataModelsMetaDataMock).toHaveBeenCalled();
});

it('get issuers by data model', async () => {
const { getIssuersByDataModelMock } = DataModelMockService(dataModel);
const { issuersByDataModel } = await dataModel.getIssuersByDataModel(
dataModelStub().id,
);

expect(issuersByDataModel.length).toBeGreaterThanOrEqual(0);
expect(getIssuersByDataModelMock).toHaveBeenCalled();
});

it('get issuers by data model -> throw error', async () => {
const { getIssuersByDataModelMock } = DataModelMockService(dataModel);
expect(
async () => await dataModel.getIssuersByDataModel('wrong'),
).rejects.toThrow('wrong is not valid');
expect(getIssuersByDataModelMock).toHaveBeenCalled();
});

it('get isssuers by data model count', async () => {
const { getIssuersDataModelCountMock } = DataModelMockService(dataModel);
const { issuersByDataModelCount } =
await dataModel.getIssuersByDataModelCount(dataModelStub().id);
expect(issuersByDataModelCount).toBeGreaterThanOrEqual(0);
expect(getIssuersDataModelCountMock).toHaveBeenCalled();
});

it('get isssuers by data model count -> throw error', async () => {
const { getIssuersDataModelCountMock } = DataModelMockService(dataModel);
expect(
async () => await dataModel.getIssuersByDataModelCount('wrong'),
).rejects.toThrow('wrong is not valid');
expect(getIssuersDataModelCountMock).toHaveBeenCalled();
});

it('get total issuers by data model ', async () => {
const { getTotalofIssuersByDataModelMock } =
DataModelMockService(dataModel);
const { getTotalofIssuersByDataModel } =
await dataModel.getTotalofIssuersByDataModel(dataModelStub().id);

expect(getTotalofIssuersByDataModel).toBeGreaterThanOrEqual(0);
expect(getTotalofIssuersByDataModelMock).toHaveBeenCalled();
});

it('get total issuers by data model -> throw error ', async () => {
const { getTotalofIssuersByDataModelMock } =
DataModelMockService(dataModel);

expect(
async () => await dataModel.getTotalofIssuersByDataModel('wrong'),
).rejects.toThrow('wrong is not valid');
expect(getTotalofIssuersByDataModelMock).toHaveBeenCalled();
});
});

0 comments on commit 878b0ba

Please sign in to comment.