Skip to content

Commit

Permalink
chore: increase data model test
Browse files Browse the repository at this point in the history
  • Loading branch information
R11manish committed Sep 13, 2024
1 parent baa9d48 commit de67427
Showing 1 changed file with 106 additions and 2 deletions.
108 changes: 106 additions & 2 deletions test/data-model.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ValidationService } from '../src/services/validator-service';
import { GTWError } from '../src/helpers/custom-error';
import { DataModel } from '../src/modules/data-model/data-model';
import { Config } from '../src/common/types';
import { mockClient, mockGet } from './stubs/common.stub';
import { Config, DataModelRequest } from '../src/common/types';
import { mockClient, mockGet, mockPost, mockPut } from './stubs/common.stub';
import { routes } from '../src/common/routes';

const mockValidationService = {} as ValidationService;
Expand Down Expand Up @@ -139,4 +139,108 @@ describe('DataModel', () => {
);
});
});

describe('createDataModel', () => {
it('should create a data model successfully', async () => {
const input: DataModelRequest = {
title: 'Test Model',
description: 'A test data model',
schema: {},
tags: ['test'],
};

const expectedOutput = {
id: 1,
...input,
created_at: '2023-09-13T12:00:00Z',
updated_at: '2023-09-13T12:00:00Z',
created_by: 'user123',
};

mockPost.mockResolvedValue({ data: expectedOutput, error: null });

const result = await dataModel.createDataModel(input);

expect(result).toEqual(expectedOutput);
expect(mockClient.POST).toHaveBeenCalledWith(routes.CreateDataModel, {
body: input,
});
});

it('should throw GTWError when API call fails', async () => {
const input: DataModelRequest = {
title: 'Test Model',
description: 'A test data model',
schema: {},
tags: ['test'],
};

const mockError = { error: 'API Error' };
mockPost.mockResolvedValue({
data: null,
error: mockError,
response: {},
});

await expect(dataModel.createDataModel(input)).rejects.toThrow(GTWError);
expect(mockClient.POST).toHaveBeenCalledWith(routes.CreateDataModel, {
body: input,
});
});
});

describe('updateDataModel', () => {
it('should update a data model successfully', async () => {
const dataModelId = 1;
const input: DataModelRequest = {
title: 'Updated Test Model',
description: 'An updated test data model',
schema: {},
tags: ['test', 'updated'],
};

const expectedOutput = {
id: dataModelId,
...input,
created_at: '2023-09-13T12:00:00Z',
updated_at: '2023-09-13T13:00:00Z',
created_by: 'user123',
};

mockPut.mockResolvedValue({ data: expectedOutput, error: null });

const result = await dataModel.updateDataModel(dataModelId, input);

expect(result).toEqual(expectedOutput);
expect(mockClient.PUT).toHaveBeenCalledWith(routes.UpdateDataModel, {
body: input,
params: { path: { id: dataModelId } },
});
});

it('should throw GTWError when API call fails', async () => {
const dataModelId = 1;
const input: DataModelRequest = {
title: 'Updated Test Model',
description: 'An updated test data model',
schema: {},
tags: ['test', 'updated'],
};

const mockError = { error: 'API Error' };
mockPut.mockResolvedValue({
data: null,
error: mockError,
response: {},
});

await expect(
dataModel.updateDataModel(dataModelId, input),
).rejects.toThrow(GTWError);
expect(mockClient.PUT).toHaveBeenCalledWith(routes.UpdateDataModel, {
body: input,
params: { path: { id: dataModelId } },
});
});
});
});

0 comments on commit de67427

Please sign in to comment.