mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
refactor: Clean up workflow stats test file (#5282)
* 🔨 - Use .spyOn for UserManagment * 🔨 - Remove a no longer needed test * 🔨 - Refactored and cleaned up workflowstats tests * 🔨 - Clean up unused imports / lines
This commit is contained in:
parent
6c8570adcc
commit
4bd9ed675e
|
@ -1,10 +1,11 @@
|
|||
import config from '@/config';
|
||||
import { InternalHooksManager } from '@/InternalHooksManager';
|
||||
import { nodeFetchedData, workflowExecutionCompleted } from '@/events/WorkflowStatistics';
|
||||
import { LoggerProxy, WorkflowExecuteMode } from 'n8n-workflow';
|
||||
import { getLogger } from '@/Logger';
|
||||
import { StatisticsNames } from '@db/entities/WorkflowStatistics';
|
||||
import { QueryFailedError } from 'typeorm';
|
||||
import config from '@/config';
|
||||
import { Db } from '@/index';
|
||||
import { nodeFetchedData, workflowExecutionCompleted } from '@/events/WorkflowStatistics';
|
||||
import { InternalHooksManager } from '@/InternalHooksManager';
|
||||
import { getLogger } from '@/Logger';
|
||||
import * as UserManagementHelper from '@/UserManagement/UserManagementHelper';
|
||||
|
||||
const FAKE_USER_ID = 'abcde-fghij';
|
||||
|
||||
|
@ -22,31 +23,15 @@ jest.spyOn(InternalHooksManager, 'getInstance').mockImplementation((...args) =>
|
|||
jest.mock('@/Db', () => {
|
||||
return {
|
||||
collections: {
|
||||
Workflow: {
|
||||
update: jest.fn(({ id, dataLoaded }, updateArgs) => {
|
||||
if (id === '1') return { affected: 1 };
|
||||
return { affected: 0 };
|
||||
}),
|
||||
},
|
||||
WorkflowStatistics: {
|
||||
// Have made a tech debt ticket to refactor this test suite for later
|
||||
insert: jest.fn(({ count, name, workflowId }) => {
|
||||
if (workflowId === '-1') throw new QueryFailedError('test error', [], '');
|
||||
else if (name === StatisticsNames.dataLoaded && workflowId === '2')
|
||||
throw new QueryFailedError('test error 2', [], '');
|
||||
return null;
|
||||
}),
|
||||
insert: jest.fn((...args) => {}),
|
||||
update: jest.fn((...args) => {}),
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
jest.mock('@/UserManagement/UserManagementHelper', () => {
|
||||
return {
|
||||
getWorkflowOwner: jest.fn((workflowId) => {
|
||||
return { id: FAKE_USER_ID };
|
||||
}),
|
||||
};
|
||||
jest.spyOn(UserManagementHelper, 'getWorkflowOwner').mockImplementation(async (workflowId) => {
|
||||
return { id: FAKE_USER_ID };
|
||||
});
|
||||
|
||||
describe('Events', () => {
|
||||
|
@ -69,25 +54,6 @@ describe('Events', () => {
|
|||
afterEach(() => {});
|
||||
|
||||
describe('workflowExecutionCompleted', () => {
|
||||
test('should fail with an invalid workflowId', async () => {
|
||||
const workflow = {
|
||||
id: 'abcde',
|
||||
name: '',
|
||||
active: false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
nodes: [],
|
||||
connections: {},
|
||||
};
|
||||
const runData = {
|
||||
finished: true,
|
||||
data: { resultData: { runData: {} } },
|
||||
mode: 'internal' as WorkflowExecuteMode,
|
||||
startedAt: new Date(),
|
||||
};
|
||||
await workflowExecutionCompleted(workflow, runData);
|
||||
});
|
||||
|
||||
test('should create metrics for production successes', async () => {
|
||||
// Call the function with a production success result, ensure metrics hook gets called
|
||||
const workflow = {
|
||||
|
@ -135,9 +101,12 @@ describe('Events', () => {
|
|||
});
|
||||
|
||||
test('should not send metrics for updated entries', async () => {
|
||||
// Call the function with the id that causes insert to fail, ensure update is called *and* metrics aren't sent
|
||||
// Call the function with a fail insert, ensure update is called *and* metrics aren't sent
|
||||
Db.collections.WorkflowStatistics.insert.mockImplementationOnce((...args) => {
|
||||
throw new QueryFailedError('invalid insert', [], '');
|
||||
});
|
||||
const workflow = {
|
||||
id: '-1',
|
||||
id: '1',
|
||||
name: '',
|
||||
active: false,
|
||||
createdAt: new Date(),
|
||||
|
@ -157,19 +126,6 @@ describe('Events', () => {
|
|||
});
|
||||
|
||||
describe('nodeFetchedData', () => {
|
||||
test('should fail with an invalid workflowId', async () => {
|
||||
const workflowId = 'abcde';
|
||||
const node = {
|
||||
id: 'abcde',
|
||||
name: 'test node',
|
||||
typeVersion: 1,
|
||||
type: '',
|
||||
position: [0, 0] as [number, number],
|
||||
parameters: {},
|
||||
};
|
||||
await nodeFetchedData(workflowId, node);
|
||||
});
|
||||
|
||||
test('should create metrics when the db is updated', async () => {
|
||||
// Call the function with a production success result, ensure metrics hook gets called
|
||||
const workflowId = '1';
|
||||
|
@ -222,7 +178,10 @@ describe('Events', () => {
|
|||
|
||||
test('should not send metrics for entries that already have the flag set', async () => {
|
||||
// Fetch data for workflow 2 which is set up to not be altered in the mocks
|
||||
const workflowId = '2';
|
||||
Db.collections.WorkflowStatistics.insert.mockImplementationOnce((...args) => {
|
||||
throw new QueryFailedError('invalid insert', [], '');
|
||||
});
|
||||
const workflowId = '1';
|
||||
const node = {
|
||||
id: 'abcde',
|
||||
name: 'test node',
|
||||
|
|
Loading…
Reference in a new issue