tests for prisma
This commit is contained in:
parent
26dab584b2
commit
ef48e8ae68
|
@ -101,7 +101,7 @@
|
||||||
".port.ts",
|
".port.ts",
|
||||||
"libs/exceptions",
|
"libs/exceptions",
|
||||||
"libs/types",
|
"libs/types",
|
||||||
"prisma-service.base.ts",
|
"prisma.service.ts",
|
||||||
"main.ts"
|
"main.ts"
|
||||||
],
|
],
|
||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
|
@ -121,7 +121,7 @@
|
||||||
".port.ts",
|
".port.ts",
|
||||||
"libs/exceptions",
|
"libs/exceptions",
|
||||||
"libs/types",
|
"libs/types",
|
||||||
"prisma-service.base.ts",
|
"prisma.service.ts",
|
||||||
"main.ts"
|
"main.ts"
|
||||||
],
|
],
|
||||||
"coverageDirectory": "../coverage",
|
"coverageDirectory": "../coverage",
|
||||||
|
|
|
@ -2,7 +2,11 @@ import { ResponseBase } from '@libs/api/response.base';
|
||||||
import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base';
|
import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base';
|
||||||
import { PrismaService } from '@libs/db/prisma.service';
|
import { PrismaService } from '@libs/db/prisma.service';
|
||||||
import { AggregateID, AggregateRoot, Mapper, RepositoryPort } from '@libs/ddd';
|
import { AggregateID, AggregateRoot, Mapper, RepositoryPort } from '@libs/ddd';
|
||||||
import { ConflictException } from '@libs/exceptions';
|
import {
|
||||||
|
ConflictException,
|
||||||
|
DatabaseErrorException,
|
||||||
|
NotFoundException,
|
||||||
|
} from '@libs/exceptions';
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
@ -45,33 +49,33 @@ class FakeResponseDto extends ResponseBase {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fakeDbWriteModel: FakeModel = {
|
const fakeRecord: FakeModel = {
|
||||||
uuid: 'd567ea3b-4981-43c9-9449-a409b5fa9fed',
|
uuid: 'd567ea3b-4981-43c9-9449-a409b5fa9fed',
|
||||||
name: 'fakeName',
|
name: 'fakeName',
|
||||||
createdAt: new Date('2023-06-28T16:02:00Z'),
|
createdAt: new Date('2023-06-28T16:02:00Z'),
|
||||||
updatedAt: new Date('2023-06-28T16:02:00Z'),
|
updatedAt: new Date('2023-06-28T16:02:00Z'),
|
||||||
};
|
};
|
||||||
|
|
||||||
let modelId = 2;
|
let recordId = 2;
|
||||||
const modelUuid = 'uuid-';
|
const recordUuid = 'uuid-';
|
||||||
const modelName = 'name-';
|
const recordName = 'fakeName-';
|
||||||
|
|
||||||
const createRandomModel = (): FakeModel => {
|
const createRandomRecord = (): FakeModel => {
|
||||||
const fakeModel: FakeModel = {
|
const fakeRecord: FakeModel = {
|
||||||
uuid: `${modelUuid}${modelId}`,
|
uuid: `${recordUuid}${recordId}`,
|
||||||
name: `${modelName}${modelId}`,
|
name: `${recordName}${recordId}`,
|
||||||
createdAt: new Date('2023-06-30T08:00:00Z'),
|
createdAt: new Date('2023-06-30T08:00:00Z'),
|
||||||
updatedAt: new Date('2023-06-30T08:00:00Z'),
|
updatedAt: new Date('2023-06-30T08:00:00Z'),
|
||||||
};
|
};
|
||||||
|
|
||||||
modelId++;
|
recordId++;
|
||||||
|
|
||||||
return fakeModel;
|
return fakeRecord;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fakeModels: FakeModel[] = [];
|
const fakeRecords: FakeModel[] = [];
|
||||||
Array.from({ length: 10 }).forEach(() => {
|
Array.from({ length: 10 }).forEach(() => {
|
||||||
fakeModels.push(createRandomModel());
|
fakeRecords.push(createRandomRecord());
|
||||||
});
|
});
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
@ -117,12 +121,6 @@ class FakePrismaService extends PrismaService {
|
||||||
const mockPrismaService = {
|
const mockPrismaService = {
|
||||||
$queryRaw: jest
|
$queryRaw: jest
|
||||||
.fn()
|
.fn()
|
||||||
.mockImplementationOnce(() => {
|
|
||||||
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
|
||||||
code: 'code',
|
|
||||||
clientVersion: 'version',
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
|
@ -131,11 +129,14 @@ const mockPrismaService = {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
throw new Error();
|
||||||
}),
|
}),
|
||||||
fake: {
|
fake: {
|
||||||
create: jest
|
create: jest
|
||||||
.fn()
|
.fn()
|
||||||
.mockResolvedValueOnce(fakeDbWriteModel)
|
.mockResolvedValueOnce(fakeRecord)
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
throw new Prisma.PrismaClientKnownRequestError('Already exists', {
|
throw new Prisma.PrismaClientKnownRequestError('Already exists', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
|
@ -143,28 +144,26 @@ const mockPrismaService = {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
throw new Error('an unknown error');
|
throw new Error('An unknown error');
|
||||||
}),
|
}),
|
||||||
|
|
||||||
findUnique: jest.fn().mockImplementation(async (params?: any) => {
|
findUnique: jest.fn().mockImplementation(async (params?: any) => {
|
||||||
let model: FakeModel;
|
let record: FakeModel;
|
||||||
|
|
||||||
if (params?.where?.uuid) {
|
if (params?.where?.uuid) {
|
||||||
model = fakeModels.find(
|
record = fakeRecords.find(
|
||||||
(entity) => entity.uuid === params?.where?.uuid,
|
(record) => record.uuid === params?.where?.uuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!model && params?.where?.uuid == 'unknown') {
|
if (!record && params?.where?.uuid == 'uuid-triggering-error') {
|
||||||
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
} else if (!model) {
|
|
||||||
throw new Error('No record found');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return model;
|
return record;
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -216,7 +215,7 @@ describe('PrismaRepositoryBase', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('insert', () => {
|
describe('insert', () => {
|
||||||
it('should create a model', async () => {
|
it('should create a record', async () => {
|
||||||
jest.spyOn(prisma.fake, 'create');
|
jest.spyOn(prisma.fake, 'create');
|
||||||
|
|
||||||
await fakeRepository.insert(
|
await fakeRepository.insert(
|
||||||
|
@ -227,7 +226,7 @@ describe('PrismaRepositoryBase', () => {
|
||||||
expect(prisma.fake.create).toHaveBeenCalledTimes(1);
|
expect(prisma.fake.create).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw a ConflictException if model already exists', async () => {
|
it('should throw a ConflictException if record already exists', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
fakeRepository.insert(
|
fakeRepository.insert(
|
||||||
FakeEntity.create({
|
FakeEntity.create({
|
||||||
|
@ -237,7 +236,7 @@ describe('PrismaRepositoryBase', () => {
|
||||||
).rejects.toBeInstanceOf(ConflictException);
|
).rejects.toBeInstanceOf(ConflictException);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an exception an error occurs', async () => {
|
it('should throw an Error if an error occurs', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
fakeRepository.insert(
|
fakeRepository.insert(
|
||||||
FakeEntity.create({
|
FakeEntity.create({
|
||||||
|
@ -247,4 +246,42 @@ describe('PrismaRepositoryBase', () => {
|
||||||
).rejects.toBeInstanceOf(Error);
|
).rejects.toBeInstanceOf(Error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('findOneById', () => {
|
||||||
|
it('should find a record by its id', async () => {
|
||||||
|
const record = await fakeRepository.findOneById('uuid-3');
|
||||||
|
expect(record.getProps().name).toBe('fakeName-3');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an Error for client error', async () => {
|
||||||
|
await expect(
|
||||||
|
fakeRepository.findOneById('uuid-triggering-error'),
|
||||||
|
).rejects.toBeInstanceOf(Error);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw a NotFoundException if id is not found', async () => {
|
||||||
|
await expect(
|
||||||
|
fakeRepository.findOneById('wrong-id'),
|
||||||
|
).rejects.toBeInstanceOf(NotFoundException);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('healthCheck', () => {
|
||||||
|
it('should return a healthy result', async () => {
|
||||||
|
const res = await fakeRepository.healthCheck();
|
||||||
|
expect(res).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an exception if database is not available', async () => {
|
||||||
|
await expect(fakeRepository.healthCheck()).rejects.toBeInstanceOf(
|
||||||
|
DatabaseErrorException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw a DatabaseErrorException if an error occurs', async () => {
|
||||||
|
await expect(fakeRepository.healthCheck()).rejects.toBeInstanceOf(
|
||||||
|
DatabaseErrorException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue