forked from fga-eps-mds/2022-2-Alectrion-EquipamentApi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from fga-eps-mds/aumentar-cobertura
Aumentar cobertura
- Loading branch information
Showing
9 changed files
with
463 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { mock } from 'jest-mock-extended' | ||
import { Equipment } from '../src/db/entities/equipment' | ||
import { FindOneEquipmentUseCase } from '../src/useCases/findOneEquipment/find-one-equipment-usecase' | ||
import { FindOneEquipmentController } from '../src/presentation/controller/find-one-equipment-controller' | ||
import { datatype } from 'faker' | ||
import { ok, serverError } from '../src/presentation/helpers' | ||
|
||
const useCaseMocked = mock<FindOneEquipmentUseCase>() | ||
const findOneEquipmentController = new FindOneEquipmentController(useCaseMocked) | ||
|
||
const mockedEquipmentBase = { | ||
id: datatype.string(), | ||
tippingNumber: datatype.string(), | ||
serialNumber: datatype.string(), | ||
type: 'CPU', | ||
status: 'ACTIVE', | ||
model: datatype.string(), | ||
description: datatype.string(), | ||
screenSize: null, | ||
power: null, | ||
screenType: null, | ||
processor: datatype.string(), | ||
storageType: datatype.string(), | ||
storageAmount: datatype.number().toString(), | ||
ram_size: datatype.number().toString(), | ||
createdAt: datatype.datetime(), | ||
updatedAt: datatype.datetime() | ||
} as unknown as Equipment | ||
|
||
describe('Should test FindOneEquipmentController', () => { | ||
it('should find equipment with success', async () => { | ||
useCaseMocked.execute.mockResolvedValue({ | ||
isSuccess: true, | ||
data: mockedEquipmentBase | ||
}) | ||
const query = { equipmentId: mockedEquipmentBase.id, tippingNumber: mockedEquipmentBase.tippingNumber } | ||
const response = await findOneEquipmentController.perform(query) | ||
expect(response).toEqual(ok(response.data)) | ||
expect(useCaseMocked.execute).toHaveBeenCalled() | ||
}) | ||
|
||
it('should not find equipment', async () => { | ||
useCaseMocked.execute.mockResolvedValue({ | ||
isSuccess: true, | ||
data: undefined | ||
}) | ||
const query = { equipmentId: datatype.string(), tippingNumber: datatype.string() } | ||
const response = await findOneEquipmentController.perform(query) | ||
expect(response).toEqual(serverError()) | ||
expect(useCaseMocked.execute).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { EquipmentNotFoundError, FindOneEquipmentUseCase } from '../src/useCases/findOneEquipment/find-one-equipment-usecase'; | ||
import { MockProxy, mock } from 'jest-mock-extended' | ||
import { Estado } from '../src/domain/entities/equipamentEnum/estado' | ||
import { Status } from '../src/domain/entities/equipamentEnum/status' | ||
import { Type } from '../src/domain/entities/equipamentEnum/type' | ||
import { ListOneEquipmentRepository } from '../src/repository/equipment/list-one-equipment' | ||
|
||
describe('FindOneEquipment', () => { | ||
let listOneEquipmentRepository: MockProxy<ListOneEquipmentRepository> | ||
let findOneEquipmentUseCase: FindOneEquipmentUseCase | ||
|
||
beforeEach(() => { | ||
listOneEquipmentRepository = mock() | ||
findOneEquipmentUseCase = new FindOneEquipmentUseCase( | ||
listOneEquipmentRepository | ||
) | ||
}) | ||
|
||
test('should return equipment when found', async () => { | ||
const expectedData = { | ||
id: 'id', | ||
acquisitionDate: new Date(), | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
situacao: Status.ACTIVE, | ||
estado: Estado.Novo, | ||
tippingNumber: '12345', | ||
model: 'DELL G15', | ||
serialNumber: 'any', | ||
type: Type.CPU | ||
}; | ||
listOneEquipmentRepository.findOne.mockResolvedValue(expectedData); | ||
|
||
const query = { tippingNumber: '12345', id: 'id' }; | ||
const result = await findOneEquipmentUseCase.execute(query); | ||
|
||
expect(listOneEquipmentRepository.findOne).toHaveBeenCalledWith(query); | ||
expect(result.isSuccess).toBe(true); | ||
expect(result.data).toEqual(expectedData); | ||
}); | ||
|
||
test('should return EquipmentNotFoundError when no equipment is found', async () => { | ||
listOneEquipmentRepository.findOne.mockResolvedValue(null); | ||
|
||
const query = { tippingNumber: '543' }; | ||
const result = await findOneEquipmentUseCase.execute(query); | ||
|
||
expect(listOneEquipmentRepository.findOne).toHaveBeenCalledWith(query); | ||
expect(result.isSuccess).toBe(false); | ||
expect(result.error).toBeInstanceOf(EquipmentNotFoundError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { NotOSFoundError, FindOrderService } from '../src/useCases/find-order-service/find-order-service'; | ||
import { MockProxy, mock } from 'jest-mock-extended' | ||
import { Equipment } from '../src/domain/entities/equipment' | ||
import { Estado } from '../src/domain/entities/equipamentEnum/estado' | ||
import { Status } from '../src/domain/entities/equipamentEnum/status' | ||
import { Status as OSStatus } from '../src/domain/entities/serviceOrderEnum/status' | ||
import { Type } from '../src/domain/entities/equipamentEnum/type' | ||
import { OrderServiceRepositoryProtocol } from '../src/repository/protocol/orderServiceRepositoryProtocol' | ||
|
||
describe('FindOrderService', () => { | ||
let orderServiceRepositoryProtocol: MockProxy<OrderServiceRepositoryProtocol> | ||
let findOrderServiceUseCase: FindOrderService | ||
|
||
const equipment: Equipment = { | ||
id: 'id', | ||
acquisitionDate: new Date(), | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
situacao: Status.ACTIVE, | ||
estado: Estado.Novo, | ||
tippingNumber: 'any', | ||
model: 'DELL G15', | ||
serialNumber: 'any', | ||
type: Type.CPU | ||
} | ||
|
||
beforeEach(() => { | ||
orderServiceRepositoryProtocol = mock() | ||
findOrderServiceUseCase = new FindOrderService( | ||
orderServiceRepositoryProtocol | ||
) | ||
}) | ||
|
||
test('should return order services when they are found', async () => { | ||
const expectedData = [{ | ||
id: 2, | ||
equipment, | ||
description: 'any_description', | ||
seiProcess: '123456789', | ||
senderPhone: '61992809831', | ||
senderDocument: '12345678910', | ||
technicianId: '123456', | ||
technicianName: 'Pessoa', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
status: OSStatus.MAINTENANCE, | ||
authorId: '123456789', | ||
senderName: 'Pessoa 2' | ||
}]; | ||
orderServiceRepositoryProtocol.findOrderServiceGeneric.mockResolvedValue(expectedData); | ||
|
||
const query = { type: 'type1', unit: 'unit1' }; | ||
const result = await findOrderServiceUseCase.execute(query); | ||
|
||
expect(orderServiceRepositoryProtocol.findOrderServiceGeneric).toHaveBeenCalledWith(query); | ||
expect(result.isSuccess).toBe(true); | ||
expect(result.data).toEqual(expectedData); | ||
}); | ||
|
||
test('should return NotOSFoundError when no order services are found', async () => { | ||
orderServiceRepositoryProtocol.findOrderServiceGeneric.mockResolvedValue(undefined); | ||
|
||
const query = { model: 'abc' }; | ||
const result = await findOrderServiceUseCase.execute(query); | ||
|
||
expect(orderServiceRepositoryProtocol.findOrderServiceGeneric).toHaveBeenCalledWith(query); | ||
expect(result.isSuccess).toBe(false); | ||
expect(result.error).toBeInstanceOf(NotOSFoundError); | ||
}); | ||
|
||
test('should set default values for "take" and "skip" if not provided in query', async () => { | ||
orderServiceRepositoryProtocol.findOrderServiceGeneric.mockResolvedValue([]); | ||
|
||
const query = { model: 'abc' }; | ||
await findOrderServiceUseCase.execute(query); | ||
|
||
expect(orderServiceRepositoryProtocol.findOrderServiceGeneric).toHaveBeenCalledWith({ | ||
type: undefined, | ||
unit: undefined, | ||
date: undefined, | ||
brand: undefined, | ||
search: undefined, | ||
model: 'abc', | ||
status: undefined, | ||
take: 0, | ||
skip: 0 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { mock } from 'jest-mock-extended' | ||
import { Equipment } from '../src/db/entities/equipment' | ||
import { FindAllAcquisitionUseCase } from '../src/useCases/findAcquisition/findAllAcquisitionUseCase' | ||
import { FindAllAcquisitionsController } from '../src/presentation/controller/findAllAcquisitionsController' | ||
import { datatype } from 'faker' | ||
import { ok } from '../src/presentation/helpers' | ||
import { EquipmentAcquisition } from '../src/db/entities/equipment-acquisition' | ||
import { ServerError } from '../src/presentation/errors' | ||
|
||
const useCaseMocked = mock<FindAllAcquisitionUseCase>() | ||
const findAllAcquisitionsController = new FindAllAcquisitionsController(useCaseMocked) | ||
|
||
const mockedEquipmentBase = { | ||
id: datatype.string(), | ||
tippingNumber: datatype.string(), | ||
serialNumber: datatype.string(), | ||
type: 'CPU', | ||
status: 'ACTIVE', | ||
model: datatype.string(), | ||
description: datatype.string(), | ||
screenSize: null, | ||
power: null, | ||
screenType: null, | ||
processor: datatype.string(), | ||
storageType: datatype.string(), | ||
storageAmount: datatype.number().toString(), | ||
ram_size: datatype.number().toString(), | ||
createdAt: datatype.datetime(), | ||
updatedAt: datatype.datetime() | ||
} as unknown as Equipment | ||
|
||
const mockedAcquisition = { | ||
id: datatype.string(), | ||
name: 'compra', | ||
equipment: mockedEquipmentBase | ||
} as unknown as EquipmentAcquisition | ||
|
||
describe('Should test FindAllAcquisitionsController', () => { | ||
it('should find equipment with success', async () => { | ||
useCaseMocked.execute.mockResolvedValue({ | ||
isSuccess: true, | ||
data: [mockedAcquisition] | ||
}) | ||
|
||
const response = await findAllAcquisitionsController.perform() | ||
expect(response).toEqual(ok(response.data)) | ||
expect(useCaseMocked.execute).toHaveBeenCalled() | ||
}) | ||
|
||
it('should not find acquisition', async () => { | ||
useCaseMocked.execute.mockResolvedValue({ | ||
isSuccess: true, | ||
data: [] | ||
}) | ||
|
||
const response = await findAllAcquisitionsController.perform() | ||
expect(response).toEqual(ok([])) | ||
expect(useCaseMocked.execute).toHaveBeenCalled() | ||
}) | ||
|
||
it('should return server error', async () => { | ||
useCaseMocked.execute.mockResolvedValue({ | ||
isSuccess: false, | ||
error: new ServerError() | ||
}) | ||
|
||
const response = await findAllAcquisitionsController.perform() | ||
|
||
expect(response).toHaveProperty('statusCode', 500) | ||
expect(response).toHaveProperty('data') | ||
expect(response.data).toBeInstanceOf(ServerError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { NotAcquisitionsFound, FindAllAcquisitionUseCase } from '../src/useCases/findAcquisition/findAllAcquisitionUseCase'; | ||
import { MockProxy, mock } from 'jest-mock-extended' | ||
import { AcquisitionRepository } from '../src/repository/acquisitionRepository' | ||
import { Equipment } from '../src/db/entities/equipment' | ||
import { datatype } from 'faker' | ||
|
||
describe('FindOneEquipment', () => { | ||
let acquisitionRepository: MockProxy<AcquisitionRepository> | ||
let findAllAcquisitionUseCase: FindAllAcquisitionUseCase | ||
|
||
beforeEach(() => { | ||
acquisitionRepository = mock() | ||
findAllAcquisitionUseCase = new FindAllAcquisitionUseCase( | ||
acquisitionRepository | ||
) | ||
}) | ||
|
||
const mockedEquipmentBase = { | ||
id: datatype.string(), | ||
tippingNumber: datatype.string(), | ||
serialNumber: datatype.string(), | ||
type: 'CPU', | ||
status: 'ACTIVE', | ||
model: datatype.string(), | ||
description: datatype.string(), | ||
screenSize: null, | ||
power: null, | ||
screenType: null, | ||
processor: datatype.string(), | ||
storageType: datatype.string(), | ||
storageAmount: datatype.number().toString(), | ||
ram_size: datatype.number().toString(), | ||
createdAt: datatype.datetime(), | ||
updatedAt: datatype.datetime() | ||
} as unknown as Equipment | ||
|
||
test('should return acquisitions when found', async () => { | ||
const expectedData = [{ | ||
id: 'abc', | ||
name: 'compra', | ||
equipment: [mockedEquipmentBase] | ||
}]; | ||
acquisitionRepository.findAll.mockResolvedValue(expectedData); | ||
|
||
const result = await findAllAcquisitionUseCase.execute(); | ||
|
||
expect(acquisitionRepository.findAll).toHaveBeenCalled(); | ||
expect(result.isSuccess).toBe(true); | ||
expect(result.data).toEqual(expectedData); | ||
}); | ||
|
||
test('should return NotAcquisitionsFound when no acquisition is found', async () => { | ||
acquisitionRepository.findAll.mockResolvedValue(null); | ||
|
||
const result = await findAllAcquisitionUseCase.execute(); | ||
|
||
expect(acquisitionRepository.findAll).toHaveBeenCalled(); | ||
expect(result.isSuccess).toBe(false); | ||
expect(result.error).toBeInstanceOf(NotAcquisitionsFound); | ||
}); | ||
}); |
Oops, something went wrong.