Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #748 from chris48s/issue744
Sort addresses in API outputs
- Loading branch information
Showing
4 changed files
with
84 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 64 additions & 60 deletions
124
polling_stations/apps/data_finder/tests/test_address_sorter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,124 @@ | ||
from operator import itemgetter | ||
from collections import namedtuple | ||
from django.test import TestCase | ||
from data_finder.helpers import AddressSorter | ||
from pollingstations.models import ResidentialAddress | ||
|
||
class AddressSorterTest(TestCase): | ||
Address = namedtuple('Address', ['id', 'address']) | ||
|
||
def setUp(self): | ||
self.sorter = AddressSorter() | ||
self.key = itemgetter(1) | ||
class AddressSorterTest(TestCase): | ||
|
||
def test_numeric_order(self): | ||
# Addresses should sort in numeric order, not string order | ||
in_list = [ | ||
(1, "10, THE SQUARE, BOGNOR REGIS"), | ||
(2, "1, THE SQUARE, BOGNOR REGIS"), | ||
(3, "2, THE SQUARE, BOGNOR REGIS"), | ||
Address(id=1, address="10, THE SQUARE, BOGNOR REGIS"), | ||
Address(id=2, address="1, THE SQUARE, BOGNOR REGIS"), | ||
Address(id=3, address="2, THE SQUARE, BOGNOR REGIS"), | ||
] | ||
|
||
expected = [ | ||
(2, "1, THE SQUARE, BOGNOR REGIS"), | ||
(3, "2, THE SQUARE, BOGNOR REGIS"), | ||
(1, "10, THE SQUARE, BOGNOR REGIS"), | ||
Address(id=2, address="1, THE SQUARE, BOGNOR REGIS"), | ||
Address(id=3, address="2, THE SQUARE, BOGNOR REGIS"), | ||
Address(id=1, address="10, THE SQUARE, BOGNOR REGIS"), | ||
] | ||
|
||
result = self.sorter.natural_sort(in_list, self.key) | ||
sorter = AddressSorter(in_list) | ||
result = sorter.natural_sort() | ||
|
||
self.assertEqual(expected, result) | ||
|
||
def test_group_by_street(self): | ||
# Numbered addresses should group by street/building | ||
in_list = [ | ||
(1, "1 Haynes House Mount Pleasant"), | ||
(2, "1 Partridge House Mount Pleasant"), | ||
(3, "3 Haynes House Mount Pleasant"), | ||
(4, "2 Partridge House Mount Pleasant"), | ||
(5, "2 Haynes House Mount Pleasant"), | ||
Address(id=1, address="1 Haynes House Mount Pleasant"), | ||
Address(id=2, address="1 Partridge House Mount Pleasant"), | ||
Address(id=3, address="3 Haynes House Mount Pleasant"), | ||
Address(id=4, address="2 Partridge House Mount Pleasant"), | ||
Address(id=5, address="2 Haynes House Mount Pleasant"), | ||
] | ||
|
||
expected = [ | ||
(1, "1 Haynes House Mount Pleasant"), | ||
(5, "2 Haynes House Mount Pleasant"), | ||
(3, "3 Haynes House Mount Pleasant"), | ||
(2, "1 Partridge House Mount Pleasant"), | ||
(4, "2 Partridge House Mount Pleasant"), | ||
Address(id=1, address="1 Haynes House Mount Pleasant"), | ||
Address(id=5, address="2 Haynes House Mount Pleasant"), | ||
Address(id=3, address="3 Haynes House Mount Pleasant"), | ||
Address(id=2, address="1 Partridge House Mount Pleasant"), | ||
Address(id=4, address="2 Partridge House Mount Pleasant"), | ||
] | ||
|
||
result = self.sorter.natural_sort(in_list, self.key) | ||
sorter = AddressSorter(in_list) | ||
result = sorter.natural_sort() | ||
|
||
self.assertEqual(expected, result) | ||
|
||
def test_group_in_numbered_buildings(self): | ||
# Numbered addresses should group inside numbered buildings | ||
in_list = [ | ||
(1, "1 Southlands Court Birchfield Road"), | ||
(2, "1 233 The Beeches Birchfield Road"), | ||
(3, "207 Birchfield Road"), | ||
(4, "203 Birchfield Road"), | ||
(5, "2 233 The Beeches Birchfield Road"), | ||
(6, "2 Southlands Court Birchfield Road"), | ||
Address(id=1, address="1 Southlands Court Birchfield Road"), | ||
Address(id=2, address="1 233 The Beeches Birchfield Road"), | ||
Address(id=3, address="207 Birchfield Road"), | ||
Address(id=4, address="203 Birchfield Road"), | ||
Address(id=5, address="2 233 The Beeches Birchfield Road"), | ||
Address(id=6, address="2 Southlands Court Birchfield Road"), | ||
] | ||
|
||
expected = [ | ||
(2, "1 233 The Beeches Birchfield Road"), | ||
(5, "2 233 The Beeches Birchfield Road"), | ||
(1, "1 Southlands Court Birchfield Road"), | ||
(6, "2 Southlands Court Birchfield Road"), | ||
(4, "203 Birchfield Road"), | ||
(3, "207 Birchfield Road"), | ||
Address(id=2, address="1 233 The Beeches Birchfield Road"), | ||
Address(id=5, address="2 233 The Beeches Birchfield Road"), | ||
Address(id=1, address="1 Southlands Court Birchfield Road"), | ||
Address(id=6, address="2 Southlands Court Birchfield Road"), | ||
Address(id=4, address="203 Birchfield Road"), | ||
Address(id=3, address="207 Birchfield Road"), | ||
] | ||
|
||
result = self.sorter.natural_sort(in_list, self.key) | ||
sorter = AddressSorter(in_list) | ||
result = sorter.natural_sort() | ||
|
||
self.assertEqual(expected, result) | ||
|
||
def test_number_suffix(self): | ||
# Numbered addresses with number suffix should sort in | ||
# numeric order and then alphabetically by suffix | ||
in_list = [ | ||
(1, "200A Evesham Road"), | ||
(2, "190A Evesham Road"), | ||
(3, "The Forge Mill Evesham Road"), | ||
(4, "202A Evesham Road"), | ||
(5, "190C Evesham Road"), | ||
(6, "190B Evesham Road"), | ||
Address(id=1, address="200A Evesham Road"), | ||
Address(id=2, address="190A Evesham Road"), | ||
Address(id=3, address="The Forge Mill Evesham Road"), | ||
Address(id=4, address="202A Evesham Road"), | ||
Address(id=5, address="190C Evesham Road"), | ||
Address(id=6, address="190B Evesham Road"), | ||
] | ||
|
||
expected = [ | ||
(2, "190A Evesham Road"), | ||
(6, "190B Evesham Road"), | ||
(5, "190C Evesham Road"), | ||
(1, "200A Evesham Road"), | ||
(4, "202A Evesham Road"), | ||
(3, "The Forge Mill Evesham Road"), | ||
Address(id=2, address="190A Evesham Road"), | ||
Address(id=6, address="190B Evesham Road"), | ||
Address(id=5, address="190C Evesham Road"), | ||
Address(id=1, address="200A Evesham Road"), | ||
Address(id=4, address="202A Evesham Road"), | ||
Address(id=3, address="The Forge Mill Evesham Road"), | ||
] | ||
|
||
result = self.sorter.natural_sort(in_list, self.key) | ||
sorter = AddressSorter(in_list) | ||
result = sorter.natural_sort() | ||
|
||
self.assertEqual(expected, result) | ||
|
||
def test_prefixed_numbers(self): | ||
# Prefixed numbers (e.g: flats) should sort by number | ||
in_list = [ | ||
(1, "Flat 10 Knapton House North Walsham Road"), | ||
(2, "Gardeners Cottage Knapton House North Walsham Road"), | ||
(3, "Old Coach House North Walsham Road"), | ||
(4, "Flat 1 Knapton House North Walsham Road"), | ||
(5, "Flat 2 Knapton House North Walsham Road"), | ||
Address(id=1, address="Flat 10 Knapton House North Walsham Road"), | ||
Address(id=2, address="Gardeners Cottage Knapton House North Walsham Road"), | ||
Address(id=3, address="Old Coach House North Walsham Road"), | ||
Address(id=4, address="Flat 1 Knapton House North Walsham Road"), | ||
Address(id=5, address="Flat 2 Knapton House North Walsham Road"), | ||
] | ||
|
||
expected = [ | ||
(4, "Flat 1 Knapton House North Walsham Road"), | ||
(5, "Flat 2 Knapton House North Walsham Road"), | ||
(1, "Flat 10 Knapton House North Walsham Road"), | ||
(2, "Gardeners Cottage Knapton House North Walsham Road"), | ||
(3, "Old Coach House North Walsham Road"), | ||
Address(id=4, address="Flat 1 Knapton House North Walsham Road"), | ||
Address(id=5, address="Flat 2 Knapton House North Walsham Road"), | ||
Address(id=1, address="Flat 10 Knapton House North Walsham Road"), | ||
Address(id=2, address="Gardeners Cottage Knapton House North Walsham Road"), | ||
Address(id=3, address="Old Coach House North Walsham Road"), | ||
] | ||
|
||
result = self.sorter.natural_sort(in_list, self.key) | ||
sorter = AddressSorter(in_list) | ||
result = sorter.natural_sort() | ||
|
||
self.assertEqual(expected, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters