Skip to content

Commit

Permalink
complete test
Browse files Browse the repository at this point in the history
  • Loading branch information
pktangyue committed Jul 25, 2015
1 parent 5eadb3b commit c95f30c
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
coverage/
7 changes: 7 additions & 0 deletions MongoProfilerAPI/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test:
./node_modules/.bin/mocha

cov test-cov:
./node_modules/.bin/istanbul cover _mocha

.PHONY: test cov test-cov
4 changes: 3 additions & 1 deletion MongoProfilerAPI/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
var express = require('express');
var app = express();
var router = require('./router.js').router;
var router = require('./router.js');

app.use('/api/avgtime/', router);

module.exports = app;

app.listen(3000, function () {
console.log('app is listening at port 3000');
});
18 changes: 9 additions & 9 deletions MongoProfilerAPI/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ var getPerDayAvgTime = function(year, date, recent, callback){
start_date = moment.utc().year(parseInt(year)).startOf('year');
end_date = start_date.clone().endOf('year');
}
console.log('start_date ' + start_date.toISOString());
console.log('end_date ' + end_date.toISOString());
// console.log('start_date ' + start_date.toISOString());
// console.log('end_date ' + end_date.toISOString());
ret.push({ $match : {
ts : {
$gte : start_date.toDate(),
Expand All @@ -38,7 +38,7 @@ var getPerDayAvgTime = function(year, date, recent, callback){
} });
ret.push( { $sort: {_id: -1}} );
if ( recent && recent > 0 ) {
console.log('recent : ' + recent);
// console.log('recent : ' + recent);
ret.push( { $limit: recent } );
}
ret.push( {
Expand Down Expand Up @@ -79,8 +79,8 @@ var getPerWeekAvgTime = function(year, date, week, recent, callback){
start_date = moment.utc().year(parseInt(year)).startOf('year');
end_date = start_date.clone().endOf('year');
}
console.log('start_date ' + start_date.toISOString());
console.log('end_date ' + end_date.toISOString());
// console.log('start_date ' + start_date.toISOString());
// console.log('end_date ' + end_date.toISOString());

ret.push({ $match : {
ts : {
Expand All @@ -94,7 +94,7 @@ var getPerWeekAvgTime = function(year, date, week, recent, callback){
} });
ret.push( { $sort: {_id: -1}} );
if ( recent && recent > 0 ) {
console.log('recent : ' + recent);
// console.log('recent : ' + recent);
ret.push( { $limit: recent } );
}
ret.push( {
Expand Down Expand Up @@ -131,8 +131,8 @@ var getPerMonthAvgTime = function(year, month, recent, callback){
start_date = moment.utc().year(parseInt(year)).startOf('year');
end_date = start_date.clone().endOf('year');
}
console.log('start_date ' + start_date.toISOString());
console.log('end_date ' + end_date.toISOString());
// console.log('start_date ' + start_date.toISOString());
// console.log('end_date ' + end_date.toISOString());

ret.push({ $match : {
ts : {
Expand All @@ -146,7 +146,7 @@ var getPerMonthAvgTime = function(year, month, recent, callback){
} });
ret.push( { $sort: {_id: -1}} );
if ( recent && recent > 0 ) {
console.log('recent : ' + recent);
// console.log('recent : ' + recent);
ret.push( { $limit: recent } );
}
ret.push( {
Expand Down
8 changes: 7 additions & 1 deletion MongoProfilerAPI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
"moment": "^2.10.3",
"mongodb": "^2.0.39"
},
"devDependencies": {}
"devDependencies": {
"async": "^1.4.0",
"istanbul": "^0.3.17",
"mocha": "^2.2.5",
"should": "^7.0.2",
"supertest": "^1.0.1"
}
}
30 changes: 13 additions & 17 deletions MongoProfilerAPI/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ var isPositiveNumber = function( val ) {
};

router.param('year', function ( req, res, next, year){
console.log('------------');
// console.log('------------');
if ( year && isPositiveNumber ( year )) {
req.year = year;
console.log('year : ' + req.year);
// console.log('year : ' + req.year);
next();
}
else {
Expand All @@ -21,75 +21,71 @@ router.param('year', function ( req, res, next, year){
}
});

router.get('/:year/*', function ( req, res, next ) {
next();
});

router.get('/:year/perday', function ( req, res) {
var date = moment.utc(req.year + req.query.date, DATE_VALID_FORMAT, true)
console.log('date : ' + req.query.date);
// console.log('date : ' + req.query.date);
if ( req.query.date && ! date.isValid()){
res.status(500).send('date must be a valid date');
return;
}

var recent = req.query.recent;
console.log('recent : ' + recent);
// console.log('recent : ' + recent);
if ( recent && ! isPositiveNumber(recent) ) {
res.status(500).send('recent must be a positive integer');
return;
}

model.getPerDayAvgTime(req.year, date, recent, function(result) {
model.getPerDayAvgTime(req.year, date, parseInt(recent), function(result) {
res.send(result);
});
});

router.get('/:year/perweek', function ( req, res) {
var week = req.query.week;
console.log('week : ' + week );
// console.log('week : ' + week );
if ( week && ( ! isPositiveNumber( week ) || parseInt( week ) > 53) ) {
res.status(500).send('week must be a valid week');
return;
}

var date = moment.utc(req.year + req.query.date, DATE_VALID_FORMAT, true)
console.log('date : ' + req.query.date);
// console.log('date : ' + req.query.date);
if ( req.query.date && ! date.isValid()){
res.status(500).send('date must be a valid date');
return;
}

var recent = req.query.recent;
console.log('recent : ' + recent);
// console.log('recent : ' + recent);
if ( recent && ! isPositiveNumber(recent) ) {
res.status(500).send('recent must be a positive integer');
return;
}

model.getPerWeekAvgTime(req.year, date, week, recent, function(result) {
model.getPerWeekAvgTime(req.year, date, week, parseInt(recent), function(result) {
res.send(result);
});
});

router.get('/:year/permonth', function ( req, res) {
var month = req.query.month
console.log('month : ' + month );
// console.log('month : ' + month );
if ( month && ( ! isPositiveNumber( month ) || parseInt( month ) > 12) ) {
res.status(500).send('month must be a valid month');
return;
}

var recent = req.query.recent;
console.log('recent : ' + recent);
// console.log('recent : ' + recent);
if ( recent && ! isPositiveNumber(recent) ) {
res.status(500).send('recent must be a positive integer');
return;
}

model.getPerMonthAvgTime(req.year, month, recent, function(result) {
model.getPerMonthAvgTime(req.year, month, parseInt(recent), function(result) {
res.send(result);
});
});

exports.router = router;
module.exports = router;
198 changes: 198 additions & 0 deletions MongoProfilerAPI/test/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
var app = require('../app.js');
var supertest = require('supertest');
var request = supertest(app);
var should = require('should');
var async = require('async');

describe('test/app.test.js', function () {
it('should return a 404 status', function(done) {
request.get('/api/avgtime/2015/per')
.expect(404)
.end(function (err, res){
done();
});
});

it('should return a Array like [ { "avgtime" : 1, "date" : "2015-07-26" } ]', function(done) {
var urls = [
'/api/avgtime/2015/perday',
'/api/avgtime/2015/perday?date=0723',
'/api/avgtime/2015/perday?recent=2'
];
var test = function (value, callback) {
request.get(value)
.expect(200)
.end(function ( err, res ) {

should.not.exist(err);

JSON.parse(res.text).should.not.throw();
var result = JSON.parse(res.text);

result.should.be.an.Array();
if ( result.length > 0 ) {
result.forEach(function(elem) {
elem.should.match( {
'date' : /^\d{4}-\d{2}-\d{2}$/,
'avgtime' : function (it) {
it.should.be.an.Number();
}
});
});
}
callback();
});
};

async.each(urls, test, function(err) {
should.not.exist(err);
done();
});
});

it('should return a Array like [ { "avgtime" : 1, "week" : 30 } ]', function(done) {
var urls = [
'/api/avgtime/2015/perweek',
'/api/avgtime/2015/perweek?week=30',
'/api/avgtime/2015/perweek?date=0723',
'/api/avgtime/2015/perweek?recent=1'
];
var test = function (value, callback) {
request.get(value)
.expect(200)
.end(function ( err, res ) {

should.not.exist(err);

JSON.parse(res.text).should.not.throw();
var result = JSON.parse(res.text);

result.should.be.an.Array();
if ( result.length > 0 ) {
result.forEach(function(elem) {
elem.should.match( {
'week' : function (it) {
it.should.be.within(0, 53);
},
'avgtime' : function (it) {
it.should.be.an.Number();
}
});
});
}
callback();
});
};

async.each(urls, test, function(err) {
should.not.exist(err);
done();
});
});

it('should return a Array like [ { "avgtime" : 1, "month" : 7 } ]', function(done) {
var urls = [
'/api/avgtime/2015/permonth',
'/api/avgtime/2015/permonth?month=7',
'/api/avgtime/2015/permonth?recent=1'
];
var test = function (value, callback) {
request.get(value)
.expect(200)
.end(function ( err, res ) {

should.not.exist(err);

JSON.parse(res.text).should.not.throw();
var result = JSON.parse(res.text);

result.should.be.an.Array();
if ( result.length > 0 ) {
result.forEach(function(elem) {
elem.should.match( {
'month' : function (it) {
it.should.be.within(1, 12);
},
'avgtime' : function (it) {
it.should.be.an.Number();
}
});
});
}
callback();
});
};

async.each(urls, test, function(err) {
should.not.exist(err);
done();
});
});

var test_500 = function (message, urls) {
it('should return a 500 status with a message "' + message + '"', function (done) {
var test = function (value, callback) {
request.get(value)
.expect(500)
.end(function (err, res) {
res.text.should.equal(message);
callback();
});
};
async.each(urls, test, function(err) {
should.not.exist(err);
done();
});
})
};

test_500('you should enter a valid year',
[
'/api/avgtime/aa/perday',
'/api/avgtime/-2/perweek',
'/api/avgtime/22a/permonth'
]
);

test_500('date must be a valid date',
[
'/api/avgtime/2015/perday?date=3910',
'/api/avgtime/2015/perday?date=abc',
'/api/avgtime/2015/perday?date=09-01'
]
);

test_500('recent must be a positive integer',
[
'/api/avgtime/2015/perday?recent=0',
'/api/avgtime/2015/perweek?recent=a',
'/api/avgtime/2015/permonth?recent=0a'
]
);

test_500('week must be a valid week',
[
'/api/avgtime/2015/perweek?week=60',
'/api/avgtime/2015/perweek?week=-1',
'/api/avgtime/2015/perweek?week=a',
'/api/avgtime/2015/perweek?week=1a',
]
);

test_500('date must be a valid date',
[
'/api/avgtime/2015/perweek?date=3910',
'/api/avgtime/2015/perweek?date=abc',
'/api/avgtime/2015/perweek?date=09-01'
]
);

test_500('month must be a valid month',
[
'/api/avgtime/2015/permonth?month=60',
'/api/avgtime/2015/permonth?month=-1',
'/api/avgtime/2015/permonth?month=a',
'/api/avgtime/2015/permonth?month=1a',
]
);
});

0 comments on commit c95f30c

Please sign in to comment.