From 194f54c38fc13f23e182e31b301452551f9bda59 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Fri, 27 Aug 2021 13:07:22 +0100 Subject: [PATCH] Add missing tests for old format areas This was (sort of) missed in [1], but it hasn't caused a problem because the code to create/update broadcasts will populate areas in the old ("areas") and the new ("ids") formats anyway [2]. However, we're about to remove create/update support code, so we need to have something in place to cope with old and new format data until we backfill old broadcasts with the new format [3]. [1]: https://github.com/alphagov/notifications-api/pull/3312 [2]: https://github.com/alphagov/notifications-api/pull/3312/files#diff-6be38b59e387630a0c6b8fc60312b7ba53ba9f36c54594fa5690646f286dd2b9R141 [3]: https://github.com/alphagov/notifications-admin/pull/4004/commits/9667433b7e2e23540eaeb25edf1a558955e97d7e --- app/models.py | 6 ++--- tests/app/broadcast_message/test_rest.py | 28 ++++++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/models.py b/app/models.py index 6895cd029..3908a69aa 100644 --- a/app/models.py +++ b/app/models.py @@ -2339,7 +2339,7 @@ class BroadcastMessage(db.Model): # TEMPORARY: while we repurpose "areas" areas_2 = dict(self.areas) areas_2["simple_polygons"] = areas_2.get("simple_polygons", []) - areas_2["ids"] = areas_2.pop("areas", []) + areas_2["ids"] = areas_2.pop("areas", areas_2.get("ids", [])) return { 'id': str(self.id), @@ -2355,8 +2355,8 @@ class BroadcastMessage(db.Model): # TEMPORARY: switch to this so we can repurpose "areas" 'areas_2': areas_2, - 'areas': self.areas.get("areas", []), - 'simple_polygons': self.areas.get("simple_polygons", []), + 'areas': areas_2["ids"], + 'simple_polygons': areas_2["simple_polygons"], 'status': self.status, diff --git a/tests/app/broadcast_message/test_rest.py b/tests/app/broadcast_message/test_rest.py index d2f50776e..cfe6bc3ff 100644 --- a/tests/app/broadcast_message/test_rest.py +++ b/tests/app/broadcast_message/test_rest.py @@ -19,7 +19,16 @@ from tests.app.db import ( ) -def test_get_broadcast_message(admin_request, sample_broadcast_service): +# TEMPORARY: while we repurpose "areas" +@pytest.mark.parametrize("area_data", [ + {"areas": ["place A", "region B"]}, + {"ids": ["place A", "region B"]}, +]) +def test_get_broadcast_message( + area_data, + admin_request, + sample_broadcast_service +): t = create_template( sample_broadcast_service, BROADCAST_TYPE, @@ -28,7 +37,7 @@ def test_get_broadcast_message(admin_request, sample_broadcast_service): bm = create_broadcast_message( t, areas={ - "areas": ['place A', 'region B'], + **area_data, "simple_polygons": [[[50.1, 1.2], [50.12, 1.2], [50.13, 1.2]]], }, personalisation={ @@ -51,16 +60,27 @@ def test_get_broadcast_message(admin_request, sample_broadcast_service): assert response['created_at'] is not None assert response['starts_at'] is None assert response['areas'] == ['place A', 'region B'] + assert response['simple_polygons'] == [[[50.1, 1.2], [50.12, 1.2], [50.13, 1.2]]] assert response['areas_2']['ids'] == ['place A', 'region B'] + assert response['areas_2']['simple_polygons'] == [[[50.1, 1.2], [50.12, 1.2], [50.13, 1.2]]] assert response['personalisation'] == {'thing': 'test'} -def test_get_broadcast_message_without_template(admin_request, sample_broadcast_service): +# TEMPORARY: while we repurpose "areas" +@pytest.mark.parametrize("area_data", [ + {"areas": ["place A", "region B"]}, + {"ids": ["place A", "region B"]}, +]) +def test_get_broadcast_message_without_template( + area_data, + admin_request, + sample_broadcast_service +): bm = create_broadcast_message( service=sample_broadcast_service, content='emergency broadcast content', areas={ - "areas": ['place A', 'region B'], + **area_data, "simple_polygons": [[[50.1, 1.2], [50.12, 1.2], [50.13, 1.2]]], }, )