Added more tests. Unit tests for PapersController.
This commit is contained in:
parent
26cd205738
commit
22598ae2bc
@ -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;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
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";
|
||||||
import { PageDto } from "src/core/domain/dtos";
|
import { PageDto } from "src/core/domain/dtos";
|
||||||
@ -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);
|
pageInter.deletePIT('thisIsIDSpecified');
|
||||||
// expect(httpPostMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/papers/_pit?keep_alive=1m`);
|
expect(httpDeleteMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/_pit`, {
|
||||||
// });
|
data: { id: 'thisIsIDSpecified' },
|
||||||
|
headers: { 'Content-Type': 'application/json' }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// it('Should touch HttpService with correct URI when time alive and time-unit are set', () => {
|
it('Should return error exeception when HttpService fails', () => {
|
||||||
// let httpPostMock = jest.spyOn(httpService, 'post').mockReturnValueOnce(of({
|
jest.spyOn(httpService, 'delete').mockImplementationOnce(() => {
|
||||||
// data: {id: '2567'},
|
throw HttpResponseException;
|
||||||
// status: 0,
|
});
|
||||||
// statusText: '',
|
|
||||||
// headers: {},
|
|
||||||
// config: {},
|
|
||||||
// }));
|
|
||||||
|
|
||||||
// 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 },
|
||||||
|
status: 0,
|
||||||
|
statusText: '',
|
||||||
|
headers: {},
|
||||||
|
config: {},
|
||||||
|
}));
|
||||||
|
|
||||||
// it('Should return error exeception when HttpService fails', () => {
|
expect(pageInter.deletePIT('')).resolves.toBe(true);
|
||||||
// jest.spyOn(httpService, 'post').mockImplementationOnce(() => {
|
});
|
||||||
// throw HttpResponseException;
|
});
|
||||||
// });
|
|
||||||
|
|
||||||
// expect(pageInter.getPIT(1)).rejects.toEqual(HttpResponseException);
|
|
||||||
// });
|
|
||||||
|
|
||||||
// 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',
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
});
|
});
|
99
src/test/papers.controller.spec.ts
Normal file
99
src/test/papers.controller.spec.ts
Normal 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');
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user