Check that all areas are in the library

We should assume to start with that areas come either from the library
or from the JSON, and not a combination of the two.
This commit is contained in:
Chris Hill-Scott
2021-01-27 14:32:35 +00:00
parent 5ae53b625b
commit 926ada2f21
2 changed files with 34 additions and 3 deletions

View File

@@ -78,9 +78,17 @@ class BroadcastMessage(JSONModel):
@property
def areas(self):
return self.get_areas(
areas=self._dict['areas']
) or CustomBroadcastAreas(
library_areas = self.get_areas(areas=self._dict['areas'])
if library_areas:
if len(library_areas) != len(self._dict['areas']):
raise RuntimeError(
f'BroadcastMessage has {len(self._dict["areas"])} areas '
f'but {len(library_areas)} found in the library'
)
return library_areas
return CustomBroadcastAreas(
areas=self._dict['areas'],
polygons=self._dict['simple_polygons'],
)

View File

@@ -1,3 +1,5 @@
import pytest
from app.models.broadcast_message import BroadcastMessage
from tests import broadcast_message_json
@@ -45,3 +47,24 @@ def test_content_comes_from_attribute_not_template(fake_uuid):
created_by_id=fake_uuid,
))
assert broadcast_message.content == 'This is a test'
def test_raises_for_missing_areas(fake_uuid):
broadcast_message = BroadcastMessage(broadcast_message_json(
id_=fake_uuid,
service_id=fake_uuid,
template_id=fake_uuid,
status='draft',
created_by_id=fake_uuid,
areas=[
'wd20-E05009372',
'something else',
],
))
with pytest.raises(RuntimeError) as exception:
broadcast_message.areas
assert str(exception.value) == (
'BroadcastMessage has 2 areas but 1 found in the library'
)