Skip to content

Commit

Permalink
test: project insights read model test (#6657)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Mar 21, 2024
1 parent 85454bf commit 3241d14
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
@@ -0,0 +1,66 @@
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init';
import getLogger from '../../../test/fixtures/no-logger';
import type { IUser } from '../../types';
import type { IProjectInsightsReadModel } from './project-insights-read-model-type';
import {
type ChangeRequestDBState,
ProjectInsightsReadModel,
} from './project-insights-read-model';

let projectInsightsReadModel: IProjectInsightsReadModel;
let user: IUser;
let db: ITestDb;
const projectId = 'default';

beforeAll(async () => {
db = await dbInit('project_insights_read_model', getLogger);
projectInsightsReadModel = new ProjectInsightsReadModel(db.rawDatabase);
user = await db.stores.userStore.insert({
username: 'test',
});
});

afterAll(async () => {
await db.destroy();
});

beforeEach(async () => {
await db.rawDatabase.table('change_requests').delete();
});

const createChangeRequest = (id: number, state: string) =>
db.rawDatabase.table('change_requests').insert({
id,
state,
environment: 'default',
project: projectId,
created_by: user.id,
});

test('can read change request status counts', async () => {
const states: ChangeRequestDBState[] = [
'Approved',
'Approved',
'Applied',
'Rejected',
'Scheduled',
'In review',
'Draft',
'Cancelled',
];
await Promise.all(
states.map((state, id) => createChangeRequest(id, state)),
);

const changeRequests =
await projectInsightsReadModel.getChangeRequests(projectId);

expect(changeRequests).toEqual({
total: 6,
approved: 2,
applied: 1,
rejected: 1,
reviewRequired: 1,
scheduled: 1,
});
});
Expand Up @@ -5,6 +5,8 @@ import type {
import type { Db } from '../../db/db';

export type ChangeRequestDBState =
| 'Draft'
| 'Cancelled'
| 'Approved'
| 'In review'
| 'Applied'
Expand Down

0 comments on commit 3241d14

Please sign in to comment.