Skip to content

Commit d19d663

Browse files
committed
Added tests for district and subDistrict
1 parent 3ee25aa commit d19d663

4 files changed

Lines changed: 222 additions & 0 deletions

File tree

tests/data/districts.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"tests" : [
3+
{
4+
"base" : "L27 8XY",
5+
"expected" : "L27"
6+
},
7+
{
8+
"base" : "NR10 3EZ",
9+
"expected" : "NR10"
10+
},
11+
{
12+
"base" : "RG4 5AY",
13+
"expected" : "RG4"
14+
},
15+
{
16+
"base" : "NE69 7AW",
17+
"expected" : "NE69"
18+
},
19+
{
20+
"base" : "SE23 2NF",
21+
"expected" : "SE23"
22+
},
23+
{
24+
"base" : "BT35 8GE",
25+
"expected" : "BT35"
26+
},
27+
{
28+
"base" : "L278XY",
29+
"expected" : "L27"
30+
},
31+
{
32+
"base" : "NR103EZ",
33+
"expected" : "NR10"
34+
},
35+
{
36+
"base" : "RG45AY",
37+
"expected" : "RG4"
38+
},
39+
{
40+
"base" : "NE697AW",
41+
"expected" : "NE69"
42+
},
43+
{
44+
"base" : "SE232NF",
45+
"expected" : "SE23"
46+
},
47+
{
48+
"base" : "BT358GE",
49+
"expected" : "BT35"
50+
},
51+
{
52+
"base" : "EC1A 1BB",
53+
"expected" : "EC1"
54+
},
55+
{
56+
"base" : "W1A0AX",
57+
"expected" : "W1"
58+
},
59+
{
60+
"base" : "NW1W1AA",
61+
"expected" : "NW1"
62+
},
63+
{
64+
"base" : "N1C 0AB",
65+
"expected" : "N1"
66+
}
67+
]
68+
}

tests/data/sub-districts.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"tests" : [
3+
{
4+
"base" : "L27 8XY",
5+
"expected" : null
6+
},
7+
{
8+
"base" : "NR10 3EZ",
9+
"expected" : null
10+
},
11+
{
12+
"base" : "RG4 5AY",
13+
"expected" : null
14+
},
15+
{
16+
"base" : "NE69 7AW",
17+
"expected" : null
18+
},
19+
{
20+
"base" : "SE23 2NF",
21+
"expected" : null
22+
},
23+
{
24+
"base" : "BT35 8GE",
25+
"expected" : null
26+
},
27+
{
28+
"base" : "L278XY",
29+
"expected" : null
30+
},
31+
{
32+
"base" : "NR103EZ",
33+
"expected" : null
34+
},
35+
{
36+
"base" : "RG45AY",
37+
"expected" : null
38+
},
39+
{
40+
"base" : "NE697AW",
41+
"expected" : null
42+
},
43+
{
44+
"base" : "SE232NF",
45+
"expected" : null
46+
},
47+
{
48+
"base" : "BT358GE",
49+
"expected" : null
50+
},
51+
{
52+
"base" : "EC1A 1BB",
53+
"expected" : "EC1A"
54+
},
55+
{
56+
"base" : "W1A0AX",
57+
"expected" : "W1A"
58+
},
59+
{
60+
"base" : "NW1W1AA",
61+
"expected" : "NW1W"
62+
},
63+
{
64+
"base" : "N1C 0AB",
65+
"expected" : "N1C"
66+
}
67+
]
68+
}

tests/exhaustive_unit.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,54 @@ describe("Exhaustive postcode test", function () {
135135
done();
136136
});
137137
});
138+
describe("District parsing", function () {
139+
it("should return the correct district", function (done) {
140+
this.timeout(60000);
141+
testData.forEach(function (testPostcode) {
142+
var pc = testPostcode[0],
143+
postcode = new Postcode(pc),
144+
downcasePostcode = new Postcode(pc.toLowerCase()),
145+
unspacedPostcode = new Postcode(pc.replace(/\s/, "")),
146+
testDistrict;
147+
if (pc.length === 7) {
148+
// Since this isn't normalised in dataset, best we can do is see if normalised data matches
149+
assert.equal(postcode.district(), downcasePostcode.district());
150+
assert.equal(postcode.district(), unspacedPostcode.district());
151+
} else {
152+
// Any space indicates incode/outcode
153+
testDistrict = pc.match(/\s.*/)[0].replace(/\s/, "");
154+
assert.equal(postcode.district(), testDistrict);
155+
assert.equal(downcasePostcode.district(), testDistrict);
156+
assert.equal(unspacedPostcode.district(), testDistrict);
157+
}
158+
});
159+
done();
160+
});
161+
});
162+
describe("Sub-district parsing", function () {
163+
it("should return the correct sub-district", function (done) {
164+
this.timeout(60000);
165+
testData.forEach(function (testPostcode) {
166+
var pc = testPostcode[0],
167+
postcode = new Postcode(pc),
168+
downcasePostcode = new Postcode(pc.toLowerCase()),
169+
unspacedPostcode = new Postcode(pc.replace(/\s/, "")),
170+
testSubDistrict;
171+
if (pc.length === 7) {
172+
// Since this isn't normalised in dataset, best we can do is see if normalised data matches
173+
assert.equal(postcode.subDistrict(), downcasePostcode.subDistrict());
174+
assert.equal(postcode.subDistrict(), unspacedPostcode.subDistrict());
175+
} else {
176+
// Any space indicates incode/outcode
177+
testSubDistrict = pc.match(/\s.*/)[0].replace(/\s/, "");
178+
assert.equal(postcode.subDistrict(), testSubDistrict);
179+
assert.equal(downcasePostcode.subDistrict(), testSubDistrict);
180+
assert.equal(unspacedPostcode.subDistrict(), testSubDistrict);
181+
}
182+
});
183+
done();
184+
});
185+
});
138186
describe("Sector parsing", function () {
139187
it("should return the correct sector", function (done) {
140188
this.timeout(60000);

tests/unit.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,44 @@ describe("Area parsing", function () {
119119
});
120120
});
121121

122+
describe("District parsing", function () {
123+
before(function (done) {
124+
testData = fs.readFile(path.join(dataDir, "districts.json"), function (error, data) {
125+
if (error) throw error;
126+
testData = JSON.parse(data);
127+
done();
128+
});
129+
});
130+
131+
it ("should correctly parse districts", function () {
132+
testData.tests.forEach(function (elem) {
133+
assert.equal(new Postcode(elem.base).district(), elem.expected);
134+
});
135+
});
136+
it ("should return null if invalid postcode", function () {
137+
assert.isNull(new Postcode("Definitely bogus").district());
138+
});
139+
});
140+
141+
describe("Sub-district parsing", function () {
142+
before(function (done) {
143+
testData = fs.readFile(path.join(dataDir, "sub-districts.json"), function (error, data) {
144+
if (error) throw error;
145+
testData = JSON.parse(data);
146+
done();
147+
});
148+
});
149+
150+
it ("should correctly parse sub-districts", function () {
151+
testData.tests.forEach(function (elem) {
152+
assert.equal(new Postcode(elem.base).subDistrict(), elem.expected);
153+
});
154+
});
155+
it ("should return null if invalid postcode", function () {
156+
assert.isNull(new Postcode("Definitely bogus").subDistrict());
157+
});
158+
});
159+
122160
describe("Sector parsing", function () {
123161
before(function (done) {
124162
testData = fs.readFile(path.join(dataDir, "sectors.json"), function (error, data) {

0 commit comments

Comments
 (0)