Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
iamigo committed Apr 18, 2017
1 parent 80de52c commit cad1970
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 5 deletions.
15 changes: 10 additions & 5 deletions db/model/subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ module.exports = function subject(seq, dataTypes) {
.catch((err) => {
throw err;
});

}

const changedKeys = Object.keys(inst._changed);
Expand Down Expand Up @@ -426,14 +425,18 @@ module.exports = function subject(seq, dataTypes) {
ignoreAttributes);
}
} else {

// Treat publishing a subject as an "add" event.
common.publishChange(inst, eventName.add);
}
} else if (inst.previous('isPublished')) {

// Treat unpublishing a subject as a "delete" event.
common.publishChange(inst, eventName.del);
return new seq.Promise((resolve, reject) =>
inst.getSamples()
.each((samp) => samp.destroy())
.then(() => resolve(inst))
.catch((err) => reject(err))
);
}
}, // hooks.afterupdate

Expand All @@ -450,7 +453,6 @@ module.exports = function subject(seq, dataTypes) {
common.publishChange(inst, eventName.del);

if (featureToggles.isFeatureEnabled(sampleStoreFeature)) {

// delete the entry in the subject store
redisOps.deleteKey(subjectType, inst.absolutePath);

Expand All @@ -472,7 +474,8 @@ module.exports = function subject(seq, dataTypes) {
}, // hooks.afterDelete

/**
* Prevents from deleting a subject which has children.
* Prevents from deleting a subject which has children. Delete the
* subject's samples.
*
* @param {Subject} inst - The instance being destroyed
* @returns {Promise} which resolves to undefined or rejects if an error
Expand All @@ -490,6 +493,8 @@ module.exports = function subject(seq, dataTypes) {
return common.setIsDeleted(seq.Promise, inst);
}
})
.then(() => inst.getSamples())
.each((samp) => samp.destroy())
.then(() => resolve())
.catch((err) => reject(err))
);
Expand Down
53 changes: 53 additions & 0 deletions tests/db/model/subject/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const expect = require('chai').expect;
const tu = require('../../../testUtils');
const u = require('./utils');
const Subject = tu.db.Subject;
const Aspect = tu.db.Aspect;
const Sample = tu.db.Sample;

describe('db: subject: delete: ', () => {
describe('no children: ', () => {
Expand Down Expand Up @@ -336,4 +338,55 @@ describe('db: subject: delete: ', () => {
.catch(done);
});
});

describe('cascade delete samples: ', () => {
let sample;
let subject;

const aspectToCreate = {
isPublished: true,
name: `${tu.namePrefix}Aspect`,
timeout: '30s',
valueType: 'NUMERIC',
};

const subjectToCreate = {
isPublished: true,
name: `${tu.namePrefix}Subject`,
};

before((done) => {
let aId;
Aspect.create(aspectToCreate)
.then((asp) => {
aId = asp.id;
return Subject.create(subjectToCreate);
})
.then((subj) => {
subject = subj;
return Sample.create({
aspectId: aId,
subjectId: subject.id,
});
})
.then((samp) => {
sample = samp;
done();
})
.catch(done);
});

after(u.forceDelete);

it('samples deleted when subject is deleted', (done) => {
Subject.findById(subject.id)
.then((subj) => subj.destroy())
.then(() => Sample.findById(sample.id))
.then((samp) => {
expect(samp).to.equal(null);
done();
})
.catch(done);
});
});
});
70 changes: 70 additions & 0 deletions tests/db/model/subject/unpublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

/**
* tests/db/model/subject/unpublish.js
*/
'use strict';

const expect = require('chai').expect;
const tu = require('../../../testUtils');
const u = require('./utils');
const Subject = tu.db.Subject;
const Aspect = tu.db.Aspect;
const Sample = tu.db.Sample;

describe('db: subject: unpublish: ', () => {
let sample;
let subject;

const aspectToCreate = {
isPublished: true,
name: `${tu.namePrefix}Aspect`,
timeout: '30s',
valueType: 'NUMERIC',
};

const subjectToCreate = {
isPublished: true,
name: `${tu.namePrefix}Subject`,
};

before((done) => {
let aId;
Aspect.create(aspectToCreate)
.then((asp) => {
aId = asp.id;
return Subject.create(subjectToCreate);
})
.then((subj) => {
subject = subj;
return Sample.create({
aspectId: aId,
subjectId: subject.id,
});
})
.then((samp) => {
sample = samp;
done();
})
.catch(done);
});

after(u.forceDelete);

it('samples deleted', (done) => {
Subject.findById(subject.id)
.then((subj) => subj.update({ isPublished: false }))
.then(() => Sample.findById(sample.id))
.then((samp) => {
expect(samp).to.equal(null);
done();
})
.catch(done);
});
});

0 comments on commit cad1970

Please sign in to comment.