Merge pull request #5 from MatthieuTD/codex/prepare-minimal-context-for-repositories
Fix e2e inventory tests to rely on in-memory Prisma stub
This commit is contained in:
@@ -175,6 +175,21 @@ class InMemoryPrismaService {
|
|||||||
return fn(this);
|
return fn(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.sites = [];
|
||||||
|
this.typeComposants = [];
|
||||||
|
this.typePieces = [];
|
||||||
|
this.typeMachines = [];
|
||||||
|
this.typeMachineComponentRequirements = [];
|
||||||
|
this.typeMachinePieceRequirements = [];
|
||||||
|
this.machines = [];
|
||||||
|
this.composants = [];
|
||||||
|
this.pieces = [];
|
||||||
|
this.customFields = [];
|
||||||
|
this.customFieldValues = [];
|
||||||
|
this.profiles = [];
|
||||||
|
}
|
||||||
|
|
||||||
site = {
|
site = {
|
||||||
create: async ({ data }: any) => {
|
create: async ({ data }: any) => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@@ -898,6 +913,7 @@ class InMemoryPrismaService {
|
|||||||
describe('Inventory flow (e2e)', () => {
|
describe('Inventory flow (e2e)', () => {
|
||||||
let app: INestApplication;
|
let app: INestApplication;
|
||||||
let prismaStub: InMemoryPrismaService;
|
let prismaStub: InMemoryPrismaService;
|
||||||
|
let prisma: InMemoryPrismaService;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
prismaStub = new InMemoryPrismaService();
|
prismaStub = new InMemoryPrismaService();
|
||||||
@@ -913,25 +929,19 @@ describe('Inventory flow (e2e)', () => {
|
|||||||
app = moduleFixture.createNestApplication();
|
app = moduleFixture.createNestApplication();
|
||||||
await app.init();
|
await app.init();
|
||||||
|
|
||||||
prisma = app.get(PrismaService);
|
prisma = prismaStub;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(() => {
|
||||||
await prisma.$executeRawUnsafe(
|
prisma.reset();
|
||||||
'TRUNCATE TABLE custom_field_values, documents, pieces, composants, machines, composant_models, piece_models, type_machine_component_requirements, type_machine_piece_requirements, custom_fields, type_machines, type_composants, type_pieces, constructeurs, sites RESTART IDENTITY CASCADE',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(() => {
|
||||||
await app.close();
|
jest.restoreAllMocks();
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
await app.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a type, create a machine with new component/piece selections, and edit technical fields', async () => {
|
it('should create a type, create a machine with new component/piece selections, and edit technical fields', async () => {
|
||||||
@@ -1090,17 +1100,17 @@ describe('Inventory flow (e2e)', () => {
|
|||||||
|
|
||||||
describe('POST /composants', () => {
|
describe('POST /composants', () => {
|
||||||
it('accepts creation when requirement matches the machine skeleton', async () => {
|
it('accepts creation when requirement matches the machine skeleton', async () => {
|
||||||
prisma.machine.findUnique.mockResolvedValue({
|
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
|
||||||
id: 'machine-1',
|
id: 'machine-1',
|
||||||
typeMachine: {
|
typeMachine: {
|
||||||
componentRequirements: [
|
componentRequirements: [
|
||||||
{ id: 'req-1', typeComposantId: 'type-comp-1' },
|
{ id: 'req-1', typeComposantId: 'type-comp-1' },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
} as any);
|
||||||
|
|
||||||
const created = { id: 'component-1' };
|
const created = { id: 'component-1' };
|
||||||
prisma.composant.create.mockResolvedValue(created);
|
const createSpy = jest.spyOn(prisma.composant, 'create').mockResolvedValue(created as any);
|
||||||
|
|
||||||
const response = await request(app.getHttpServer())
|
const response = await request(app.getHttpServer())
|
||||||
.post('/composants')
|
.post('/composants')
|
||||||
@@ -1113,18 +1123,20 @@ describe('Inventory flow (e2e)', () => {
|
|||||||
.expect(201);
|
.expect(201);
|
||||||
|
|
||||||
expect(response.body).toEqual(created);
|
expect(response.body).toEqual(created);
|
||||||
expect(prisma.composant.create).toHaveBeenCalled();
|
expect(createSpy).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('refuses creation when requirement is not part of the machine skeleton', async () => {
|
it('refuses creation when requirement is not part of the machine skeleton', async () => {
|
||||||
prisma.machine.findUnique.mockResolvedValue({
|
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
|
||||||
id: 'machine-1',
|
id: 'machine-1',
|
||||||
typeMachine: {
|
typeMachine: {
|
||||||
componentRequirements: [
|
componentRequirements: [
|
||||||
{ id: 'req-1', typeComposantId: 'type-comp-1' },
|
{ id: 'req-1', typeComposantId: 'type-comp-1' },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
} as any);
|
||||||
|
|
||||||
|
const createSpy = jest.spyOn(prisma.composant, 'create');
|
||||||
|
|
||||||
await request(app.getHttpServer())
|
await request(app.getHttpServer())
|
||||||
.post('/composants')
|
.post('/composants')
|
||||||
@@ -1136,23 +1148,23 @@ describe('Inventory flow (e2e)', () => {
|
|||||||
})
|
})
|
||||||
.expect(400);
|
.expect(400);
|
||||||
|
|
||||||
expect(prisma.composant.create).not.toHaveBeenCalled();
|
expect(createSpy).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('POST /pieces', () => {
|
describe('POST /pieces', () => {
|
||||||
it('accepts creation when requirement matches the machine skeleton', async () => {
|
it('accepts creation when requirement matches the machine skeleton', async () => {
|
||||||
prisma.machine.findUnique.mockResolvedValue({
|
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
|
||||||
id: 'machine-1',
|
id: 'machine-1',
|
||||||
typeMachine: {
|
typeMachine: {
|
||||||
pieceRequirements: [
|
pieceRequirements: [
|
||||||
{ id: 'req-1', typePieceId: 'type-piece-1' },
|
{ id: 'req-1', typePieceId: 'type-piece-1' },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
} as any);
|
||||||
|
|
||||||
const created = { id: 'piece-1' };
|
const created = { id: 'piece-1' };
|
||||||
prisma.piece.create.mockResolvedValue(created);
|
const createSpy = jest.spyOn(prisma.piece, 'create').mockResolvedValue(created as any);
|
||||||
|
|
||||||
const response = await request(app.getHttpServer())
|
const response = await request(app.getHttpServer())
|
||||||
.post('/pieces')
|
.post('/pieces')
|
||||||
@@ -1165,18 +1177,20 @@ describe('Inventory flow (e2e)', () => {
|
|||||||
.expect(201);
|
.expect(201);
|
||||||
|
|
||||||
expect(response.body).toEqual(created);
|
expect(response.body).toEqual(created);
|
||||||
expect(prisma.piece.create).toHaveBeenCalled();
|
expect(createSpy).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('refuses creation when requirement is not part of the machine skeleton', async () => {
|
it('refuses creation when requirement is not part of the machine skeleton', async () => {
|
||||||
prisma.machine.findUnique.mockResolvedValue({
|
jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({
|
||||||
id: 'machine-1',
|
id: 'machine-1',
|
||||||
typeMachine: {
|
typeMachine: {
|
||||||
pieceRequirements: [
|
pieceRequirements: [
|
||||||
{ id: 'req-1', typePieceId: 'type-piece-1' },
|
{ id: 'req-1', typePieceId: 'type-piece-1' },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
} as any);
|
||||||
|
|
||||||
|
const createSpy = jest.spyOn(prisma.piece, 'create');
|
||||||
|
|
||||||
await request(app.getHttpServer())
|
await request(app.getHttpServer())
|
||||||
.post('/pieces')
|
.post('/pieces')
|
||||||
@@ -1188,7 +1202,7 @@ describe('Inventory flow (e2e)', () => {
|
|||||||
})
|
})
|
||||||
.expect(400);
|
.expect(400);
|
||||||
|
|
||||||
expect(prisma.piece.create).not.toHaveBeenCalled();
|
expect(createSpy).not.toHaveBeenCalled();
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user