Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #65

Merged
merged 10 commits into from
Sep 13, 2024
11 changes: 9 additions & 2 deletions src/modules/account/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MediaType } from 'openapi-typescript-helpers';
import { OpenAPIClient } from '../../common/types';
import { OpenAPIClient, MyAccountResponse } from '../../common/types';
import { paths } from '../../api';
import { GTWError } from '../../helpers/custom-error';

Expand All @@ -10,7 +10,14 @@ export class Account {
this.client = client;
}

async getAccountInfo() {
/**
* This async function retrieves account information by making a GET request to '/accounts/me' and
* handles errors by throwing a custom GTWError if any occur.
* @returns The `getAccountInfo` function is returning the `data` object fetched from the
* `/accounts/me` endpoint. If there is an error during the API call, a `GTWError` is thrown with the
* error and response details.
*/
async getAccountInfo(): Promise<MyAccountResponse> {
const { data, response, error } = await this.client.GET('/accounts/me');

if (error) {
Expand Down
27 changes: 21 additions & 6 deletions src/modules/data-model/data-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { MediaType } from 'openapi-typescript-helpers';
import { Config, DataModelRequest, OpenAPIClient } from '../../common/types';
import {
Config,
DataModelRequest,
HelperPaginatedResponse,
OpenAPIClient,
DataModel as DMTypes,
} from '../../common/types';
import { ValidationService } from '../../services/validator-service';
import { paths } from '../../api';
import { GTWError } from '../../helpers/custom-error';
Expand Down Expand Up @@ -34,7 +40,10 @@ export class DataModel {
* error during the API request, a `GTWError` is thrown with the error and response details. If there
* is no error, the function returns the fetched data.
*/
async getDataModels(page: number = 1, page_size: number = 10) {
async getDataModels(
page: number = 1,
page_size: number = 10,
): Promise<HelperPaginatedResponse<DMTypes>> {
const { data, error, response } = await this.client.GET('/data-models', {
params: { query: { page, page_size } },
});
Expand All @@ -54,7 +63,7 @@ export class DataModel {
* @returns The `createDataModel` function is returning the `data` object after making a POST request
* to create a data model.
*/
async createDataModel(dataModelInput: DataModelRequest) {
async createDataModel(dataModelInput: DataModelRequest): Promise<DMTypes> {
const { data, error, response } = await this.client.POST('/data-models', {
body: dataModelInput,
});
Expand All @@ -76,7 +85,10 @@ export class DataModel {
* @returns The `updateDataModel` function is returning the updated data model after making a PUT
* request to the server with the provided `dataModelInput` for the specified `dataModelId`.
*/
async updateDataModel(dataModelId: number, dataModelInput: DataModelRequest) {
async updateDataModel(
dataModelId: number,
dataModelInput: DataModelRequest,
): Promise<DMTypes> {
const { data, error, response } = await this.client.PUT(
'/data-models/{id}',
{
Expand All @@ -103,7 +115,7 @@ export class DataModel {
* `GTWError` with the error and response details. If there is no error, it will return the retrieved
* data.
*/
async getDataModelById(dataModelId: number) {
async getDataModelById(dataModelId: number): Promise<DMTypes> {
const { data, error, response } = await this.client.GET(
'/data-models/{id}',
{
Expand Down Expand Up @@ -131,7 +143,10 @@ export class DataModel {
* during the API request, a `GTWError` is thrown with the error and response details. If the request
* is successful, the function returns the retrieved data.
*/
async getMyDataModels(page: number = 1, page_size: number = 10) {
async getMyDataModels(
page: number = 1,
page_size: number = 10,
): Promise<HelperPaginatedResponse<DMTypes>> {
const { data, response, error } = await this.client.GET('/data-models/me', {
params: { query: { page, page_size } },
});
Expand Down
47 changes: 47 additions & 0 deletions test/account.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { OpenAPIClient, MyAccountResponse } from '../src/common/types';
import { paths } from '../src/api';

import { GTWError } from '../src/helpers/custom-error';
import { Account } from '../src/modules/account/account';
import { MediaType } from 'openapi-typescript-helpers';
import { mockClient, mockGet } from './stubs/common.stub';
import { routes } from '../src/common/routes';

describe('Account', () => {
let account: Account;

beforeEach(() => {
account = new Account(
mockClient as unknown as OpenAPIClient<paths, MediaType>,
);
});

describe('getAccountInfo', () => {
it('should return account info when API call is successful', async () => {
const mockData: MyAccountResponse = { did: '123', username: 'Test User' };
mockGet.mockResolvedValue({
data: mockData,
response: {},
error: null,
});

const result = await account.getAccountInfo();

expect(result).toEqual(mockData);
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetMyAccount);
});

it('should throw GTWError when API call fails', async () => {
const mockResponse = { status: 401 } as Response;

mockGet.mockResolvedValue({
data: null,
response: mockResponse,
error: { error: 'Unauthorized' },
});

await expect(account.getAccountInfo()).rejects.toThrow(GTWError);
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetMyAccount);
});
});
});
246 changes: 246 additions & 0 deletions test/data-model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
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, DataModelRequest } from '../src/common/types';
import { mockClient, mockGet, mockPost, mockPut } from './stubs/common.stub';
import { routes } from '../src/common/routes';

const mockValidationService = {} as ValidationService;
const mockConfig = {} as Config;

describe('DataModel', () => {
let dataModel: DataModel;

beforeEach(() => {
jest.clearAllMocks();
dataModel = new DataModel(mockClient, mockValidationService, mockConfig);
});

describe('getDataModels', () => {
it('should fetch data models successfully', async () => {
const mockResponse = {
data: [{ id: 1, name: 'Model1' }],
total: 1,
page: 1,
pageSize: 10,
};
mockGet.mockResolvedValue({
data: mockResponse,
error: null,
response: {} as Response,
});

const result = await dataModel.getDataModels();

expect(result).toEqual(mockResponse);
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetDataModels, {
params: { query: { page: 1, page_size: 10 } },
});
});

it('should throw GTWError on API error', async () => {
const mockError = { error: 'API Error' };
const mockResponse = { status: 400 } as Response;
mockGet.mockResolvedValue({
data: null,
error: mockError,
response: mockResponse,
});

await expect(dataModel.getDataModels()).rejects.toThrow(GTWError);
await expect(dataModel.getDataModels()).rejects.toHaveProperty(
'statusCode',
400,
);
await expect(dataModel.getDataModels()).rejects.toHaveProperty(
'message',
'API Error',
);
});
});

describe('getDataModelById', () => {
it('should fetch a data model by id successfully', async () => {
const mockResponse = { id: 1, name: 'Model1' };
mockGet.mockResolvedValue({
data: mockResponse,
error: null,
response: {} as Response,
});

const result = await dataModel.getDataModelById(1);

expect(result).toEqual(mockResponse);
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetDataModelByID, {
params: { path: { id: 1 } },
});
});

it('should throw GTWError on API error', async () => {
const mockError = { error: 'Not Found' };
const mockResponse = { status: 404 } as Response;
mockGet.mockResolvedValue({
data: null,
error: mockError,
response: mockResponse,
});

await expect(dataModel.getDataModelById(1)).rejects.toThrow(GTWError);
await expect(dataModel.getDataModelById(1)).rejects.toHaveProperty(
'statusCode',
404,
);
await expect(dataModel.getDataModelById(1)).rejects.toHaveProperty(
'message',
'Not Found',
);
});
});

describe('getMyDataModels', () => {
it('should fetch user-specific data models successfully', async () => {
const mockResponse = {
data: [{ id: 1, name: 'MyModel1' }],
total: 1,
page: 1,
pageSize: 10,
};
mockGet.mockResolvedValue({
data: mockResponse,
error: null,
response: {} as Response,
});

const result = await dataModel.getMyDataModels();

expect(result).toEqual(mockResponse);
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetDataModelsByUser, {
params: { query: { page: 1, page_size: 10 } },
});
});

it('should throw GTWError on API error', async () => {
const mockError = { error: 'Unauthorized' };
const mockResponse = { status: 401 } as Response;
mockGet.mockResolvedValue({
data: null,
error: mockError,
response: mockResponse,
});

await expect(dataModel.getMyDataModels()).rejects.toThrow(GTWError);
await expect(dataModel.getMyDataModels()).rejects.toHaveProperty(
'statusCode',
401,
);
await expect(dataModel.getMyDataModels()).rejects.toHaveProperty(
'message',
'Unauthorized',
);
});
});

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 } },
});
});
});
});
Loading