Skip to content

Commit

Permalink
Test fixes after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Dec 14, 2024
1 parent 40b821b commit 8a4443a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/lib/manage-hierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import _ from 'lodash';

import Auth from './authentication';
import { ChtApi, RemotePlace } from './cht-api';
import { ChtApi } from './cht-api';
import { ChtConfJobData } from '../worker/cht-conf-worker';
import { ContactType } from '../config';
import { JobParams, IQueue, getChtConfQueue } from './queues';
import Place from '../services/place';
import RemotePlaceResolver from './remote-place-resolver';
import SessionCache from '../services/session-cache';
import { RemotePlace } from './remote-place-cache';

export const HIERARCHY_ACTIONS = ['move', 'merge', 'delete'];
export type HierarchyAction = typeof HIERARCHY_ACTIONS[number];
Expand Down Expand Up @@ -85,7 +86,7 @@ function getSourceAndDestinationIds(

if (hierarchyAction === 'move') {
if (destinationId === sourceLineage[1]?.id) {
throw Error(`Place "${sourceLineage[0]?.name}" already has "${destinationLineage[1]?.name}" as parent`);
throw Error(`Place "${sourceLineage[0]?.name.original}" already has "${destinationLineage[1]?.name.original}" as parent`);
}
}

Expand Down Expand Up @@ -129,7 +130,7 @@ async function resolve(prefix: string, formData: any, contactType: ContactType,
await RemotePlaceResolver.resolveOne(place, sessionCache, chtApi, { fuzz: true });
place.validate();

const validationError = place.validationErrors && Object.keys(place.validationErrors).find(err => err.startsWith('hierarchy_'));
const validationError = place.validationErrors && Object.keys(place.validationErrors).find(err => err.startsWith(prefix));
if (validationError) {
throw Error(place.validationErrors?.[validationError]);
}
Expand Down
44 changes: 23 additions & 21 deletions test/lib/manage-hierarchy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,35 @@ import { mockChtApi } from '../mocks';
import chaiAsPromised from 'chai-as-promised';
import Auth from '../../src/lib/authentication';
import { BullQueue } from '../../src/lib/queues';
import RemotePlaceCache from '../../src/lib/remote-place-cache';
Chai.use(chaiAsPromised);

const { expect } = Chai;

describe('lib/manage-hierarchy.ts', () => {
let chtConfQueue: any;

const subcountyDocs = [
{ _id: 'from-sub', name: 'From Sub' },
{ _id: 'to-sub', name: 'To Sub' }
];
const chuDocs = [
{ _id: 'from-chu-id', name: 'c-h-u', parent: { _id: 'from-sub' } },
{ _id: 'to-chu-id', name: 'destination', parent: { _id: 'to-sub' } },
];

const chtApiWithDocs = () => mockChtApi(subcountyDocs, chuDocs);

beforeEach(() => {
chtConfQueue = sinon.createStubInstance(BullQueue);
sinon.stub(Auth, 'encodeTokenForWorker').returns('encoded-token');
RemotePlaceCache.clear({});
});

afterEach(() => {
sinon.restore();
});

const chtApi = () => mockChtApi(
[
{ id: 'from-sub', name: 'From Sub', lineage: [], type: 'remote' },
{ id: 'to-sub', name: 'To Sub', lineage: [], type: 'remote' }
],
[
{ id: 'from-chu-id', name: 'c-h-u', lineage: ['from-sub'], type: 'remote' },
{ id: 'to-chu-id', name: 'destination', lineage: ['to-sub'], type: 'remote' }
],
);

describe('move', () => {
it('move CHU: success', async () => {
const formData = {
Expand All @@ -47,15 +49,15 @@ describe('lib/manage-hierarchy.ts', () => {
const contactType = Config.getContactType('c_community_health_unit');
const sessionCache = new SessionCache();

const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue);
const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue);
expect(actual.sourceLineage.map((l:any) => l.id)).to.deep.eq(['from-chu-id', 'from-sub']);
expect(actual.destinationLineage.map((l:any) => l.id)).to.deep.eq([undefined, 'to-sub']);

// Verify the data passed to mockmoveContactQueue
expect(chtConfQueue.add.calledOnce).to.be.true;
const jobParams = chtConfQueue.add.getCall(0).args[0];

expect(jobParams).to.have.property('jobName').that.equals('move_[From Sub.c-h-u]_to_[To Sub]');
expect(jobParams).to.have.property('jobName').that.equals('move_[From Sub.C-h-u]_to_[To Sub]');
expect(jobParams).to.have.property('jobData').that.deep.include({
action: 'move',
sourceId: 'from-chu-id',
Expand All @@ -74,7 +76,7 @@ describe('lib/manage-hierarchy.ts', () => {
const contactType = Config.getContactType('c_community_health_unit');
const sessionCache = new SessionCache();

const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue);
const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, mockChtApi(chuDocs), chtConfQueue);
await expect(actual).to.eventually.be.rejectedWith('search string is empty');
});

Expand All @@ -88,7 +90,7 @@ describe('lib/manage-hierarchy.ts', () => {
const contactType = Config.getContactType('c_community_health_unit');
const sessionCache = new SessionCache();

const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue);
const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue);
await expect(actual).to.eventually.be.rejectedWith('Place "c-h-u" already has "From Sub" as parent');
});

Expand All @@ -102,8 +104,8 @@ describe('lib/manage-hierarchy.ts', () => {
const contactType = Config.getContactType('c_community_health_unit');
const sessionCache = new SessionCache();

const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue);
await expect(actual).to.eventually.be.rejectedWith('Cannot find \'b_sub_county\' matching \'Invalid Sub\'');
const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue);
await expect(actual).to.eventually.be.rejectedWith('Cannot find \'b_sub_county\' matching \'invalid sub\'');
});
});

Expand All @@ -119,15 +121,15 @@ describe('lib/manage-hierarchy.ts', () => {
const contactType = Config.getContactType('c_community_health_unit');
const sessionCache = new SessionCache();

const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue);
const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue);
expect(actual.sourceLineage.map((l:any) => l.id)).to.deep.eq(['from-chu-id', 'from-sub']);
expect(actual.destinationLineage.map((l:any) => l.id)).to.deep.eq(['to-chu-id', 'to-sub']);

// Verify the data passed to mockmoveContactQueue
expect(chtConfQueue.add.calledOnce).to.be.true;
const jobParams = chtConfQueue.add.getCall(0).args[0];

expect(jobParams).to.have.property('jobName').that.equals('merge_[From Sub.c-h-u]_to_[To Sub.destination]');
expect(jobParams).to.have.property('jobName').that.equals('merge_[From Sub.C-h-u]_to_[To Sub.Destination]');
expect(jobParams).to.have.property('jobData').that.deep.include({
action: 'merge',
sourceId: 'from-chu-id',
Expand All @@ -148,15 +150,15 @@ describe('lib/manage-hierarchy.ts', () => {
const contactType = Config.getContactType('c_community_health_unit');
const sessionCache = new SessionCache();

const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue);
const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue);
expect(actual.sourceLineage.map((l:any) => l.id)).to.deep.eq(['from-chu-id', 'from-sub']);
expect(actual.destinationLineage.map((l:any) => l.id)).to.deep.eq([]);

// Verify the data passed to mockmoveContactQueue
expect(chtConfQueue.add.calledOnce).to.be.true;
const jobParams = chtConfQueue.add.getCall(0).args[0];

expect(jobParams).to.have.property('jobName').that.equals('delete_[From Sub.c-h-u]');
expect(jobParams).to.have.property('jobName').that.equals('delete_[From Sub.C-h-u]');
expect(jobParams).to.have.property('jobData').that.deep.include({
action: 'delete',
sourceId: 'from-chu-id',
Expand Down

0 comments on commit 8a4443a

Please sign in to comment.