n8n/packages/cli/test/unit/repositories/workflowStatistics.test.ts
कारतोफ्फेलस्क्रिप्ट™ ff77ef4b62
ci: Delete some duplicate code in cli tests (no-changelog) (#9049)
2024-04-05 13:47:49 +02:00

55 lines
1.8 KiB
TypeScript

import { Container } from 'typedi';
import { type InsertResult, QueryFailedError } from '@n8n/typeorm';
import { mock, mockClear } from 'jest-mock-extended';
import { StatisticsNames, WorkflowStatistics } from '@db/entities/WorkflowStatistics';
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
import { mockEntityManager } from '../../shared/mocking';
describe('insertWorkflowStatistics', () => {
const entityManager = mockEntityManager(WorkflowStatistics);
const workflowStatisticsRepository = Container.get(WorkflowStatisticsRepository);
beforeEach(() => {
mockClear(entityManager.insert);
});
it('Successfully inserts data when it is not yet present', async () => {
entityManager.findOne.mockResolvedValueOnce(null);
entityManager.insert.mockResolvedValueOnce(mock<InsertResult>());
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
StatisticsNames.dataLoaded,
'workflowId',
);
expect(insertionResult).toBe('insert');
});
it('Does not insert when data is present', async () => {
entityManager.findOne.mockResolvedValueOnce(mock<WorkflowStatistics>());
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
StatisticsNames.dataLoaded,
'workflowId',
);
expect(insertionResult).toBe('alreadyExists');
expect(entityManager.insert).not.toHaveBeenCalled();
});
it('throws an error when insertion fails', async () => {
entityManager.findOne.mockResolvedValueOnce(null);
entityManager.insert.mockImplementation(async () => {
throw new QueryFailedError('Query', [], new Error('driver error'));
});
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
StatisticsNames.dataLoaded,
'workflowId',
);
expect(insertionResult).toBe('failed');
});
});