mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-03-10 13:22:50 -04:00
Add function method to aggregate areas
This is standalone for now - we'll plumb it in later.
This commit is contained in:
@@ -127,6 +127,10 @@ class BroadcastArea(BaseBroadcastArea, SortableMixin):
|
||||
def ancestors(self):
|
||||
return list(self._ancestors_iterator)
|
||||
|
||||
@cached_property
|
||||
def parent(self):
|
||||
return next(iter(self.ancestors), None)
|
||||
|
||||
@property
|
||||
def _ancestors_iterator(self):
|
||||
id = self.id
|
||||
|
||||
10
app/broadcast_areas/utils.py
Normal file
10
app/broadcast_areas/utils.py
Normal file
@@ -0,0 +1,10 @@
|
||||
def aggregate_areas(areas):
|
||||
areas = _aggregate_wards_by_local_authority(areas)
|
||||
return areas
|
||||
|
||||
|
||||
def _aggregate_wards_by_local_authority(areas):
|
||||
return {
|
||||
area.parent if area.id.startswith('wd20-')
|
||||
else area for area in areas
|
||||
}
|
||||
@@ -21,3 +21,19 @@ SANTA_A = [
|
||||
[25.8910, 66.5500],
|
||||
[25.889, 66.55000],
|
||||
]
|
||||
|
||||
BURFORD = [
|
||||
[51.8925, -1.7495],
|
||||
[51.7372, -1.7825],
|
||||
[51.7159, -1.5257],
|
||||
[51.8866, -1.5037],
|
||||
[51.8925, -1.7495],
|
||||
]
|
||||
|
||||
CHELTENHAM = [
|
||||
[51.9324, -2.1265],
|
||||
[51.8633, -2.1306],
|
||||
[51.8637, -2.0242],
|
||||
[51.9328, -2.0221],
|
||||
[51.9324, -2.1265],
|
||||
]
|
||||
|
||||
71
tests/app/broadcast_areas/test_utils.py
Normal file
71
tests/app/broadcast_areas/test_utils.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import pytest
|
||||
|
||||
from app.broadcast_areas.utils import aggregate_areas
|
||||
from app.models.broadcast_message import BroadcastMessage
|
||||
from tests import broadcast_message_json
|
||||
from tests.app.broadcast_areas.custom_polygons import (
|
||||
BRISTOL,
|
||||
BURFORD,
|
||||
CHELTENHAM,
|
||||
SANTA_A,
|
||||
SKYE,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('area_ids', 'expected_area_names'), [
|
||||
(
|
||||
[
|
||||
'wd20-E05004294', # Hester’s Way, Cheltenham (electoral ward)
|
||||
'wd20-E05010981', # Painswick & Upton, Stroud (electoral ward)
|
||||
], [
|
||||
'Cheltenham', # in Gloucestershire (upper tier authority)
|
||||
'Stroud', # in Gloucestershire
|
||||
],
|
||||
),
|
||||
(
|
||||
[
|
||||
'wd20-E05004294', # Hester’s Way, Cheltenham (electoral ward)
|
||||
'wd20-E05009372', # Hackney Central (electoral ward)
|
||||
], [
|
||||
'Cheltenham', # in Gloucestershire (upper tier authority)
|
||||
'Hackney', # in Greater London* (DB doesn't know this)
|
||||
],
|
||||
),
|
||||
(
|
||||
[
|
||||
'lad20-E07000037', # High Peak (lower tier authority)
|
||||
],
|
||||
[
|
||||
'High Peak', # in Derbyshire (upper tier authority)
|
||||
]
|
||||
),
|
||||
(
|
||||
[
|
||||
'lad20-E07000037', # High Peak (lower tier authority)
|
||||
'lad20-E07000035', # Derbyshire Dales (lower tier authority)
|
||||
],
|
||||
[
|
||||
'Derbyshire Dales', # in Derbyshire (upper tier authority)
|
||||
'High Peak', # in Derbyshire
|
||||
]
|
||||
),
|
||||
(
|
||||
[
|
||||
'ctry19-E92000001', # England
|
||||
],
|
||||
[
|
||||
'England',
|
||||
]
|
||||
)
|
||||
])
|
||||
def test_aggregate_areas(
|
||||
area_ids,
|
||||
expected_area_names,
|
||||
):
|
||||
broadcast_message = BroadcastMessage(
|
||||
broadcast_message_json(area_ids=area_ids)
|
||||
)
|
||||
|
||||
assert sorted(
|
||||
area.name for area in aggregate_areas(broadcast_message.areas)
|
||||
) == expected_area_names
|
||||
Reference in New Issue
Block a user