mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 17:14:05 -08:00
55 lines
1.8 KiB
TypeScript
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');
|
|
});
|
|
});
|