Skip to content

Commit

Permalink
All tests passing for all apis
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusoftnet committed May 11, 2015
1 parent cbd6293 commit 63dffae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
20 changes: 10 additions & 10 deletions apis/address/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ var parse = require("co-body");
var monk = require("monk");
var wrap = require("co-monk");
var db = monk("localhost/addressApi");
var addresses = wrap(db.get("address"));
var addresses = wrap(db.get("addresses"));
module.exports.addresses = addresses;

// routes
app.use(routes.post("/", add));
app.use(routes.get("/:id", getAddress));
app.use(routes.get("/:id", get));
app.use(routes.put("/:id", update));
app.use(routes.del("/:id", remove));

Expand All @@ -20,37 +20,37 @@ app.listen(3000);
console.log("The app is listening. Port 3000");


var add = module.exports.add = function *() {
function *add() {
var postedAddress = yield parse(this);

if(!exists(postedAddress.user)){
this.set('ValidationError', 'User is required');
if(!exists(postedAddress.userId)){
this.set('ValidationError', 'User ID is required');
this.status = 200;
return;
};

var insertedAddress = yield addresses.insert(postedAddress);

this.set("location", "/address/" + insertedAddress._id);
this.set("location", "/" + insertedAddress._id);
this.status = 201;
};

var getAddress = module.exports.getAddress = function *(id) {
function *get(id) {
var address = yield addresses.findById(id);
this.body = address;
this.status = 200;
};

var update = module.exports.update = function *(id) {
function *update(id) {
var postedAddress = yield parse(this);

yield addresses.updateById(id, postedAddress);

this.set("location", "/address/" + id);
this.set("location", "/" + id);
this.status = 204;
};

var remove = module.exports.remove = function *(id) {
function *remove(id) {
yield addresses.remove({_id : id});
this.status = 200;
};
Expand Down
34 changes: 17 additions & 17 deletions apis/address/test/address.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,45 @@ describe('Address API', function(){
var test_address = {};

beforeEach(function (done) {
test_address = helpers.test_addresses;
helpers.removeAll(done);
test_address = helpers.test_address;
helpers.removeAll();
done();
});

afterEach(function (done) {
helpers.removeAll(done);
helpers.removeAll();
done();
});

it('creates a new address', function(done){
// Post
request
.post('/address')
.post('/')
.send(test_address)
.expect('location', /^\/address\/[0-9a-fA-F]{24}$/) // Mongo Object Id /address/234234523562512512
.expect(201)
.end(function () {
co(function *() {
var addressFromDb = yield addresses.findOne({ userId : test_user.userId });
var addressFromDb = yield addresses.findOne({ userId : test_address.userId });
addressFromDb.city.should.equal(test_address.city);
}).then(done, done);
});
});

it('returns validation error if user is not present', function(done){
delete test_address.userId;
var addressToPost = {};

request
.post('/address')
.send(u)
.expect('ValidationError', "User is required")
.post('/')
.send(addressToPost)
.expect('ValidationError', "User ID is required")
.expect(200, done);
});

it('deletes an existing address', function(done){
co(function *() {
var address = yield addresses.insert(test_address);
var addressUrl = '/address/' + address._id;
var addressUrl = '/' + address._id;

request
.del(addressUrl)
Expand All @@ -55,11 +57,9 @@ describe('Address API', function(){

it('returns an existing address', function (done) {
co(function *() {
var address = yield addresses.insert(test_address);
var addressUrl = '/' + address._id;

var address = yield addresses.insert(test_user);
var addressUrl = '/address/' + address._id;

// Get
request
.get(addressUrl)
.set('Accept', 'application/json')
Expand All @@ -74,16 +74,16 @@ describe('Address API', function(){
it('updates an existing address', function(done){
co(function *() {
var address = yield addresses.insert(test_address);
var addressUrl = '/address/' + address._id;
var addressUrl = '/' + address._id;

request
.put(addressUrl)
.send({name: 'Marcus v2', City: 'Bandung Updated'})
.send({name: 'Marcus v2', city: 'Bandung Updated'})
.expect('location', addressUrl)
.expect(204)
.end(function () {
co(function *() {
var addressFromDb = yield users.findOne({ userId : test_user.userId });
var addressFromDb = yield addresses.findById(address._id);
addressFromDb.city.should.equal('Bandung Updated');
}).then(done, done);
});
Expand Down
4 changes: 1 addition & 3 deletions apis/address/test/testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ module.exports.request = require('supertest').agent(app.listen());
var addresses = app.addresses;
module.exports.addresses = app.addresses;

module.exports.removeAll = function(done){
console.log(app.addresses);
module.exports.removeAll = function(){
co(function *(){
yield addresses.remove({});
done();
});
};

Expand Down
2 changes: 1 addition & 1 deletion apis/order/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "index.js",
"scripts": {
"start": "node --harmony index.js",
"test": "./node_modules/mocha/bin/mocha --harmony-generators -u bdd -R spec"
"test": "./node_modules/mocha/bin/mocha --harmony-generators -u bdd -R list"
},
"author": "Marcus Hammarberg",
"license": "BSD-2-Clause",
Expand Down

0 comments on commit 63dffae

Please sign in to comment.