test(db): add PhaseRepository findByNumber and getNextNumber tests

- Test findByNumber with matching/non-matching initiative and number
- Test getNextNumber returns 1 for empty initiative
- Test getNextNumber returns max + 1 with existing phases
- Test getNextNumber handles gaps in numbering
This commit is contained in:
Lukas May
2026-01-31 19:24:09 +01:00
parent de57c15666
commit 1f4a95df4a

View File

@@ -168,4 +168,93 @@ describe('DrizzlePhaseRepository', () => {
);
});
});
describe('findByNumber', () => {
it('should find phase by initiative and number', async () => {
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 1,
name: 'Phase 1',
});
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 2,
name: 'Phase 2',
});
const found = await phaseRepo.findByNumber(testInitiativeId, 1);
expect(found).not.toBeNull();
expect(found?.name).toBe('Phase 1');
const foundTwo = await phaseRepo.findByNumber(testInitiativeId, 2);
expect(foundTwo).not.toBeNull();
expect(foundTwo?.name).toBe('Phase 2');
});
it('should return null for non-existent number', async () => {
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 1,
name: 'Phase 1',
});
const found = await phaseRepo.findByNumber(testInitiativeId, 99);
expect(found).toBeNull();
});
it('should return null for wrong initiative', async () => {
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 1,
name: 'Phase 1',
});
// Create another initiative
const otherInitiative = await initiativeRepo.create({
name: 'Other Initiative',
});
const found = await phaseRepo.findByNumber(otherInitiative.id, 1);
expect(found).toBeNull();
});
});
describe('getNextNumber', () => {
it('should return 1 for empty initiative', async () => {
const next = await phaseRepo.getNextNumber(testInitiativeId);
expect(next).toBe(1);
});
it('should return max + 1', async () => {
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 1,
name: 'P1',
});
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 2,
name: 'P2',
});
const next = await phaseRepo.getNextNumber(testInitiativeId);
expect(next).toBe(3);
});
it('should handle gaps in numbering', async () => {
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 1,
name: 'P1',
});
await phaseRepo.create({
initiativeId: testInitiativeId,
number: 5,
name: 'P5',
});
const next = await phaseRepo.getNextNumber(testInitiativeId);
expect(next).toBe(6);
});
});
});