Files
notifications-admin/tests/app/broadcast_areas/test_broadcast_area.py
T

189 lines
5.3 KiB
Python
Raw Normal View History

2020-08-10 10:46:31 +01:00
import pytest
from app.broadcast_areas import (
2020-07-24 12:54:22 +01:00
BroadcastAreasRepository,
broadcast_area_libraries,
)
def test_loads_libraries():
assert [
2020-07-31 13:21:21 +01:00
(library.id, library.name, library.is_group) for library in sorted(broadcast_area_libraries)
] == [
(
2020-08-13 12:25:22 +01:00
'ctyua16',
2020-07-31 13:21:21 +01:00
'Counties and Unitary Authorities in England and Wales',
False,
),
(
2020-08-13 12:25:22 +01:00
'ctry19',
'Countries',
2020-07-31 13:21:21 +01:00
False,
),
(
2020-08-13 12:25:22 +01:00
'wd20-lad20',
'Local authorities',
2020-07-31 13:21:21 +01:00
True,
),
]
def test_loads_areas_from_library():
assert [
(area.id, area.name) for area in sorted(
2020-08-13 12:25:22 +01:00
broadcast_area_libraries.get('ctry19')
)
] == [
2020-08-13 12:25:22 +01:00
('ctry19-E92000001', 'England'),
('ctry19-N92000002', 'Northern Ireland'),
('ctry19-S92000003', 'Scotland'),
('ctry19-W92000004', 'Wales'),
]
def test_examples():
2020-08-13 12:25:22 +01:00
countries = broadcast_area_libraries.get('ctry19').get_examples()
2020-08-12 12:17:40 +01:00
assert countries == 'England, Northern Ireland, Scotland, and Wales'
2020-08-11 11:33:27 +01:00
counties = broadcast_area_libraries.get(
2020-08-13 12:25:22 +01:00
'ctyua16',
2020-08-11 11:33:27 +01:00
).get_examples()
2020-08-12 12:17:40 +01:00
assert counties == 'Barking and Dagenham, Barnet, Barnsley, and 170 more…'
2020-08-11 11:33:27 +01:00
wards = broadcast_area_libraries.get(
2020-08-13 12:25:22 +01:00
'wd20-lad20',
2020-08-11 11:33:27 +01:00
).get_examples()
2020-08-12 12:17:40 +01:00
assert wards == 'Aberdeen City, Aberdeenshire, Adur, and 375 more…'
@pytest.mark.parametrize('id', (
2020-08-13 12:25:22 +01:00
'ctry19-E92000001',
'ctry19-N92000002',
'ctry19-S92000003',
'ctry19-W92000004',
pytest.param('mercia', marks=pytest.mark.xfail(raises=KeyError)),
))
def test_loads_areas_from_libraries(id):
assert (
2020-08-13 12:25:22 +01:00
broadcast_area_libraries.get('ctry19').get(id)
) == (
broadcast_area_libraries.get_areas(id)[0]
)
def test_get_names_of_areas():
areas = broadcast_area_libraries.get_areas(
2020-08-13 12:25:22 +01:00
'ctry19-W92000004',
'lad20-W06000014',
'ctry19-E92000001',
'ctyua16-E10000012',
2020-07-31 13:21:21 +01:00
)
assert [area.name for area in sorted(areas)] == [
'England', 'Essex', 'Vale of Glamorgan', 'Wales',
]
def test_get_areas_accepts_lists():
areas_from_list = broadcast_area_libraries.get_areas(
2020-08-10 13:19:16 +01:00
[
2020-08-13 12:25:22 +01:00
'ctry19-W92000004',
'ctyua16-W06000014',
'ctry19-E92000001',
'ctyua16-E10000012',
2020-08-10 13:19:16 +01:00
]
)
areas_from_args = broadcast_area_libraries.get_areas(
2020-08-13 12:25:22 +01:00
'ctry19-W92000004',
'ctyua16-W06000014',
'ctry19-E92000001',
'ctyua16-E10000012',
)
2020-08-13 12:25:22 +01:00
assert len(areas_from_args) == len(areas_from_list) == 4
assert areas_from_args == areas_from_list
def test_has_polygons():
assert len(
2020-08-13 12:25:22 +01:00
broadcast_area_libraries.get_polygons_for_areas_long_lat('ctry19-E92000001')
) == 35
assert len(
2020-08-13 12:25:22 +01:00
broadcast_area_libraries.get_polygons_for_areas_long_lat('ctry19-S92000003')
) == 195
assert len(
2020-07-31 13:21:21 +01:00
broadcast_area_libraries.get_polygons_for_areas_long_lat(
2020-08-13 12:25:22 +01:00
'ctry19-E92000001',
'ctry19-S92000003',
2020-07-31 13:21:21 +01:00
)
) == 35 + 195 == 230
assert len(
2020-07-31 13:21:21 +01:00
broadcast_area_libraries.get_polygons_for_areas_lat_long(
2020-08-13 12:25:22 +01:00
'ctry19-E92000001',
'ctry19-S92000003',
2020-07-31 13:21:21 +01:00
)
) == 35 + 195 == 230
2020-08-13 12:25:22 +01:00
assert broadcast_area_libraries.get_polygons_for_areas_lat_long('ctry19-E92000001')[0][0] == [
55.811085, -2.034358 # https://goo.gl/maps/wsf2LUWzYinwydMk8
]
def test_polygons_are_enclosed_unless_asked_not_to_be():
2020-08-13 12:25:22 +01:00
england = broadcast_area_libraries.get('ctry19').get('ctry19-E92000001')
assert len(england.polygons) == len(england.unenclosed_polygons)
first_polygon = england.polygons[0]
assert first_polygon[0] != first_polygon[1] != first_polygon[2]
assert first_polygon[0] == first_polygon[-1]
first_polygon_unenclosed = england.unenclosed_polygons[0]
assert first_polygon_unenclosed[0] == first_polygon[0]
assert first_polygon_unenclosed[-1] != first_polygon[-1]
assert first_polygon_unenclosed[-1] == first_polygon[-2]
def test_lat_long_order():
2020-08-13 12:25:22 +01:00
lat_long = broadcast_area_libraries.get_polygons_for_areas_lat_long('ctry19-E92000001')
long_lat = broadcast_area_libraries.get_polygons_for_areas_long_lat('ctry19-E92000001')
assert len(lat_long[0]) == len(long_lat[0]) == 2082 # Coordinates in polygon
assert len(lat_long[0][0]) == len(long_lat[0][0]) == 2 # Axes in coordinates
assert lat_long[0][0] == list(reversed(long_lat[0][0]))
2020-07-24 12:54:22 +01:00
def test_includes_electoral_wards():
2020-08-13 12:25:22 +01:00
areas = broadcast_area_libraries.get_areas(['wd20-E05009289'])
2020-07-24 12:54:22 +01:00
assert len(areas) == 1
2020-07-31 14:30:42 +01:00
def test_electoral_wards_are_groupable_cardiff():
2020-08-13 12:25:22 +01:00
areas = broadcast_area_libraries.get_areas(['lad20-W06000015'])
2020-07-31 14:30:42 +01:00
assert len(areas) == 1
cardiff = areas[0]
assert len(cardiff.sub_areas) == 29
def test_electoral_wards_are_groupable_ealing():
2020-08-13 12:25:22 +01:00
areas = broadcast_area_libraries.get_areas(['lad20-E09000009'])
2020-07-31 14:30:42 +01:00
assert len(areas) == 1
ealing = areas[0]
assert len(ealing.sub_areas) == 23
def test_repository_has_all_libraries():
repo = BroadcastAreasRepository()
libraries = repo.get_libraries()
2020-08-12 17:40:08 +01:00
assert len(libraries) == 3
assert [
'Counties and Unitary Authorities in England and Wales',
'Countries',
2020-08-13 12:25:22 +01:00
'Local authorities',
2020-07-31 13:21:21 +01:00
] == sorted([name for _, name, _is_group in libraries])