improve database tests

This commit is contained in:
sbriat 2023-01-27 12:18:32 +01:00
parent b3f3e174df
commit 199906b4e7
1 changed files with 176 additions and 45 deletions

View File

@ -3,6 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from '../../src/adapters/secondaries/prisma-service'; import { PrismaService } from '../../src/adapters/secondaries/prisma-service';
import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract'; import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract';
import { DatabaseException } from '../../src/exceptions/database.exception'; import { DatabaseException } from '../../src/exceptions/database.exception';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
class FakeEntity { class FakeEntity {
uuid?: string; uuid?: string;
@ -57,7 +58,20 @@ const mockPrismaService = {
return Promise.resolve([fakeEntities, fakeEntities.length]); return Promise.resolve([fakeEntities, fakeEntities.length]);
}), }),
fake: { fake: {
create: jest.fn().mockResolvedValue(fakeEntityCreated), create: jest
.fn()
.mockResolvedValueOnce(fakeEntityCreated)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new Error('an unknown error');
}),
findMany: jest.fn().mockImplementation((params?: any) => { findMany: jest.fn().mockImplementation((params?: any) => {
if (params?.where?.limit == 1) { if (params?.where?.limit == 1) {
@ -77,46 +91,99 @@ const mockPrismaService = {
); );
} }
if (!entity) { if (!entity && params?.where?.uuid == 'unknown') {
throw new PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
} else if (!entity) {
throw new Error('no entity'); throw new Error('no entity');
} }
return entity; return entity;
}), }),
findFirst: jest.fn().mockImplementation((params?: any) => { findFirst: jest
if (params?.where?.name) { .fn()
return Promise.resolve( .mockImplementationOnce((params?: any) => {
fakeEntities.find((entity) => entity.name === params?.where?.name), if (params?.where?.name) {
); return Promise.resolve(
} fakeEntities.find((entity) => entity.name === params?.where?.name),
}), );
update: jest.fn().mockImplementation((params: any) => {
const entity = fakeEntities.find(
(entity) => entity.uuid === params.where.uuid,
);
Object.entries(params.data).map(([key, value]) => {
entity[key] = value;
});
return Promise.resolve(entity);
}),
delete: jest.fn().mockImplementation((params: any) => {
let found = false;
fakeEntities.forEach((entity, index) => {
if (entity.uuid === params?.where?.uuid) {
found = true;
fakeEntities.splice(index, 1);
} }
}); })
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new Error('an unknown error');
}),
if (!found) { update: jest
throw new Error(); .fn()
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
}), .mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
})
.mockImplementationOnce((params: any) => {
const entity = fakeEntities.find(
(entity) => entity.name === params.where.name,
);
Object.entries(params.data).map(([key, value]) => {
entity[key] = value;
});
return Promise.resolve(entity);
})
.mockImplementation((params: any) => {
const entity = fakeEntities.find(
(entity) => entity.uuid === params.where.uuid,
);
Object.entries(params.data).map(([key, value]) => {
entity[key] = value;
});
return Promise.resolve(entity);
}),
delete: jest
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
})
.mockImplementation((params: any) => {
let found = false;
fakeEntities.forEach((entity, index) => {
if (entity.uuid === params?.where?.uuid) {
found = true;
fakeEntities.splice(index, 1);
}
});
if (!found) {
throw new Error();
}
}),
}, },
}; };
@ -180,14 +247,32 @@ describe('PrismaRepository', () => {
expect(newEntity).toBe(fakeEntityCreated); expect(newEntity).toBe(fakeEntityCreated);
expect(prisma.fake.create).toHaveBeenCalledTimes(1); expect(prisma.fake.create).toHaveBeenCalledTimes(1);
}); });
it('should throw a DatabaseException for client error', async () => {
await expect(
fakeRepository.create(fakeEntityToCreate),
).rejects.toBeInstanceOf(DatabaseException);
});
it('should throw a DatabaseException if uuid is not found', async () => {
await expect(
fakeRepository.create(fakeEntityToCreate),
).rejects.toBeInstanceOf(DatabaseException);
});
}); });
describe('findOne', () => { describe('findOneByUuid', () => {
it('should find an entity by uuid', async () => { it('should find an entity by uuid', async () => {
const entity = await fakeRepository.findOneByUuid(fakeEntities[0].uuid); const entity = await fakeRepository.findOneByUuid(fakeEntities[0].uuid);
expect(entity).toBe(fakeEntities[0]); expect(entity).toBe(fakeEntities[0]);
}); });
it('should throw a DatabaseException for client error', async () => {
await expect(
fakeRepository.findOneByUuid('unknown'),
).rejects.toBeInstanceOf(DatabaseException);
});
it('should throw a DatabaseException if uuid is not found', async () => { it('should throw a DatabaseException if uuid is not found', async () => {
await expect( await expect(
fakeRepository.findOneByUuid('wrong-uuid'), fakeRepository.findOneByUuid('wrong-uuid'),
@ -195,8 +280,55 @@ describe('PrismaRepository', () => {
}); });
}); });
describe('findOne', () => {
it('should find one entity', async () => {
const entity = await fakeRepository.findOne({
name: fakeEntities[0].name,
});
expect(entity.name).toBe(fakeEntities[0].name);
});
it('should throw a DatabaseException for client error', async () => {
await expect(
fakeRepository.findOne({
name: fakeEntities[0].name,
}),
).rejects.toBeInstanceOf(DatabaseException);
});
it('should throw a DatabaseException for unknown error', async () => {
await expect(
fakeRepository.findOne({
name: fakeEntities[0].name,
}),
).rejects.toBeInstanceOf(DatabaseException);
});
});
describe('update', () => { describe('update', () => {
it('should update an entity', async () => { it('should throw a DatabaseException for client error', async () => {
await expect(
fakeRepository.update('fake-uuid', { name: 'error' }),
).rejects.toBeInstanceOf(DatabaseException);
await expect(
fakeRepository.updateWhere({ name: 'error' }, { name: 'new error' }),
).rejects.toBeInstanceOf(DatabaseException);
});
it('should update an entity with name', async () => {
const newName = 'new-random-name';
await fakeRepository.updateWhere(
{ name: fakeEntities[0].name },
{
name: newName,
},
);
expect(fakeEntities[0].name).toBe(newName);
});
it('should update an entity with uuid', async () => {
const newName = 'random-name'; const newName = 'random-name';
await fakeRepository.update(fakeEntities[0].uuid, { await fakeRepository.update(fakeEntities[0].uuid, {
@ -209,10 +341,19 @@ describe('PrismaRepository', () => {
await expect( await expect(
fakeRepository.update('fake-uuid', { name: 'error' }), fakeRepository.update('fake-uuid', { name: 'error' }),
).rejects.toBeInstanceOf(DatabaseException); ).rejects.toBeInstanceOf(DatabaseException);
await expect(
fakeRepository.updateWhere({ name: 'error' }, { name: 'new error' }),
).rejects.toBeInstanceOf(DatabaseException);
}); });
}); });
describe('delete', () => { describe('delete', () => {
it('should throw a DatabaseException for client error', async () => {
await expect(fakeRepository.delete('fake-uuid')).rejects.toBeInstanceOf(
DatabaseException,
);
});
it('should delete an entity', async () => { it('should delete an entity', async () => {
const savedUuid = fakeEntities[0].uuid; const savedUuid = fakeEntities[0].uuid;
@ -231,14 +372,4 @@ describe('PrismaRepository', () => {
); );
}); });
}); });
describe('findOne', () => {
it('should find one entity', async () => {
const entity = await fakeRepository.findOne({
name: fakeEntities[0].name,
});
expect(entity.name).toBe(fakeEntities[0].name);
});
});
}); });