Skip to content
Browse files

improving tests

  • Loading branch information...
1 parent e49ad42 commit d67054dc26dd2f221af2f353b5033a9f0498969d @yesobo committed
Showing with 26 additions and 64 deletions.
  1. +1 −6 daos/mongoHQDao.coffee
  2. +2 −7 daos/mongoHQDao.js
  3. +1 −0 routes.coffee
  4. +1 −0 routes.js
  5. +21 −51 test/mocha_api_test.js
View
7 daos/mongoHQDao.coffee
@@ -55,15 +55,12 @@ module.exports = class MongoDBConnector
#call: callback parameters are (err, item)
findById: (pId, callback) ->
- console.log "init: findById"
initTransaction.call this, (err, collection) ->
if err?
console.log "ERROR!"
callback err, null
else
- console.log "id = #{pId}"
collection.findOne id:pId, (err, item) ->
- console.log "findOne callback: error: #{err}, item: #{item}"
callback err, item
#call: callback parameters are (err, doc)
@@ -73,7 +70,6 @@ module.exports = class MongoDBConnector
console.log "ERROR!"
callback err, null
else
- console.log "inserting pattern: #{util.inspect(pattern)}"
collection.insert pattern, (err, doc) ->
callback err, doc
@@ -97,11 +93,10 @@ module.exports = class MongoDBConnector
#call: callback parameters are (err)
delete: (pId, callback) ->
- console.log "INIT: daoObj delete"
initTransaction.call this, (err, collection) ->
if err?
console.log "ERROR!"
+ callback err
else
- console.log "removing element with id = #{pId}"
collection.remove id:pId, (err) ->
callback err
View
9 daos/mongoHQDao.js
@@ -76,17 +76,14 @@
};
MongoDBConnector.prototype.findById = function(pId, callback) {
- console.log("init: findById");
return initTransaction.call(this, function(err, collection) {
if (err != null) {
console.log("ERROR!");
return callback(err, null);
} else {
- console.log("id = " + pId);
return collection.findOne({
id: pId
}, function(err, item) {
- console.log("findOne callback: error: " + err + ", item: " + item);
return callback(err, item);
});
}
@@ -99,7 +96,6 @@
console.log("ERROR!");
return callback(err, null);
} else {
- console.log("inserting pattern: " + (util.inspect(pattern)));
return collection.insert(pattern, function(err, doc) {
return callback(err, doc);
});
@@ -132,12 +128,11 @@
};
MongoDBConnector.prototype["delete"] = function(pId, callback) {
- console.log("INIT: daoObj delete");
return initTransaction.call(this, function(err, collection) {
if (err != null) {
- return console.log("ERROR!");
+ console.log("ERROR!");
+ return callback(err);
} else {
- console.log("removing element with id = " + pId);
return collection.remove({
id: pId
}, function(err) {
View
1 routes.coffee
@@ -42,6 +42,7 @@ app.post '/api/patterns', (req, res) ->
app.put '/api/patterns/:id', (req, res) ->
intId = parseInt req.params.id
updated_pattern =
+ id: intId
name: req.body.name
category: req.body.category
intent: req.body.intent
View
1 routes.js
@@ -57,6 +57,7 @@
var intId, updated_pattern;
intId = parseInt(req.params.id);
updated_pattern = {
+ id: intId,
name: req.body.name,
category: req.body.category,
intent: req.body.intent,
View
72 test/mocha_api_test.js
@@ -14,8 +14,8 @@ var test_pattern1 = {
"structure": "Cambiar por BLOB"
};
-var test_pattern1_modif = {
- "id": 1,
+var test_pattern3_modif = {
+ "id": 3,
"name": "MODIF Singleton",
"category": "MODIF Creational",
"intent": "MODIF Ensure a class only has one instance, and provide a global point of acg cess to it",
@@ -47,13 +47,11 @@ var test_pattern2 = {
describe('Tests for patterns API, ', function() {
var testIfExists = function(test_pattern, cb) {
- console.log("looking for pattern.id = " + test_pattern.id);
request.get(url + '/api/patterns/' + test_pattern.id, function (err, res, body) {
if(err) {
done(err);
}
else {
- console.log(res.statusCode);
res.statusCode.should.be.equal(200);
should.exist(body);
var pattern = JSON.parse(body);
@@ -90,7 +88,7 @@ describe('Tests for patterns API, ', function() {
};
describe('Get all patterns test', function(){
- it('should be successful.', function(done){
+ it('should return statusCode 200 and a json object with the 2 test elements', function(done){
request.get(url + '/api/patterns', function (err, res, body) {
if(err) {
done(err);
@@ -102,22 +100,6 @@ describe('Tests for patterns API, ', function() {
var patterns = JSON.parse(body);
patterns.should.be.an.instanceOf(Array);
patterns.should.have.length(2);
-
- var aux_pattern;
- for (var i = 0; i < patterns.length; i++) {
- if (i === 0) {
- aux_pattern = test_pattern1;
- } else {
- aux_pattern = test_pattern2;
- }
- patterns[i].id = aux_pattern.id;
- patterns[i].name = aux_pattern.name;
- patterns[i].category = aux_pattern.category;
- patterns[i].intent = aux_pattern.intent;
- patterns[i].motivation = aux_pattern.motivation;
- patterns[i].applicability = aux_pattern.applicability;
- patterns[i].structure = aux_pattern.structure;
- }
done();
}
});
@@ -125,7 +107,7 @@ describe('Tests for patterns API, ', function() {
});
describe('Get number of patterns test', function(){
- it('should be successful.', function(done){
+ it('should return statusCode 200 and "2".', function(done){
request.get(url + '/api/patterns/count', function (err, res, body) {
if(err) {
done(err);
@@ -143,7 +125,7 @@ describe('Tests for patterns API, ', function() {
});
describe('Get pattern with id = 1 test', function() {
- it('should be succesful', function(done){
+ it('should return the test_pattern1 element', function(done){
testIfExists(test_pattern1, function() {
done();
});
@@ -151,7 +133,7 @@ describe('Tests for patterns API, ', function() {
});
describe('Insert new pattern with id = 3', function() {
- it('should be succesful', function(done){
+ it('should return statusCode 200 and our new_pattern object must be in our collection', function(done){
var post_options = {
method: 'POST',
uri: url + '/api/patterns',
@@ -163,31 +145,21 @@ describe('Tests for patterns API, ', function() {
};
var post_callback = function(error, res, body) {
res.statusCode.should.be.equal(200);
- /*
// Check if new_pattern is in the db
- // ¡IT DOES'NT WORK! maybe due to 2 consecutive requests
testIfExists(new_pattern, function() {
- console.log("deleting pattern");
- deletePatternById(new_pattern.id, function() {
- console.log("testing absense");
- testIfNotExists(new_pattern, function() {
- done();
- });
- });
+ done();
});
- */
- done();
};
request(post_options, post_callback);
});
});
- describe('Update pattern with id = 1 test', function() {
- it('should be succesful', function(done){
+ describe('Update pattern with id = 3 test', function() {
+ it('should return status code 200 and must have changed the element at our collection', function(done){
var post_options = {
method: 'PUT',
- uri: url + '/api/patterns/1',
- form: test_pattern1,
+ uri: url + '/api/patterns/3',
+ form: test_pattern3_modif,
port: 8010,
headers: {
"Content-Type": "application/json"
@@ -195,24 +167,22 @@ describe('Tests for patterns API, ', function() {
};
var put_callback = function(error, res, body) {
res.statusCode.should.be.equal(200);
- done();
+ testIfExists(test_pattern3_modif, function() {
+ done();
+ });
};
request(post_options, put_callback);
});
});
+
describe('Delete pattern with id = 3 test', function() {
- it('should be succesful', function(done) {
- var post_options = {
- method: 'DELETE',
- uri: url + '/api/patterns/3',
- port: 8010
- };
- var del_callback = function(error, res, body) {
- res.statusCode.should.be.equal(200);
- done();
- };
- request.del(post_options, del_callback);
+ it('should return statusCode 200 and it must not be int our collection', function(done) {
+ deletePatternById(new_pattern.id, function() {
+ testIfNotExists(new_pattern, function() {
+ done();
+ });
+ });
});
});
});

0 comments on commit d67054d

Please sign in to comment.
Something went wrong with that request. Please try again.