Skip to content

Commit

Permalink
Merge branch 'master' into tokenPage
Browse files Browse the repository at this point in the history
  • Loading branch information
annyhe committed Dec 14, 2016
2 parents c98657b + edc9414 commit ce9a6b2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
15 changes: 9 additions & 6 deletions db/helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ const changeType = {
};

/**
* Takes a sample instance and enhances it with the subject instance
* Takes a sample instance and enhances it with the subject instance and
* aspect instance
* @param {Sequelize} seq - A reference to Sequelize to have access to the
* the Promise class.
* @param {Instance} inst - The Sample Instance.
* @returns {Promise} - Returns a promise which resolves to sample instance
* enhanced with subject instance information.
* enhanced with subject instance and aspect instance information.
*/
function augmentSampleWithSubjectInfo(seq, inst) {
function augmentSampleWithSubjectAspectInfo(seq, inst) {
return new seq.Promise((resolve, reject) => {
inst.getSubject()
.then((sub) => {
Expand All @@ -48,13 +49,15 @@ function augmentSampleWithSubjectInfo(seq, inst) {
if (sub) {
inst.dataValues.absolutePath = sub.absolutePath;
}

inst.subject = sub;
}).then(() => inst.getAspect())
.then((asp) => {
inst.dataValues.aspect = asp;
resolve(inst);
})
.catch((err) => reject(err));
});
}
} // augmentSampleWithSubjectAspectInfo

/**
* This function checks if the aspect and subject associated with the sample
Expand Down Expand Up @@ -235,7 +238,7 @@ module.exports = {
setIsDeleted,
publishChange,
sampleAspectAndSubjectArePublished,
augmentSampleWithSubjectInfo,
augmentSampleWithSubjectAspectInfo,
validateJsonSchema,
createDBLog,
changeType,
Expand Down
6 changes: 3 additions & 3 deletions db/model/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ module.exports = function sample(seq, dataTypes) {
if (published) {
// augment the sample instance with the subject instance to enable
// filtering by subjecttags in the realtime socketio module
common.augmentSampleWithSubjectInfo(seq, samp)
common.augmentSampleWithSubjectAspectInfo(seq, samp)
.then(() => common.publishChange(samp, eventName.add));
}
});
Expand All @@ -326,7 +326,7 @@ module.exports = function sample(seq, dataTypes) {
if (published) {
// augument the sample instance with the subject instance to enable
// filtering by subjecttags in the realtime socketio module
common.augmentSampleWithSubjectInfo(seq, inst)
common.augmentSampleWithSubjectAspectInfo(seq, inst)
.then(() => common.publishChange(inst, eventName.del));
}
});
Expand Down Expand Up @@ -354,7 +354,7 @@ module.exports = function sample(seq, dataTypes) {
if (published) {
// augument the sample instance with the subject instance to enable
// filtering by subjecttags in the realtime socketio module
common.augmentSampleWithSubjectInfo(seq, inst)
common.augmentSampleWithSubjectAspectInfo(seq, inst)
.then(() => common.publishChange(inst, eventName.upd, changedKeys,
ignoreAttributes));
}
Expand Down
51 changes: 30 additions & 21 deletions tests/db/helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ describe('utility function tests:', () => {
it('destory with a model', (done) => {
const par = { name: `${tu.namePrefix}Gamma`, isPublished: true };
Subject.create(par)
.then((o) => {
return o.destroy();
})
.then((o) => o.destroy())
.then((o) => {
const obj = common.publishChange(o, 'delete');
expect(obj).to.have.property('delete');
Expand All @@ -50,9 +48,7 @@ describe('utility function tests:', () => {
it('update a model instance', (done) => {
const par = { name: `${tu.namePrefix}Kappa`, isPublished: false };
Subject.create(par)
.then((o) => {
return o.update({ isPublished: true });
})
.then((o) => o.update({ isPublished: true }))
.then((o) => {
const changedKeys = Object.keys(o._changed);
const ignoreAttributes = ['isDeleted'];
Expand All @@ -68,9 +64,7 @@ describe('utility function tests:', () => {
' is a part of ignoreAttributes ', (done) => {
const par = { name: `${tu.namePrefix}Delta`, isPublished: false };
Subject.create(par)
.then((o) => {
return o.update({ isPublished: true });
})
.then((o) => o.update({ isPublished: true }))
.then((o) => {
const changedKeys = Object.keys(o._changed);
const ignoreAttributes = changedKeys;
Expand All @@ -86,9 +80,7 @@ describe('utility function tests:', () => {
'in ignoreAttributes', (done) => {
const par = { name: `${tu.namePrefix}Eta`, isPublished: false };
Subject.create(par)
.then((o) => {
return o.update({ isPublished: true });
})
.then((o) => o.update({ isPublished: true }))
.then((o) => {
const changedKeys = Object.keys(o._changed);
const ignoreAttributes = changedKeys;
Expand All @@ -100,7 +92,8 @@ describe('utility function tests:', () => {
.catch((err) => done(err));
});
});
describe('sampleAspectAndSubjectArePublished function:', () => {
describe('test sampleAspectAndSubjectArePublished and' +
' augmentSampleWithSubjectAspectInfo function:', () => {
let sub;
before((done) => {
Aspect.create({
Expand All @@ -120,36 +113,52 @@ describe('utility function tests:', () => {
.catch((err) => done(err));
});

it('create sample; check for true', (done) => {
it('sampleAspectAndSubjectArePublished : check for true', (done) => {
Sample.upsertByName({
name: `${tu.namePrefix}Subject|${tu.namePrefix}Aspect`,
value: '1',
})
.then((samp) => {
return common.sampleAspectAndSubjectArePublished(tu.db.sequelize, samp);
})
.then((samp) =>
common.sampleAspectAndSubjectArePublished(tu.db.sequelize, samp))
.then((pub) => {
expect(pub).to.equal(true);
})
.then(() => done())
.catch((err) => done(err));
});

it('create sample; check for false', (done) => {
it('sampleAspectAndSubjectArePublished : check for false', (done) => {
Subject.findById(sub.id)
.then((s) => s.update({ isPublished: false }))
.then(() => Sample.upsertByName({
name: `${tu.namePrefix}Subject|${tu.namePrefix}Aspect`,
value: '1',
}))
.then((samp) => {
return common.sampleAspectAndSubjectArePublished(tu.db.sequelize, samp);
})
.then((samp) =>
common.sampleAspectAndSubjectArePublished(tu.db.sequelize, samp))
.then((pub) => {
expect(pub).to.equal(false);
})
.then(() => done())
.catch((err) => done(err));
});

it('augmentSampleWithSubjectAspectInfo : returned sample should have' +
' subject and aspect information', (done) => {
Sample.upsertByName({
name: `${tu.namePrefix}Subject|${tu.namePrefix}Aspect`,
value: '1',
})
.then((samp) =>
common.augmentSampleWithSubjectAspectInfo(tu.db.sequelize, samp))
.then((sam) => {
expect(sam.subject.name).to.equal(`${tu.namePrefix}Subject`);
expect(sam.aspect.name).to.equal(`${tu.namePrefix}Aspect`);
expect(sam.subject.tags).to.be.instanceof(Array);
expect(sam.aspect.tags).to.to.be.instanceof(Array);
})
.then(() => done())
.catch((err) => done(err));
});
});
});

0 comments on commit ce9a6b2

Please sign in to comment.