Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions backend/src/cost/__test__/cashflow-cost.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,14 @@ describe('CostService', () => {
).rejects.toThrow('Cost with name Food not found');
});

it('throws ConflictException when rename target already exists', async () => {
// --- Updated: was mocking ConditionalCheckFailedException directly which is wrong for transactWrite ---
it('throws ConflictException when rename target already exists (Put condition fails)', async () => {
mockGetPromise.mockResolvedValue({
Item: { name: 'Food', amount: 200, type: CostType.MealsFood },
});
mockTransactWritePromise.mockRejectedValue({
code: 'ConditionalCheckFailedException',
code: 'TransactionCanceledException',
CancellationReasons: [{ Code: 'ConditionalCheckFailed' }, { Code: 'None' }],
});

await expect(
Expand All @@ -573,6 +575,34 @@ describe('CostService', () => {
).rejects.toThrow('Cost with name Meals already exists');
});

// --- New: covers the Delete condition failing mid-transaction ---
it('throws NotFoundException when rename source disappears mid-transaction (Delete condition fails)', async () => {
mockGetPromise.mockResolvedValue({
Item: { name: 'Food', amount: 200, type: CostType.MealsFood },
});
mockTransactWritePromise.mockRejectedValue({
code: 'TransactionCanceledException',
CancellationReasons: [{ Code: 'None' }, { Code: 'ConditionalCheckFailed' }],
});

await expect(
service.updateCost('Food', {
name: 'Meals',
amount: 300,
type: CostType.MealsFood,
date: '2026-03-22' as TDateISO,
}),
).rejects.toThrow(NotFoundException);
await expect(
service.updateCost('Food', {
name: 'Meals',
amount: 300,
type: CostType.MealsFood,
date: '2026-03-22' as TDateISO,
}),
).rejects.toThrow('Cost with name Food not found');
});

it('throws InternalServerErrorException on rename transaction error', async () => {
mockGetPromise.mockResolvedValue({
Item: { name: 'Food', amount: 200, type: CostType.MealsFood },
Expand Down
Loading
Loading