Browse files

added findByIdTest

  • Loading branch information...
1 parent 0501e31 commit cdc3f0dac2d6742a67bd8ddc8a7d9a3846dde732 @yesobo committed Nov 27, 2012
Showing with 96 additions and 26 deletions.
  1. +24 −8 daos/dao.coffee
  2. +31 −13 daos/dao.js
  3. +17 −3 daos/dao_test.coffee
  4. +24 −2 daos/dao_test.js
View
32 daos/dao.coffee
@@ -16,18 +16,27 @@ module.exports = class MongoDBConnector
#call: callback parameters are (err, collection)
initTransaction = (callback) ->
if @db._state == 'connected'
+ console.log "Conection opened"
@db.collection @dbName, callback
else
+ console.log "opening connection..."
privateDb = @db
privateDBName = @dbName
@db.open (err, p_client) ->
- privateDb.authenticate 'admin', '1234', (err) ->
- privateDb.collection privateDBName, callback
+ if err?
+ console.log "ERROR opening connection: #{err}"
+ else
+ console.log "connection opened"
+ console.log "authenticating..."
+ privateDb.authenticate 'admin', '1234', (err) ->
+ console.log "autenticated!"
+ privateDb.collection privateDBName, callback
#call: callback parameters are (err, items)
findAll: (callback) ->
initTransaction.call this, (err, collection) ->
if err?
+ console.log "ERROR!"
callback err, null
else
collection.find().toArray (err, items) ->
@@ -37,22 +46,29 @@ module.exports = class MongoDBConnector
count: (callback) ->
initTransaction.call this, (err, collection) ->
if err?
+ console.log "ERROR"
callback err, null
else
collection.count (err, count) ->
callback err, count
#call: callback parameters are (err, item)
findById: (pId, callback) ->
- initTransaction (err, collection) ->
- collection.findOne id:pId, (err, item) ->
- callback err, item
+ initTransaction.call this, (err, collection) ->
+ if err?
+ console.log "ERROR!"
+ callback err, null
+ else
+ collection.findOne id:pId, (err, item) ->
+ callback err, item
#call: callback parameters are (err, doc)
insert: (pattern, callback) ->
- initTransaction (err, collection) ->
- collection.count (err, count) ->
- pattern.id = count + 1
+ initTransaction.call this, (err, collection) ->
+ if err?
+ console.log "ERROR!"
+ callback err, null
+ else
collection.insert pattern, (err, doc) ->
callback err, doc
View
44 daos/dao.js
@@ -26,21 +26,31 @@
initTransaction = function(callback) {
var privateDBName, privateDb;
if (this.db._state === 'connected') {
+ console.log("Conection opened");
return this.db.collection(this.dbName, callback);
} else {
+ console.log("opening connection...");
privateDb = this.db;
privateDBName = this.dbName;
return this.db.open(function(err, p_client) {
- return privateDb.authenticate('admin', '1234', function(err) {
- return privateDb.collection(privateDBName, callback);
- });
+ if (err != null) {
+ return console.log("ERROR opening connection: " + err);
+ } else {
+ console.log("connection opened");
+ console.log("authenticating...");
+ return privateDb.authenticate('admin', '1234', function(err) {
+ console.log("autenticated!");
+ return privateDb.collection(privateDBName, callback);
+ });
+ }
});
}
};
MongoDBConnector.prototype.findAll = function(callback) {
return initTransaction.call(this, function(err, collection) {
if (err != null) {
+ console.log("ERROR!");
return callback(err, null);
} else {
return collection.find().toArray(function(err, items) {
@@ -53,6 +63,7 @@
MongoDBConnector.prototype.count = function(callback) {
return initTransaction.call(this, function(err, collection) {
if (err != null) {
+ console.log("ERROR");
return callback(err, null);
} else {
return collection.count(function(err, count) {
@@ -63,23 +74,30 @@
};
MongoDBConnector.prototype.findById = function(pId, callback) {
- return initTransaction(function(err, collection) {
- return collection.findOne({
- id: pId
- }, function(err, item) {
- return callback(err, item);
- });
+ return initTransaction.call(this, function(err, collection) {
+ if (err != null) {
+ console.log("ERROR!");
+ return callback(err, null);
+ } else {
+ return collection.findOne({
+ id: pId
+ }, function(err, item) {
+ return callback(err, item);
+ });
+ }
});
};
MongoDBConnector.prototype.insert = function(pattern, callback) {
- return initTransaction(function(err, collection) {
- return collection.count(function(err, count) {
- pattern.id = count + 1;
+ return initTransaction.call(this, function(err, collection) {
+ if (err != null) {
+ console.log("ERROR!");
+ return callback(err, null);
+ } else {
return collection.insert(pattern, function(err, doc) {
return callback(err, doc);
});
- });
+ }
});
};
View
20 daos/dao_test.coffee
@@ -7,6 +7,15 @@ should = require "#{modules_url}/should"
describe 'Tests for MongoDBConnector', ->
daoObj = null
+ new_pattern = {
+ "id": 3,
+ "name": "Factory Method",
+ "category": "Creational",
+ "intent": "Define an interface for creating an object, but let subclasses decide which class to instantiate. Lets a class defer instantiation to subclasses",
+ "motivation": "",
+ "applicability": "",
+ "structure": ""
+ };
beforeEach ->
daoObj = new MongoDBConnector 'design_patterns', 'alex.mongohq.com', 10001
it 'Can be instatiated with paraneters', (done) ->
@@ -26,7 +35,12 @@ describe 'Tests for MongoDBConnector', ->
daoObj.count (err, count) ->
should.strictEqual count, 2
done()
- it "findById with id = 2 returns the Prototype pattern"
- daoObj.findById (err, item) ->
- item.name.should.be.equals "Prototype"
+ it "findById with id = 2 returns the Prototype pattern", (done) ->
+ daoObj.findById 2, (err, item) ->
+ item.should.have.property 'name', 'Prototype'
+ done()
+ it.only "insert new pattern (Factory Method) must the pattern", (done) ->
+ daoObj.insert new_pattern, (err, item) ->
+ doc = item[0]
+ doc.should.have.property 'name', 'Factory Method'
done()
View
26 daos/dao_test.js
@@ -11,8 +11,17 @@
should = require("" + modules_url + "/should");
describe('Tests for MongoDBConnector', function() {
- var daoObj;
+ var daoObj, new_pattern;
daoObj = null;
+ new_pattern = {
+ "id": 3,
+ "name": "Factory Method",
+ "category": "Creational",
+ "intent": "Define an interface for creating an object, but let subclasses decide which class to instantiate. Lets a class defer instantiation to subclasses",
+ "motivation": "",
+ "applicability": "",
+ "structure": ""
+ };
beforeEach(function() {
return daoObj = new MongoDBConnector('design_patterns', 'alex.mongohq.com', 10001);
});
@@ -38,7 +47,20 @@
return done();
});
});
- return it("findById with id = 2 returns the Prototype pattern");
+ it("findById with id = 2 returns the Prototype pattern", function(done) {
+ return daoObj.findById(2, function(err, item) {
+ item.should.have.property('name', 'Prototype');
+ return done();
+ });
+ });
+ return it.only("insert new pattern (Factory Method) must the pattern", function(done) {
+ return daoObj.insert(new_pattern, function(err, item) {
+ var doc;
+ doc = item[0];
+ doc.should.have.property('name', 'Factory Method');
+ return done();
+ });
+ });
});
}).call(this);

0 comments on commit cdc3f0d

Please sign in to comment.