Remove argument unpacking from get_areas

Making it only callable in one way is just less stuff to understand.
This commit is contained in:
Chris Hill-Scott
2021-08-06 10:06:26 +01:00
parent 6c766c24b6
commit 5e1b96a3a7
4 changed files with 17 additions and 39 deletions

View File

@@ -227,18 +227,11 @@ class BroadcastAreaLibraries(SerialisedModelCollection, GetItemByIdMixin):
def __init__(self):
self.items = BroadcastAreasRepository().get_libraries()
def get_areas(self, *area_ids):
# allow people to call `get_areas('a', 'b') or get_areas(['a', 'b'])`
if len(area_ids) == 1 and isinstance(area_ids[0], list):
area_ids = area_ids[0]
def get_areas(self, area_ids):
areas = BroadcastAreasRepository().get_areas(area_ids)
return [BroadcastArea(area) for area in areas]
def get_areas_with_simple_polygons(self, *area_ids):
if len(area_ids) == 1 and isinstance(area_ids[0], list):
area_ids = area_ids[0]
def get_areas_with_simple_polygons(self, area_ids):
areas = BroadcastAreasRepository().get_areas_with_simple_polygons(area_ids)
return [BroadcastArea.from_row_with_simple_polygons(area) for area in areas]

View File

@@ -287,7 +287,7 @@ def choose_broadcast_sub_area(service_id, broadcast_message_id, library_slug, ar
broadcast_message_id,
service_id=current_service.id,
)
area = BroadcastMessage.libraries.get_areas(area_slug)[0]
area = BroadcastMessage.libraries.get_areas([area_slug])[0]
back_link = _get_broadcast_sub_area_back_link(service_id, broadcast_message_id, library_slug)

View File

@@ -18,9 +18,9 @@ from app.notify_client.broadcast_message_api_client import (
broadcast_message_api_client,
)
ESTIMATED_AREA_OF_LARGEST_UK_COUNTY = broadcast_area_libraries.get_areas(
ESTIMATED_AREA_OF_LARGEST_UK_COUNTY = broadcast_area_libraries.get_areas([
'ctyua19-E10000023' # North Yorkshire
)[0].polygons.estimated_area
])[0].polygons.estimated_area
class BroadcastMessage(JSONModel):
@@ -203,7 +203,7 @@ class BroadcastMessage(JSONModel):
def get_areas(self, areas):
return broadcast_area_libraries.get_areas(
*areas
areas
)
def get_simple_polygons(self, areas):

View File

@@ -77,40 +77,25 @@ def test_loads_areas_from_libraries(id):
assert (
broadcast_area_libraries.get('ctry19').get(id)
) == (
broadcast_area_libraries.get_areas(id)[0]
broadcast_area_libraries.get_areas([id])[0]
)
def test_get_names_of_areas():
areas = broadcast_area_libraries.get_areas(
areas = broadcast_area_libraries.get_areas([
'ctry19-W92000004',
'lad20-W06000014',
'ctry19-E92000001',
)
])
assert [area.name for area in sorted(areas)] == [
'England', 'Vale of Glamorgan', 'Wales',
]
def test_get_areas_accepts_lists():
areas_from_list = broadcast_area_libraries.get_areas(
[
'ctry19-W92000004',
'ctry19-E92000001',
]
)
areas_from_args = broadcast_area_libraries.get_areas(
'ctry19-W92000004',
'ctry19-E92000001',
)
assert len(areas_from_args) == len(areas_from_list) == 2
assert areas_from_args == areas_from_list
def test_has_polygons():
england = broadcast_area_libraries.get_areas('ctry19-E92000001')[0]
scotland = broadcast_area_libraries.get_areas('ctry19-S92000003')[0]
england = broadcast_area_libraries.get_areas(['ctry19-E92000001'])[0]
scotland = broadcast_area_libraries.get_areas(['ctry19-S92000003'])[0]
assert len(england.polygons) == 35
assert len(scotland.polygons) == 195
@@ -130,7 +115,7 @@ def test_polygons_are_enclosed():
def test_lat_long_order():
england = broadcast_area_libraries.get_areas('ctry19-E92000001')[0]
england = broadcast_area_libraries.get_areas(['ctry19-E92000001'])[0]
lat_long = england.polygons.as_coordinate_pairs_lat_long
long_lat = england.polygons.as_coordinate_pairs_long_lat
@@ -212,13 +197,13 @@ def test_every_area_has_count_of_phones(library):
))
def test_count_of_phones_for_all_levels(area_id, area_name, expected_count):
area = broadcast_area_libraries.get_areas(area_id)[0]
area = broadcast_area_libraries.get_areas([area_id])[0]
assert area.name == area_name
assert area.count_of_phones == expected_count
def test_city_of_london_counts_are_not_derived_from_population():
city_of_london = broadcast_area_libraries.get_areas('lad20-E09000001')[0]
city_of_london = broadcast_area_libraries.get_areas(['lad20-E09000001'])[0]
assert city_of_london.name == 'City of London'
assert len(city_of_london.sub_areas) == len(CITY_OF_LONDON.WARDS) == 25
@@ -315,7 +300,7 @@ def test_phone_density(
area, expected_phones_per_square_mile,
):
assert close_enough(
broadcast_area_libraries.get_areas(area)[0].phone_density,
broadcast_area_libraries.get_areas([area])[0].phone_density,
expected_phones_per_square_mile,
)
@@ -351,11 +336,11 @@ def test_estimated_bleed(
area, expected_bleed_in_m, expected_bleed_in_degrees,
):
assert close_enough(
broadcast_area_libraries.get_areas(area)[0].estimated_bleed_in_m,
broadcast_area_libraries.get_areas([area])[0].estimated_bleed_in_m,
expected_bleed_in_m,
)
assert close_enough(
broadcast_area_libraries.get_areas(area)[0].estimated_bleed_in_degrees,
broadcast_area_libraries.get_areas([area])[0].estimated_bleed_in_degrees,
expected_bleed_in_degrees,
)