Skip to content

Commit

Permalink
Now constructing where= field more accurately
Browse files Browse the repository at this point in the history
  • Loading branch information
mdb committed Feb 7, 2013
1 parent f9d933d commit c196e95
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
11 changes: 8 additions & 3 deletions lib/helpers.js
Expand Up @@ -18,9 +18,14 @@ exports.initialCap = function (str) {
};

exports.constructWhereField = function (obj) {
var params = _.map(obj, function (value, key) {
return key.toUpperCase() + "='" + exports.initialCap(value) + "'";
}).join('+and+');
var properties = ['objectid', 'age', 'race', 'sex', 'type', 'date', 'unit', 'action', 'status', 'long_', 'lat'],
params = _.map(obj, function (value, key) {
if (_.contains(properties, key.toLowerCase())) {
return key.toUpperCase() + "='" + exports.initialCap(value) + "'";
}
}).filter(function (item) {
return (item !== undefined);
}).join('+and+');

return params;
};
Expand Down
32 changes: 26 additions & 6 deletions test/helpers.js
Expand Up @@ -37,11 +37,31 @@ describe("helpers", function() {
expect(typeof helpers.constructWhereField).to.eql('function');
});

it("returns the properly formatted string value of the where='' part of an API request", function () {
expect(helpers.constructWhereField({
foo: 'bar',
baz: 'bim'
})).to.eql("FOO='Bar'+and+BAZ='Bim'");
describe("when the object it is passed contains relevant and permitted 'where' field properties", function () {
it("returns the properly formatted string value of the where='' part of an API request", function () {
expect(helpers.constructWhereField({
objectid: 'someObjectId',
race: 'someRace',
age: 'someAge',
sex: 'someSex',
type: 'someType',
date: 'someDate',
unit: 'someUnit',
action: 'someAction',
status: 'someStatus',
long_: 'someLong',
lat: 'someLat'
})).to.eql("OBJECTID='SomeObjectId'+and+RACE='SomeRace'+and+AGE='SomeAge'+and+SEX='SomeSex'+and+TYPE='SomeType'+and+DATE='SomeDate'+and+UNIT='SomeUnit'+and+ACTION='SomeAction'+and+STATUS='SomeStatus'+and+LONG_='SomeLong'+and+LAT='SomeLat'");
});
});

describe("when the object it is passed contains relevant and permitted 'where' field properties, as well as not-permitted where field properties", function () {
it("returns the properly formatted string value of the where='' part of an API request", function () {
expect(helpers.constructWhereField({
objectid: 'someObjectId',
prohibitedField: 'valueShouldNotAppear'
})).to.eql("OBJECTID='SomeObjectId'");
});
});
});

Expand All @@ -54,7 +74,7 @@ describe("helpers", function() {
expect(helpers.constructReqParams({
foo: 'bar',
baz: 'bim'
}).where).to.eql("FOO='Bar'+and+BAZ='Bim'");
}).where).to.eql("");

expect(helpers.constructReqParams({
foo: 'bar',
Expand Down
49 changes: 32 additions & 17 deletions test/phl_pac_complaints.js
Expand Up @@ -35,26 +35,41 @@ describe("PhlPacComplaints", function() {
it("exists as a method on a PhlPacComplaints instance", function () {
expect(typeof phlPacComplaints.getData).to.eql('function');
});

it("makes an API call to the URL it is passed endpoint", function (done) {
nock('http://gis.phila.gov')
.get("/ArcGIS/rest/services/PhilaGov/PAC_Complaints_2009_2012/MapServer/0/query?where=FOO=%27Bar%27&f=json")
.reply(200, {resp: 'fakeResponse'});

phlPacComplaints.getData({foo: 'bar'}, function(err, data) {
expect(data).to.eql({resp: 'fakeResponse'});
done();

describe("when it is passed a query object whose properties are not allowed", function () {
it("makes an API call to the proper endpoint, without appending the irrelevant params object properties to the where field", function (done) {
nock('http://gis.phila.gov')
.get("/ArcGIS/rest/services/PhilaGov/PAC_Complaints_2009_2012/MapServer/0/query?where=&f=json")
.reply(200, {resp: 'fakeResponse'});

phlPacComplaints.getData({foo: 'bar'}, function(err, data) {
expect(data).to.eql({resp: 'fakeResponse'});
done();
});
});
});

describe("when it is passed a query object whose properties are allowed", function () {
it("makes an API call to the proper endpoint, appending the relevant params object properties to the where field", function (done) {
nock('http://gis.phila.gov')
.get("/ArcGIS/rest/services/PhilaGov/PAC_Complaints_2009_2012/MapServer/0/query?where=RACE=%27White%27&f=json")
.reply(200, {resp: 'fakeResponse'});

phlPacComplaints.getData({race: 'white'}, function(err, data) {
expect(data).to.eql({resp: 'fakeResponse'});
done();
});
});
});

it("continues to work as designed, even if the API responds with an error code of 500", function (done) {
nock("http://gis.phila.gov")
.get("/ArcGIS/rest/services/PhilaGov/PAC_Complaints_2009_2012/MapServer/0/query?where=FOO=%27Bar%27&f=json")
.reply(500, {resp: 'fake500Response'});
it("continues to work as designed, even if the API responds with an error code of 500", function (done) {
nock("http://gis.phila.gov")
.get("/ArcGIS/rest/services/PhilaGov/PAC_Complaints_2009_2012/MapServer/0/query?where=RACE=%27White%27&f=json")
.reply(500, {resp: 'fake500Response'});

phlPacComplaints.getData({foo: 'bar'}, function(err, data) {
expect(data).to.eql({resp: 'fake500Response'});
done();
phlPacComplaints.getData({race: 'white'}, function(err, data) {
expect(data).to.eql({resp: 'fake500Response'});
done();
});
});
});
});
Expand Down

0 comments on commit c196e95

Please sign in to comment.