Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #22 from stanzheng/master
Browse files Browse the repository at this point in the history
Adding Tests -- WIP #1
  • Loading branch information
Stanley Zheng committed Dec 18, 2014
2 parents e2b815a + 28ddce5 commit 59c285a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
sudo: false
node_js:
- "0.11"
- "0.10"
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
hrt-bus-text-my-bus
===================

[![Build Status](https://travis-ci.org/Code4HR/hrt-bus-text-my-bus.svg?branch=master)](https://travis-ci.org/Code4HR/hrt-bus-text-my-bus)

STATUS: pre-alpha need more testing
phone number: 757-913-5000

Expand Down Expand Up @@ -45,7 +47,7 @@ npm install
node app.js
```

<img src="http://i.imgur.com/UTX2FUu.png" style="width:400px";></img>
<img src="http://i.imgur.com/UTX2FUu.png" width="400px";></img>


HRT BUS API
Expand Down
39 changes: 20 additions & 19 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
(function () {
module.exports = (function () {
var r = require("request"),
express = require("express"),
app = express(),
Expand All @@ -39,15 +39,15 @@

// Asking for help.
(/help/i.test(text) ?
getResponse('Hi, thanks for texting your HRT bus! Please text an '
+ 'address or a stop number to find the next time your bus will'
getResponse('Hi, thanks for texting your HRT bus! Please text an '
+ 'address or a stop number to find the next time your bus will'
+ ' come your way.') :
// Check for an address.
hasAddress(text) ?
getStop(text).then(getTimes).then(getResponse) :
// Checks for a bus stop number, like http://hrtb.us/#stops/0263
hasStop(text) ?
getTimes(text).then(getResponse) :
hasStop(text) ?
getTimes(text).then(getResponse) :
// Otherwise, give a help(ful) message.
getResponse('Hi! You have texted the Text Your Bus application. I '
+ 'do not quite understand your request. Please text "help" '
Expand Down Expand Up @@ -76,7 +76,7 @@
* @return {Boolean} Does the body have an address?
**/
var hasAddress = function (text) {
return /^\s*(\d*)\s*(([a-z]+[.]?|\d*(1st|[23]n?d|[4-9]th))\s*)+(([a-z]+[.]?)\s*,?)?\s*(([a-z]*\s*)*,?)?\s*(V(irgini)?a)?\s*$/i.test(text)
return /^\s*(\d*)\s*(([a-z]+[.]?|\d*(1st|[23]n?d|[4-9]th))\s*)+(([a-z]+[.]?)\s*,?)?\s*(([a-z]*\s*)*,?)?\s*(V(irgini)?a)?\s*$/i.test(text);
};

/**
Expand All @@ -95,7 +95,7 @@
if (bounded.length === 0) {
return resolve('0');
} else {
r.get('http://api.hrtb.us/api/stops/near/'
r.get('http://api.hrtb.us/api/stops/near/'
+ point.lat + '/' + point.lon,
function (error, response, body) {
return resolve(closestStop(JSON.parse(body)
Expand All @@ -111,7 +111,7 @@
* Seven Cities area.
*
* @param {Array} points - The points returned from Nominatim.
*
*
* @return {Array} The points local to the Seven Cities.
**/
var bound = function (points) {
Expand Down Expand Up @@ -147,11 +147,11 @@
* @return {Number} The distance from x to y.
**/
var distance = function (x, y) {
return Math.sqrt(Math.pow(x.location[0] - y.location[0], 2)
return Math.sqrt(Math.pow(x.location[0] - y.location[0], 2)
+ Math.pow(x.location[1] - y.location[1], 2));
};

/**
/**
* Makes API request to the HRTB.US API and transform the json results.
*
* @param {String} stop - The stop to get times for.
Expand All @@ -163,7 +163,7 @@
return new bPromise(function (resolve, reject) {
r.get("http://api.hrtb.us/api/stop_times/" + stop,
function (err, response, body) {
/* If we cannot parse the times, provide a helpful error
/* If we cannot parse the times, provide a helpful error
* message. */
return resolve(body === '[]' || JSON.parse(body) === [] ?
'Hmm, I cannot tell if that stop does not exist or if '
Expand All @@ -172,7 +172,7 @@
+ 'stop you mentioned any time soon.' :
showTimes(JSON.parse(body)));
});
});
});
};

/**
Expand All @@ -189,15 +189,15 @@
// EVMS/NORFOLK will be here in about 10, the next one in 15.
// NEWTOWN ROAD will be here in about 5 minutes and the next.
return _.reduce(times, function (response, time) {
return {
return {
'0': 'Light rail'
, '3': 'Bus'
, '4': 'Ferry'
}[time.drop_off_type] + ' '
+ time.routeShortName + ' to ' + time.destination
+ ' will arrive in '
, '4': 'Ferry'
}[time.drop_off_type] + ' '
+ time.routeShortName + ' to ' + time.destination
+ ' will arrive in '
+ moment.utc(time.arrival_time).diff(moment.utc()
, 'minutes')
, 'minutes')
+ ' minutes.';
}, '');
}).join('\n');
Expand All @@ -214,7 +214,7 @@
return new bPromise(function (resolve, reject) {
console.log(message);
resolve('<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n'
+ '<Response>\n<Message>' + message
+ '<Response>\n<Message>' + message
+ '</Message>\n</Response>');
});
};
Expand All @@ -234,4 +234,5 @@
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
return app;
}());
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@
"description": "hrt-bus-text-my-bus ===================",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test"
},
"repository": {
"type": "git",
"url": "https://github.com/stanzheng/hrt-bus-text-my-bus.git"
},
"author": "",
"license": "ISC",
"license": "APACHE",
"bugs": {
"url": "https://github.com/stanzheng/hrt-bus-text-my-bus/issues"
},
"homepage": "https://github.com/stanzheng/hrt-bus-text-my-bus",
"dependencies": {
"bluebird": "^2.3.10",
"bluebird": "^2.3.11",
"body-parser": "^1.9.0",
"express": "^4.9.7",
"lodash": "^2.4.1",
"moment": "^2.8.3",
"path": "^0.4.9",
"querystring": "^0.2.0",
"request": "^2.45.0",
"request": "^2.51.0",
"should": "^4.4.1",
"supertest": "^0.15.0",
"twilio": "^1.7.0",
"where": "^0.1.2"
"where": "^0.1.2",
"xml2js": "^0.4.4"
},
"devDependencies": {
"mocha": "^2.0.1"
}
}
59 changes: 59 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var assert = require("assert");
var express = require("express");
var r = require("supertest");
var parseString = require('xml2js').parseString;
var should = require('should');
/**
* Test
*
* @param {Object} req - The request value.
**/
process.env.PORT = 4000;
var app = require("../app.js");


describe('General incoming request', function(){

it('responds with xml', function(done){
r(app)
.post('/msg')
.send({Body:"8004"})
.set('Accept', 'application/xml')
.expect(200)
.end(function(err, res){
if (err) throw err;
done();
});
});

it('light rail stops are valid, testing 8004', function(done){
r(app)
.post('/msg')
.send({Body:"8004"})
.set('Accept', 'application/xml')
.expect(200)
.end(function(err, res){
if (err) throw err;
should.not.exist(err);

// cleans utf8 parsing for xml in form
// see http://www.multiasking.com/blog/xml2js-sax-js-non-whitespace-before-first-tag/
var xml = res.text.replace("\ufeff", "");
parseString(xml, function (err, ouput) {
var parse = ouput.Response.Message[0];
parse.should.startWith('Light rail ');
//parse.should.have("Light rail");
});
done();
});
});
});


// #TODO Tests for
// Bounding Boxes
// Asking for help
// Checking for input a known address
// Checking for input a unknown/invalid address
// Check for known stop number
// Check for unknown/invalid stop number

0 comments on commit 59c285a

Please sign in to comment.