Skip to content

Commit

Permalink
adding updateModelCustomQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
filnik committed Jan 20, 2014
1 parent 9f679f4 commit 4fed961
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/directAPI.js
Expand Up @@ -146,6 +146,25 @@ BaseAPI.prototype.updateModel = function (req, res, id) {
this.updateModelCustom(res, req.body, id);
};

/**
* as before but query instead of the default one
* @param req
* @param res
* @param query
*/
BaseAPI.prototype.updateModelCustomQuery = function (res, body, query) {
if (body === undefined) {
return this.returnError(res, 'missing updated ' + this.APIName);
}

var that = this;
this.Model.update(query, body, function (err, data) {
if (res) {
return that._checkStatus(err, res, data);
}
});
};

/**
* returns a (generic) error to the user
* @param res
Expand Down
2 changes: 1 addition & 1 deletion test/common.js
Expand Up @@ -6,4 +6,4 @@ global.expect = require("chai").expect;

global.chai.use(require("sinon-chai"));

global.createRes = function (callback) {return {status: function () {return this; }, json: callback };};
global.createRes = function (callback) {return {status: function () {return this; }, json: callback }; };
18 changes: 18 additions & 0 deletions test/directAPI_spec.js
Expand Up @@ -141,6 +141,23 @@ describe("base_api", function () {
}, timestamp);
});

it("should update an element correctly with query", function (done) {
var timestamp = new Date(),
res = createRes(function (numRowAffected) {
expect(numRowAffected).to.eql(1);
var res = createRes(function(data) {
expect(data[0]['name']).to.eql('changed');
});
base.getFromModel({session: {}}, res);
done();
});
createNewElement(function(data){
// use updateModel instead of updateModelCustom to cover more lines in the test
base.updateModelCustomQuery(res,
{ name: "changed" }, {_id: data._id.toString()});
}, timestamp);
});

it("should not update an element correctly", function (done) {
var res = createRes(function (data) {
expect(data.error).to.eql('missing updated model');
Expand All @@ -149,6 +166,7 @@ describe("base_api", function () {
base.updateModel({ body: undefined }, res, 'id');
});


it("should handle correctly an error with checkstatus", function (done) {
base._checkStatus('commonErr', createRes(function (data){
expect(data.error).to.eql('commonErr');
Expand Down

0 comments on commit 4fed961

Please sign in to comment.