From 39bfa0352ce5bfb27eca0779c745f2da1c5da581 Mon Sep 17 00:00:00 2001 From: lichengyin Date: Mon, 7 Dec 2015 10:34:32 +0800 Subject: [PATCH] fix test error --- src/model/base.js | 12 +++++++----- test/model/base.js | 18 +++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/model/base.js b/src/model/base.js index 8798985d..537df01f 100644 --- a/src/model/base.js +++ b/src/model/base.js @@ -280,12 +280,14 @@ export default class extends Base { this.readonlyFields.forEach(item => { delete data[item]; }); - //check data is empty - if (think.isEmpty(data)) { - return think.reject(new Error(think.locale('DATA_EMPTY'))); - } let parsedData = this.parseData(data); + //check data is empty + if (think.isEmpty(parsedData)) { + return think.reject(new Error(think.locale('DATA_EMPTY'))); + } + let copyData = think.extend({}, parsedData); + //check where condition if(think.isEmpty(options.where)){ //get where condition from data @@ -300,7 +302,7 @@ export default class extends Base { parsedData = await this.beforeUpdate(parsedData, options); let rows = await this.db().update(parsedData, options); - await this.afterUpdate(data, options); + await this.afterUpdate(copyData, options); return rows; } /** diff --git a/test/model/base.js b/test/model/base.js index 1f46956a..262acfc9 100644 --- a/test/model/base.js +++ b/test/model/base.js @@ -127,7 +127,7 @@ describe('model/base.js', function(){ return Promise.resolve({ affectedRows: 3 }) - }else if(sql.trim() === "UPDATE `think_user` SET `title`='title2' WHERE ( `id` = 106 )"){ + }else if(sql.trim() === "UPDATE `think_cate` SET `title`='title2' WHERE ( `id` = 106 )"){ return Promise.resolve({ affectedRows: 4 }) @@ -499,16 +499,18 @@ describe('model/base.js', function(){ }) }) it('update, where condition from data', function(done){ + var instance = new Base('cate', think.config('db')); instance.update({ id: 102, name: 'name1', title: 'title1' }).then(function(rows){ var sql = instance.getLastSql(); - //console.log(sql, rows) - assert.equal(sql, "UPDATE `think_user` SET `title`='title1' WHERE ( `id` = 102 )") - assert.equal(rows, 3); + assert.equal(sql, "UPDATE `think_cate` SET `title`='title1' WHERE ( `id` = 102 )") + assert.equal(rows, 0); done(); + }).catch(function(err){ + console.log(err.stack) }) }) it('update many, empty', function(done){ @@ -517,18 +519,20 @@ describe('model/base.js', function(){ }) }) it('update many', function(done){ + var instance = new Base('cate', think.config('db')); instance.updateMany([{ id: 105, name: 'name1', title: 'title1' }]).then(function(rows){ var sql = instance.getLastSql(); - assert.equal(sql, "UPDATE `think_user` SET `title`='title1' WHERE ( `id` = 105 )") - assert.equal(rows, 1); + assert.equal(sql, "UPDATE `think_cate` SET `title`='title1' WHERE ( `id` = 105 )") + assert.equal(rows, 0); done(); }) }) it('update many 2', function(done){ + var instance = new Base('cate', think.config('db')); instance.updateMany([{ id: 100, name: 'name1', @@ -539,7 +543,7 @@ describe('model/base.js', function(){ title: 'title2' }]).then(function(rows){ var sql = instance.getLastSql(); - assert.equal(sql, "UPDATE `think_user` SET `title`='title2' WHERE ( `id` = 106 )") + assert.equal(sql, "UPDATE `think_cate` SET `title`='title2' WHERE ( `id` = 106 )") assert.equal(rows, 4); done(); })