fix prisma tests
This commit is contained in:
parent
c6d8a50fd6
commit
caddcd22c5
|
@ -1,4 +1,4 @@
|
||||||
import { ConsoleLogger, Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
||||||
import { DatabaseException } from '../../exceptions/DatabaseException';
|
import { DatabaseException } from '../../exceptions/DatabaseException';
|
||||||
import { ICollection } from '../../interfaces/collection.interface';
|
import { ICollection } from '../../interfaces/collection.interface';
|
||||||
|
@ -11,7 +11,6 @@ import { PrismaService } from './prisma-service';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export abstract class PrismaRepository<T> implements IRepository<T> {
|
export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
protected _model: string;
|
protected _model: string;
|
||||||
protected _logger: ConsoleLogger = new ConsoleLogger(PrismaRepository.name);
|
|
||||||
|
|
||||||
constructor(protected readonly _prisma: PrismaService) {}
|
constructor(protected readonly _prisma: PrismaService) {}
|
||||||
|
|
||||||
|
@ -38,7 +37,7 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOneByUuid(uuid: string, include?: any): Promise<T> {
|
async findOneByUuid(uuid: string): Promise<T> {
|
||||||
try {
|
try {
|
||||||
const entity = await this._prisma[this._model].findUnique({
|
const entity = await this._prisma[this._model].findUnique({
|
||||||
where: { uuid },
|
where: { uuid },
|
||||||
|
@ -98,7 +97,7 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(uuid: string, entity: Partial<T>, include?: any): Promise<T> {
|
async update(uuid: string, entity: Partial<T>): Promise<T> {
|
||||||
try {
|
try {
|
||||||
const updatedEntity = await this._prisma[this._model].update({
|
const updatedEntity = await this._prisma[this._model].update({
|
||||||
where: { uuid },
|
where: { uuid },
|
||||||
|
|
|
@ -5,7 +5,6 @@ import { PrismaRepository } from '../../src/adapters/secondaries/prisma-reposito
|
||||||
import { DatabaseException } from '../../src/exceptions/DatabaseException';
|
import { DatabaseException } from '../../src/exceptions/DatabaseException';
|
||||||
|
|
||||||
class FakeEntity {
|
class FakeEntity {
|
||||||
id?: number;
|
|
||||||
uuid?: string;
|
uuid?: string;
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +15,6 @@ const entityName = 'name-';
|
||||||
|
|
||||||
const createRandomEntity = (): FakeEntity => {
|
const createRandomEntity = (): FakeEntity => {
|
||||||
const entity: FakeEntity = {
|
const entity: FakeEntity = {
|
||||||
id: entityId,
|
|
||||||
uuid: `${entityUuid}${entityId}`,
|
uuid: `${entityUuid}${entityId}`,
|
||||||
name: `${entityName}${entityId}`,
|
name: `${entityName}${entityId}`,
|
||||||
};
|
};
|
||||||
|
@ -32,7 +30,6 @@ const fakeEntityToCreate: FakeEntity = {
|
||||||
|
|
||||||
const fakeEntityCreated: FakeEntity = {
|
const fakeEntityCreated: FakeEntity = {
|
||||||
...fakeEntityToCreate,
|
...fakeEntityToCreate,
|
||||||
id: 1,
|
|
||||||
uuid: 'some-uuid',
|
uuid: 'some-uuid',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,7 +48,17 @@ class FakePrismaService extends PrismaService {
|
||||||
}
|
}
|
||||||
|
|
||||||
const mockPrismaService = {
|
const mockPrismaService = {
|
||||||
|
$transaction: jest.fn().mockImplementation(async (data: any) => {
|
||||||
|
const entities = await data[0];
|
||||||
|
if (entities.length == 1) {
|
||||||
|
return Promise.resolve([[fakeEntityCreated], 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve([fakeEntities, fakeEntities.length]);
|
||||||
|
}),
|
||||||
fake: {
|
fake: {
|
||||||
|
create: jest.fn().mockResolvedValue(fakeEntityCreated),
|
||||||
|
|
||||||
findMany: jest.fn().mockImplementation((params?: any) => {
|
findMany: jest.fn().mockImplementation((params?: any) => {
|
||||||
if (params?.where?.limit == 1) {
|
if (params?.where?.limit == 1) {
|
||||||
return Promise.resolve([fakeEntityCreated]);
|
return Promise.resolve([fakeEntityCreated]);
|
||||||
|
@ -59,16 +66,11 @@ const mockPrismaService = {
|
||||||
|
|
||||||
return Promise.resolve(fakeEntities);
|
return Promise.resolve(fakeEntities);
|
||||||
}),
|
}),
|
||||||
|
count: jest.fn().mockResolvedValue(fakeEntities.length),
|
||||||
create: jest.fn().mockResolvedValue(fakeEntityCreated),
|
|
||||||
|
|
||||||
findUnique: jest.fn().mockImplementation(async (params?: any) => {
|
findUnique: jest.fn().mockImplementation(async (params?: any) => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
||||||
if (params?.where?.id) {
|
|
||||||
entity = fakeEntities.find((entity) => entity.id === params?.where?.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params?.where?.uuid) {
|
if (params?.where?.uuid) {
|
||||||
entity = fakeEntities.find(
|
entity = fakeEntities.find(
|
||||||
(entity) => entity.uuid === params?.where?.uuid,
|
(entity) => entity.uuid === params?.where?.uuid,
|
||||||
|
@ -145,20 +147,28 @@ describe('PrismaRepository', () => {
|
||||||
describe('findAll', () => {
|
describe('findAll', () => {
|
||||||
it('should return an array of entities', async () => {
|
it('should return an array of entities', async () => {
|
||||||
jest.spyOn(prisma.fake, 'findMany');
|
jest.spyOn(prisma.fake, 'findMany');
|
||||||
|
jest.spyOn(prisma.fake, 'count');
|
||||||
|
jest.spyOn(prisma, '$transaction');
|
||||||
|
|
||||||
const entities = await fakeRepository.findAll();
|
const entities = await fakeRepository.findAll();
|
||||||
expect(entities).toBe(fakeEntities);
|
expect(entities).toStrictEqual({
|
||||||
|
data: fakeEntities,
|
||||||
|
total: fakeEntities.length,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an array containing only one entity', async () => {
|
it('should return an array containing only one entity', async () => {
|
||||||
jest.spyOn(prisma.fake, 'findMany');
|
const entities = await fakeRepository.findAll(1, 10, { limit: 1 });
|
||||||
|
|
||||||
const entities = await fakeRepository.findAll({ limit: 1 });
|
|
||||||
|
|
||||||
expect(prisma.fake.findMany).toHaveBeenCalledWith({
|
expect(prisma.fake.findMany).toHaveBeenCalledWith({
|
||||||
|
skip: 0,
|
||||||
|
take: 10,
|
||||||
where: { limit: 1 },
|
where: { limit: 1 },
|
||||||
});
|
});
|
||||||
expect(entities).toEqual([fakeEntityCreated]);
|
expect(entities).toEqual({
|
||||||
|
data: [fakeEntityCreated],
|
||||||
|
total: 1,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||||
import { DeleteUserCommand } from '../../commands/delete-user.command';
|
import { DeleteUserCommand } from '../../commands/delete-user.command';
|
||||||
import { DeleteUserUseCase } from '../../domain/usecases/delete-user.usecase';
|
import { DeleteUserUseCase } from '../../domain/usecases/delete-user.usecase';
|
||||||
|
|
||||||
const usersMock = [
|
const mockUsers = [
|
||||||
{
|
{
|
||||||
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
|
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
|
||||||
firstName: 'John',
|
firstName: 'John',
|
||||||
|
@ -26,9 +26,9 @@ const usersMock = [
|
||||||
|
|
||||||
const mockUsersRepository = {
|
const mockUsersRepository = {
|
||||||
delete: jest.fn().mockImplementation((uuid: string) => {
|
delete: jest.fn().mockImplementation((uuid: string) => {
|
||||||
usersMock.forEach((user, index) => {
|
mockUsers.forEach((user, index) => {
|
||||||
if (user.uuid === uuid) {
|
if (user.uuid === uuid) {
|
||||||
usersMock.splice(index, 1);
|
mockUsers.splice(index, 1);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -58,11 +58,11 @@ describe('DeleteUserUseCase', () => {
|
||||||
|
|
||||||
describe('execute', () => {
|
describe('execute', () => {
|
||||||
it('should delete an User', async () => {
|
it('should delete an User', async () => {
|
||||||
const savedUuid = usersMock[0].uuid;
|
const savedUuid = mockUsers[0].uuid;
|
||||||
const deleteUserCommand = new DeleteUserCommand(savedUuid);
|
const deleteUserCommand = new DeleteUserCommand(savedUuid);
|
||||||
await deleteUserUseCase.execute(deleteUserCommand);
|
await deleteUserUseCase.execute(deleteUserCommand);
|
||||||
|
|
||||||
const deletedUser = usersMock.find((user) => user.uuid === savedUuid);
|
const deletedUser = mockUsers.find((user) => user.uuid === savedUuid);
|
||||||
expect(deletedUser).toBeUndefined();
|
expect(deletedUser).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||||
|
import { FindAllUsersUseCase } from '../../domain/usecases/find-all-users.usecase';
|
||||||
|
import { FindAllUsersQuery } from '../../queries/find-all-users.query';
|
||||||
|
|
||||||
|
const mockUsers = [
|
||||||
|
{
|
||||||
|
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
|
||||||
|
firstName: 'John',
|
||||||
|
lastName: 'Doe',
|
||||||
|
email: 'john.doe@email.com',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uuid: 'bb281075-1b98-4456-89d6-c643d3044a92',
|
||||||
|
firstName: 'Jane',
|
||||||
|
lastName: 'Doe',
|
||||||
|
email: 'jane.doe@email.com',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uuid: 'bb281075-1b98-4456-89d6-c643d3044a93',
|
||||||
|
firstName: 'Jimmy',
|
||||||
|
lastName: 'Doe',
|
||||||
|
email: 'jimmy.doe@email.com',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const mockUsersRepository = {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
findAll: jest.fn().mockImplementation((query?: FindAllUsersQuery) => {
|
||||||
|
return Promise.resolve(mockUsers);
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('FindAllUsersUseCase', () => {
|
||||||
|
let findAllUsersUseCase: FindAllUsersUseCase;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
{
|
||||||
|
provide: UsersRepository,
|
||||||
|
useValue: mockUsersRepository,
|
||||||
|
},
|
||||||
|
FindAllUsersUseCase,
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
findAllUsersUseCase = module.get<FindAllUsersUseCase>(FindAllUsersUseCase);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(findAllUsersUseCase).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('execute', () => {
|
||||||
|
it('should return an array filled with users', async () => {
|
||||||
|
const users = await findAllUsersUseCase.execute(new FindAllUsersQuery());
|
||||||
|
|
||||||
|
expect(users).toBe(mockUsers);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue