Added more tests. Unit tests for PapersController.

This commit is contained in:
danny-mhlv 2022-08-19 03:14:06 +03:00
parent 26cd205738
commit 22598ae2bc
4 changed files with 148 additions and 63 deletions

View File

@ -35,7 +35,7 @@ export class PapersController {
(response: SearchResultDto) => { (response: SearchResultDto) => {
return response.data; return response.data;
}, },
(error: HttpException) => { (error) => {
throw error; throw error;
} }
); );
@ -61,7 +61,7 @@ export class PapersController {
(response: SearchResultDto) => { (response: SearchResultDto) => {
return response.data; return response.data;
}, },
(error: HttpException) => { (error) => {
throw error; throw error;
} }
); );

View File

@ -1,6 +1,5 @@
import { HttpService } from "@nestjs/axios"; import { HttpService } from "@nestjs/axios";
import { ConfigModule } from "@nestjs/config"; import { ConfigModule } from "@nestjs/config";
import { ModuleRef } from "@nestjs/core";
import { Test } from "@nestjs/testing"; import { Test } from "@nestjs/testing";
import { Observable, of } from "rxjs"; import { Observable, of } from "rxjs";
import { EsTime, Order } from "src/core/domain"; import { EsTime, Order } from "src/core/domain";
@ -263,70 +262,57 @@ describe('Unit tests for PageInterceptor', () => {
}); });
}); });
// describe('deletePIT()', () => { describe('deletePIT()', () => {
// it('Should touch HttpService.delete() method', () => { it('Should touch HttpService.delete() method', () => {
// let httpPostMock = jest.spyOn(httpService, 'delete').mockReturnValueOnce(of({ let httpDeleteMock = jest.spyOn(httpService, 'delete').mockReturnValueOnce(
// data: {id: '2567'}, of({
// status: 0, data: {succeeded: true},
// statusText: '', status: 0,
// headers: {}, statusText: '',
// config: {}, headers: {},
// })); config: {},
}));
// pageInter.getPIT(1); pageInter.deletePIT('');
// expect(httpPostMock).toHaveBeenCalled(); expect(httpDeleteMock).toHaveBeenCalled();
// }); });
// it('Should contain correct port in the URI from .env', () => { it('Should contain correct port in the URI from .env and passed PIT ID in the request body', () => {
// let httpPostMock = jest.spyOn(httpService, 'post').mockReturnValueOnce(of({ let httpDeleteMock = jest.spyOn(httpService, 'delete').mockReturnValueOnce(
// data: {id: '2567'}, of({
// status: 0, data: { succeeded: true },
// statusText: '', status: 0,
// headers: {}, statusText: '',
// config: {}, headers: {},
// })); config: {},
}));
// pageInter.getPIT(1);
// expect(httpPostMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/papers/_pit?keep_alive=1m`); pageInter.deletePIT('thisIsIDSpecified');
// }); expect(httpDeleteMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/_pit`, {
data: { id: 'thisIsIDSpecified' },
// it('Should touch HttpService with correct URI when time alive and time-unit are set', () => { headers: { 'Content-Type': 'application/json' }
// let httpPostMock = jest.spyOn(httpService, 'post').mockReturnValueOnce(of({ });
// data: {id: '2567'}, });
// status: 0,
// statusText: '', it('Should return error exeception when HttpService fails', () => {
// headers: {}, jest.spyOn(httpService, 'delete').mockImplementationOnce(() => {
// config: {}, throw HttpResponseException;
// })); });
// let time = 2; expect(pageInter.deletePIT('')).rejects.toEqual(HttpResponseException);
// let unit = EsTime.sec; });
// pageInter.getPIT(time, unit); it('Should return true when Elasticsearch successfully removed PIT', () => {
// expect(httpPostMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/papers/_pit?keep_alive=${time+unit}`); jest.spyOn(httpService, 'delete').mockReturnValueOnce(
// }); of({
data: { succeeded: true },
// it('Should return error exeception when HttpService fails', () => { status: 0,
// jest.spyOn(httpService, 'post').mockImplementationOnce(() => { statusText: '',
// throw HttpResponseException; headers: {},
// }); config: {},
}));
// expect(pageInter.getPIT(1)).rejects.toEqual(HttpResponseException);
// }); expect(pageInter.deletePIT('')).resolves.toBe(true);
});
// it('Should return a non-empty string when HttpService request succeedes', () => { });
// jest.spyOn(httpService, 'post').mockReturnValueOnce(of({
// data: {id: '2567', keep_alive: '1m'},
// status: 0,
// statusText: '',
// headers: {},
// config: {},
// }));
// expect(pageInter.getPIT(1)).resolves.toEqual({
// id: '2567',
// keep_alive: '1m',
// });
// });
// });
}); });

View File

@ -0,0 +1,99 @@
import { HttpModule } from "@nestjs/axios";
import { NotFoundException } from "@nestjs/common";
import { Test } from "@nestjs/testing";
import { PapersController } from "src/application";
import { SearchService } from "src/core/services/common/search.service";
describe('Unit tests for PapersController', () => {
let searchService: SearchService;
let papersController: PapersController;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
PapersController,
{
provide: SearchService,
useValue: {
findByContext: jest.fn(),
findByID: jest.fn()
}
}
],
imports: [HttpModule]
}).compile();
papersController = moduleRef.get(PapersController);
searchService = moduleRef.get(SearchService);
});
it('Should be defined', () => {
expect(papersController).toBeDefined();
expect(searchService).toBeDefined();
});
describe('getByContext()', () => {
it('Should touch SearchService.findByContext() method', () => {
let findCtxMock = jest.spyOn(searchService, 'findByContext')
.mockResolvedValueOnce({
data: {
took: undefined,
timed_out: undefined,
hits: undefined,
_shards: undefined,
},
statusCode: 0
});
papersController.getByContext({ query: undefined });
expect(findCtxMock).toHaveBeenCalled();
});
it('Should resolve, when searched successfully', () => {
const searchResultMock = {
took: 1,
timed_out: false,
hits: {
total: {},
hits: [
{
_source: {
id: 'thisIsID',
title: 'andThisIsTheTitle',
authors: ['alsoAuthors'],
topic: 'andThatIsTheTopic',
summary: 'someSummaries',
tags: ['tag1', 'tag2'],
content: 'finallyContent!'
}
}
],
},
_shards: undefined,
};
jest.spyOn(searchService, 'findByContext')
.mockResolvedValueOnce({
data: searchResultMock,
statusCode: 200
});
expect(papersController.getByContext({ query: undefined })).resolves.toEqual(searchResultMock);
});
it('Should throw, when search was unsuccessful', () => {
searchService.findByContext = jest.fn()
.mockRejectedValueOnce(new NotFoundException);
expect(papersController.getByContext({ query: undefined }))
.rejects.toThrow(NotFoundException)
});
});
describe('getByID()', () => {
it.todo('Should touch SearchService.findByID() method');
it.todo('Should resolve, when searched successfully');
it.todo('Should throw, when search was unsuccessful');
});
});