Skip to content

Commit

Permalink
feat(SPD): Implement scottish postcode lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
cblanc committed Aug 28, 2019
1 parent 4b3104b commit d946ed9
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/controllers/scottish_postcodes_controller.js
@@ -0,0 +1,24 @@
"use strict";

const ScottishPostcode = require("../models/scottish_postcode");
const Postcode = require("postcode");
const {
InvalidPostcodeError,
PostcodeNotFoundError,
} = require("../lib/errors.js");

exports.show = (request, response, next) => {
const { postcode } = request.params;
if (!Postcode.isValid(postcode.trim()))
return next(new InvalidPostcodeError());

ScottishPostcode.find(postcode, (error, result) => {
if (error) return next(error);
if (!result) return next(new PostcodeNotFoundError());
response.jsonApiResponse = {
status: 200,
result: ScottishPostcode.toJson(result),
};
return next();
});
};
5 changes: 4 additions & 1 deletion config/routes.js
Expand Up @@ -5,7 +5,8 @@ const utils = require("../app/controllers/utils_controller");
const places = require("../app/controllers/places_controller");
const outcodes = require("../app/controllers/outcodes_controller");
const postcodes = require("../app/controllers/postcodes_controller");
const terminatedPostcodes = require("../app/terminated_postcodes_controller");
const scottishPostcodes = require("../app/controllers/scottish_postcodes_controller");
const terminatedPostcodes = require("../app/controllers/terminated_postcodes_controller");

module.exports = app => {
app.get("/", pages.home);
Expand Down Expand Up @@ -36,4 +37,6 @@ module.exports = app => {
app.get("/random/postcodes", postcodes.random);

app.get("/terminated_postcodes/:postcode", terminatedPostcodes.show);

app.get("/scotland/postcodes/:postcode", scottishPostcodes.show);
};
12 changes: 12 additions & 0 deletions test/helper/setup.js
Expand Up @@ -101,11 +101,23 @@ const clearScottishPostcodeDb = callback => {
ScottishPostcode._destroyRelation(callback);
};

const seedScottishPostcodeDb = callback => {
if (NO_RELOAD_DB) return callback(null);
async.series(
[
cb => ScottishPostcode._setupTable(seedScotlandPostcodePath, cb),
cb => ScottishConstituency._setupTable(cb),
],
callback
);
};

module.exports = {
clearTestDb,
seedPostcodePath,
seedTerminatedPostcodeDb,
seedPostcodeDb,
seedScottishPostcodeDb,
seedScotlandPostcodePath,
clearTerminatedPostcodesDb,
clearPostcodeDb,
Expand Down
116 changes: 116 additions & 0 deletions test/scottish_postcodes.integration.js
@@ -0,0 +1,116 @@
"use strict";

const request = require("supertest");
const { assert } = require("chai");
const {
postcodesioApplication,
clearScottishPostcodeDb,
seedScottishPostcodeDb,
} = require("./helper");
const app = postcodesioApplication();
const Postcode = require("postcode");

const error404Message = "Postcode not found";

describe("Scottish postcode route", () => {
const testPostcode = "ML11 0GH";

before(function(done) {
this.timeout(0);
clearScottishPostcodeDb((error, result) => {
if (error) return done(error);
seedScottishPostcodeDb((error, result) => {
if (error) return done(error);
done();
});
});
});

after(clearScottishPostcodeDb);

describe("/GET /scotland/postcodes/:postcode", () => {
it("should return 200 if postcode found", done => {
const path = `/scotland/postcodes/${encodeURI(testPostcode)}`;
request(app)
.get(path)
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
assert.equal(response.body.status, 200);
assert.equal(Object.keys(response.body).length, 2);
done();
});
});

it("returns the correct attributes", done => {
const path = `/scotland/postcodes/${encodeURI(testPostcode)}`;
request(app)
.get(path)
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
const { result } = response.body;
assert.deepEqual(result, {
codes: {
scottish_parliamentary_constituency: "S16000090",
},
postcode: "ML11 0GH",
scottish_parliamentary_constituency: "Clydesdale",
});
done();
});
});

it("accepts padded postcode", done => {
const postcode = " " + testPostcode + " ";
const path = `/scotland/postcodes/${encodeURI(postcode)}`;
request(app)
.get(path)
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
assert.equal(response.body.status, 200);
const { result } = response.body;
assert.deepEqual(result, {
codes: {
scottish_parliamentary_constituency: "S16000090",
},
postcode: "ML11 0GH",
scottish_parliamentary_constituency: "Clydesdale",
});
done();
});
});

it("404 if not a valid postcode according to the postcode module", done => {
const path = `/scotland/postcodes/foo`;
assert.isFalse(Postcode.isValid("foo"));
request(app)
.get(path)
.expect("Content-Type", /json/)
.expect(404)
.end(done);
});

it("should return 404 if postcode not found", done => {
const postcode = "ID11QE";
const path = `/scotland/postcodes/${encodeURI(postcode)}`;
request(app)
.get(path)
.expect("Content-Type", /json/)
.expect(404)
.end((error, response) => {
if (error) return done(error);
assert.property(response.body, "status");
assert.equal(response.body.status, 404);
assert.property(response.body, "error");
assert.equal(Object.keys(response.body).length, 2);
assert.equal(response.body.error, error404Message);
done();
});
});
});
});

0 comments on commit d946ed9

Please sign in to comment.