Skip to content

Commit

Permalink
Merge pull request #21 from jedp/master
Browse files Browse the repository at this point in the history
Added tests for db.updateInstead
  • Loading branch information
jedp committed Dec 15, 2012
2 parents 19a4695 + 1f04eb7 commit a24c07f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
11 changes: 6 additions & 5 deletions server/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,15 @@ exports.updateProfile = function (email, name, cb) {
conn.query('INSERT INTO members (email, name) ' +
'VALUES (?, ?)', [email, name],
function (err, res) {
finCb();
if (err) {
if (/Duplicate entry .*? for key 'PRIMARY'/.test(err.message)) {
return updateInstead(email, name, cb);
}
console.error(err);
finCb();
return updateInstead(email, name, cb);
} else {
finCb();
return cb(null);
return cb(err);
}
return cb(null);
});
});
};
Expand Down
31 changes: 28 additions & 3 deletions tests/db_vows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ fs = require('fs'),
path = require('path'),
child_process = require('child_process'),
TEST_EMAIL = "oxfordcommagirl@roomr.gov",
TEST_NAME = "OCG",
TEST_FIRST_NAME = "OCG",
TEST_SECOND_NAME = "Juanita",
TEST_ROOM = "Our Terrible Ideas",
TEST_OTHER_EMAIL = "biggles@spanishinquisition.org",
TEST_OTHER_NAME = "Cardinal Biggles";
Expand Down Expand Up @@ -96,7 +97,7 @@ suite.addBatch({
suite.addBatch({
"We can create": {
topic: function() {
db.updateProfile(TEST_EMAIL, TEST_NAME, this.callback);
db.updateProfile(TEST_EMAIL, TEST_FIRST_NAME, this.callback);
},

"a new user": function(err, other) {
Expand All @@ -111,7 +112,31 @@ suite.addBatch({
"her profile": function(err, profile) {
assert(err === null);
assert(profile.email === TEST_EMAIL);
assert(profile.name === TEST_NAME);
assert(profile.name === TEST_FIRST_NAME);
}
}
}
});

suite.addBatch({
"Our user can update": {
topic: function() {
db.updateProfile(TEST_EMAIL, TEST_SECOND_NAME, this.callback);
},

"her profile": function(err) {
assert(!err);
},

"which we see": {
topic: function() {
db.getProfile(TEST_EMAIL, this.callback);
},

"when we retrive it": function(err, profile) {
assert(err === null);
assert(profile.email === TEST_EMAIL);
assert(profile.name === TEST_SECOND_NAME);
}
}
}
Expand Down

0 comments on commit a24c07f

Please sign in to comment.