Skip to content

Commit 11d0f75

Browse files
committed
Add more test cases for saving with options
1 parent 9f335e9 commit 11d0f75

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

test/integration/hook.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe("Hook", function() {
354354
});
355355

356356
describe("afterSave", function () {
357-
before(setup());
357+
beforeEach(setup());
358358

359359
it("should trigger after saving an instance", function (done) {
360360
Person.create([{ name: "John Doe" }], function () {
@@ -365,6 +365,19 @@ describe("Hook", function() {
365365
return done();
366366
});
367367
});
368+
369+
it("should not trigger after saving an unchanged instance", function (done) {
370+
Person.create({ name: "Edger" }, function (err, edger) {
371+
should.not.exist(err);
372+
373+
triggeredHooks = {};
374+
edger.save(function (err) {
375+
should.not.exist(err);
376+
should.not.exist(triggeredHooks.afterSave);
377+
done();
378+
});
379+
});
380+
});
368381
});
369382

370383
describe("beforeValidation", function () {

test/integration/model-save.js

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ describe("Model.save()", function() {
88
var Person = null;
99

1010
var setup = function (nameDefinition, opts) {
11+
opts = opts || {};
12+
1113
return function (done) {
1214
Person = db.define("person", {
1315
name : nameDefinition || String
1416
}, opts || {});
1517

16-
Person.hasOne("parent");
18+
Person.hasOne("parent", Person, opts.hasOneOpts);
1719

1820
return helper.dropSync(Person, done);
1921
};
@@ -198,6 +200,119 @@ describe("Model.save()", function() {
198200
});
199201
});
200202

203+
describe("with saveAssociations", function () {
204+
var afterSaveCalled = false;
205+
206+
if (common.protocol() == 'mongodb') return;
207+
208+
beforeEach(function (done) {
209+
function afterSave () {
210+
afterSaveCalled = true;
211+
}
212+
var hooks = { afterSave: afterSave };
213+
214+
setup(null, { hooks: hooks, cache: false, hasOneOpts: { autoFetch: true } })(function (err) {
215+
should.not.exist(err);
216+
217+
Person.create({ name: 'Olga' }, function (err, olga) {
218+
should.not.exist(err);
219+
220+
should.exist(olga);
221+
Person.create({ name: 'Hagar', parent_id: olga.id }, function (err, hagar) {
222+
should.not.exist(err);
223+
should.exist(hagar);
224+
afterSaveCalled = false;
225+
done();
226+
});
227+
});
228+
});
229+
});
230+
231+
it("off should not save associations but save itself", function (done) {
232+
Person.one({ name: 'Hagar' }, function (err, hagar) {
233+
should.not.exist(err);
234+
should.exist(hagar.parent);
235+
236+
hagar.parent.name = 'Olga2';
237+
hagar.save({name: 'Hagar2'}, { saveAssociations: false }, function (err) {
238+
should.not.exist(err);
239+
should.equal(afterSaveCalled, true);
240+
241+
Person.get(hagar.parent.id, function (err, olga) {
242+
should.not.exist(err);
243+
should.equal(olga.name, 'Olga');
244+
done();
245+
});
246+
});
247+
});
248+
});
249+
250+
it("off should not save associations or itself if there are no changes", function (done) {
251+
Person.one({ name: 'Hagar' }, function (err, hagar) {
252+
should.not.exist(err);
253+
254+
hagar.save({}, { saveAssociations: false }, function (err) {
255+
should.not.exist(err);
256+
should.equal(afterSaveCalled, false);
257+
258+
Person.get(hagar.parent.id, function (err, olga) {
259+
should.not.exist(err);
260+
should.equal(olga.name, 'Olga');
261+
done();
262+
});
263+
});
264+
});
265+
});
266+
267+
it("unspecified should save associations and itself", function (done) {
268+
Person.one({ name: 'Hagar' }, function (err, hagar) {
269+
should.not.exist(err);
270+
should.exist(hagar.parent);
271+
272+
hagar.parent.name = 'Olga2';
273+
hagar.save({name: 'Hagar2'}, function (err) {
274+
should.not.exist(err);
275+
276+
Person.get(hagar.parent.id, function (err, olga) {
277+
should.not.exist(err);
278+
should.equal(olga.name, 'Olga2');
279+
280+
Person.get(hagar.id, function (err, person) {
281+
should.not.exist(err);
282+
should.equal(person.name, 'Hagar2');
283+
284+
done();
285+
});
286+
});
287+
});
288+
});
289+
});
290+
291+
it("on should save associations and itself", function (done) {
292+
Person.one({ name: 'Hagar' }, function (err, hagar) {
293+
should.not.exist(err);
294+
should.exist(hagar.parent);
295+
296+
hagar.parent.name = 'Olga2';
297+
hagar.save({name: 'Hagar2'}, { saveAssociations: true }, function (err) {
298+
should.not.exist(err);
299+
300+
Person.get(hagar.parent.id, function (err, olga) {
301+
should.not.exist(err);
302+
should.equal(olga.name, 'Olga2');
303+
304+
Person.get(hagar.id, function (err, person) {
305+
should.not.exist(err);
306+
should.equal(person.name, 'Hagar2');
307+
308+
done();
309+
});
310+
});
311+
});
312+
});
313+
});
314+
});
315+
201316
describe("with a point property", function () {
202317
if (common.protocol() == 'sqlite' || common.protocol() == 'mongodb') return;
203318

0 commit comments

Comments
 (0)