Skip to content

Commit

Permalink
Change DataContext.get to DataContext.bind
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester committed Jun 10, 2024
1 parent 3128337 commit d3b9532
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion api/src/controllers/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const ctx = require('../services/data-context');
const serverUtils = require('../server-utils');
const auth = require('../auth');

const getPerson = (qualifier) => ctx.get(Person.v1.get)(qualifier);
const getPerson = (qualifier) => ctx.bind(Person.v1.get)(qualifier);

module.exports = {
v1: {
Expand Down
4 changes: 1 addition & 3 deletions api/src/services/data-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ const { getLocalDataContext } = require('@medic/cht-datasource');
const db = require('../db');
const config = require('../config');

const dataContext = getLocalDataContext(config, db);

Object.assign(module.exports, dataContext);
module.exports = getLocalDataContext(config, db);
12 changes: 6 additions & 6 deletions api/tests/mocha/controllers/person.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const serverUtils = require('../../../src/server-utils');

describe('Person Controller', () => {
let authCheck;
let dataContextGet;
let dataContextBind;
let serverUtilsError;
let req;
let res;

beforeEach(() => {
authCheck = sinon.stub(auth, 'check');
dataContextGet = sinon.stub(dataContext, 'get');
dataContextBind = sinon.stub(dataContext, 'bind');
serverUtilsError = sinon.stub(serverUtils, 'error');
res = {
json: sinon.stub(),
Expand All @@ -36,7 +36,7 @@ describe('Person Controller', () => {
.stub(Qualifier, 'byUuid')
.returns(qualifier);
personGet = sinon.stub();
dataContextGet
dataContextBind
.withArgs(Person.v1.get)
.returns(personGet);
});
Expand All @@ -48,7 +48,7 @@ describe('Person Controller', () => {
await controller.v1.get(req, res);

expect(authCheck.calledOnceWithExactly(req, 'can_view_contacts')).to.be.true;
expect(dataContextGet.calledOnceWithExactly(Person.v1.get)).to.be.true;
expect(dataContextBind.calledOnceWithExactly(Person.v1.get)).to.be.true;
expect(byUuid.calledOnceWithExactly(req.params.uuid)).to.be.true;
expect(personGet.calledOnceWithExactly(qualifier)).to.be.true;
expect(res.json.calledOnceWithExactly(person)).to.be.true;
Expand All @@ -61,7 +61,7 @@ describe('Person Controller', () => {
await controller.v1.get(req, res);

expect(authCheck.calledOnceWithExactly(req, 'can_view_contacts')).to.be.true;
expect(dataContextGet.calledOnceWithExactly(Person.v1.get)).to.be.true;
expect(dataContextBind.calledOnceWithExactly(Person.v1.get)).to.be.true;
expect(byUuid.calledOnceWithExactly(req.params.uuid)).to.be.true;
expect(personGet.calledOnceWithExactly(qualifier)).to.be.true;
expect(res.json.notCalled).to.be.true;
Expand All @@ -79,7 +79,7 @@ describe('Person Controller', () => {
await controller.v1.get(req, res);

expect(authCheck.calledOnceWithExactly(req, 'can_view_contacts')).to.be.true;
expect(dataContextGet.notCalled).to.be.true;
expect(dataContextBind.notCalled).to.be.true;
expect(byUuid.notCalled).to.be.true;
expect(personGet.notCalled).to.be.true;
expect(res.json.notCalled).to.be.true;
Expand Down
4 changes: 2 additions & 2 deletions api/tests/mocha/services/data-context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Data context service', () => {
it('is initialized with the methods from the data context', () => {
const expectedDataContext = dataSource.getLocalDataContext(config, db);

expect(dataContext.get).is.a('function');
expect(dataContext).excluding('get').to.deep.equal(expectedDataContext);
expect(dataContext.bind).is.a('function');
expect(dataContext).excluding('bind').to.deep.equal(expectedDataContext);
});
});
2 changes: 1 addition & 1 deletion shared-libs/cht-datasource/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const getDatasource = (ctx: DataContext) => {
* @param uuid the UUID of the person to retrieve
* @returns the person or `null` if no person is found for the UUID
*/
getByUuid: (uuid: string) => ctx.get(Person.v1.get)(Qualifier.byUuid(uuid)),
getByUuid: (uuid: string) => ctx.bind(Person.v1.get)(Qualifier.byUuid(uuid)),
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion shared-libs/cht-datasource/src/libs/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export const hasFields = (

/** @internal */
export abstract class AbstractDataContext implements DataContext {
readonly get = <T>(fn: (ctx: DataContext) => T): T => fn(this);
readonly bind = <T>(fn: (ctx: DataContext) => T): T => fn(this);
}
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/src/libs/data-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export interface DataContext {
* @param fn the function to execute
* @returns the result of the function
*/
get: <T>(fn: (ctx: DataContext) => T) => T
bind: <T>(fn: (ctx: DataContext) => T) => T
}

const isDataContext = (context: unknown): context is DataContext => {
return isRecord(context) && hasField(context, { name: 'get', type: 'function' });
return isRecord(context) && hasField(context, { name: 'bind', type: 'function' });
};

/** @internal */
Expand Down
10 changes: 5 additions & 5 deletions shared-libs/cht-datasource/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { DataContext } from '../src';

describe('CHT Script API - getDatasource', () => {
let dataContext: DataContext;
let dataContextGet: SinonStub;
let dataContextBind: SinonStub;
let assertDataContext: SinonStub;
let datasource: ReturnType<typeof Index.getDatasource>;

beforeEach(() => {
dataContextGet = sinon.stub();
dataContext = { get: dataContextGet };
dataContextBind = sinon.stub();
dataContext = { bind: dataContextBind };
assertDataContext = sinon.stub(Context, 'assertDataContext');
datasource = Index.getDatasource(dataContext);
});
Expand Down Expand Up @@ -57,14 +57,14 @@ describe('CHT Script API - getDatasource', () => {
it('getByUuid', async () => {
const expectedPerson = {};
const personGet = sinon.stub().resolves(expectedPerson);
dataContextGet.returns(personGet);
dataContextBind.returns(personGet);
const qualifier = { uuid: 'my-persons-uuid' };
const byUuid = sinon.stub(Qualifier, 'byUuid').returns(qualifier);

const returnedPerson = await person.getByUuid(qualifier.uuid);

expect(returnedPerson).to.equal(expectedPerson);
expect(dataContextGet.calledOnceWithExactly(Person.v1.get)).to.be.true;
expect(dataContextBind.calledOnceWithExactly(Person.v1.get)).to.be.true;
expect(personGet.calledOnceWithExactly(qualifier)).to.be.true;
expect(byUuid.calledOnceWithExactly(qualifier.uuid)).to.be.true;
});
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/test/libs/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ describe('core lib', () => {
describe('AbstractDataContext', () => {
class TestDataContext extends AbstractDataContext { }

it('get', () => {
it('bind', () => {
const ctx = new TestDataContext();
const testFn = sinon.stub().returns('test');

const result = ctx.get<string>(testFn);
const result = ctx.bind<string>(testFn);

expect(result).to.equal('test');
expect(testFn.calledOnceWithExactly(ctx)).to.be.true;
Expand Down
2 changes: 1 addition & 1 deletion shared-libs/cht-datasource/test/libs/data-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DataContext } from '../../dist';


describe('context lib', () => {
const context = { get: sinon.stub() } as DataContext;
const context = { bind: sinon.stub() } as DataContext;
let isLocalDataContext: SinonStub;
let isRemoteDataContext: SinonStub;
let assertRemoteDataContext: SinonStub;
Expand Down

0 comments on commit d3b9532

Please sign in to comment.