diff --git a/app/dao/inbound_numbers_dao.py b/app/dao/inbound_numbers_dao.py index 4f682710f..e9cdeaed2 100644 --- a/app/dao/inbound_numbers_dao.py +++ b/app/dao/inbound_numbers_dao.py @@ -16,13 +16,7 @@ def dao_get_inbound_number_for_service(service_id): @transactional -def dao_allocate_inbound_number_to_service(service_id): - available_numbers = InboundNumber.query.filter( - InboundNumber.active, InboundNumber.service_id.is_(None)).all() +def dao_set_inbound_number_to_service(service_id, inbound_number): + inbound_number.service_id = service_id - if len(available_numbers) > 0: - available_numbers[0].service_id = service_id - - db.session.add(available_numbers[0]) - else: - raise IndexError('No inbound numbers available') + db.session.add(inbound_number) diff --git a/app/models.py b/app/models.py index 80e8ac805..2c4030663 100644 --- a/app/models.py +++ b/app/models.py @@ -254,11 +254,11 @@ class InboundNumber(db.Model): created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) def serialize(self): - serialized = { + return { "id": str(self.id), "number": self.number, "provider": self.provider, - "service_id": self.service_id, + "service_id": str(self.service_id), "active": self.active, "created_at": self.created_at.strftime(DATETIME_FORMAT), } diff --git a/tests/app/dao/test_inbound_numbers_dao.py b/tests/app/dao/test_inbound_numbers_dao.py index 969dccceb..333ea5d10 100644 --- a/tests/app/dao/test_inbound_numbers_dao.py +++ b/tests/app/dao/test_inbound_numbers_dao.py @@ -1,10 +1,11 @@ import pytest +from sqlalchemy.exc import IntegrityError from app.dao.inbound_numbers_dao import ( dao_get_inbound_numbers, dao_get_available_inbound_numbers, dao_get_inbound_number_for_service, - dao_allocate_inbound_number_to_service + dao_set_inbound_number_to_service ) from app.models import InboundNumber @@ -27,8 +28,9 @@ def test_get_available_inbound_numbers(notify_db, notify_db_session, sample_inbo def test_allocate_inbound_number_to_service(notify_db, notify_db_session, sample_inbound_numbers): service = create_service(service_name='test service') + numbers = dao_get_available_inbound_numbers() - dao_allocate_inbound_number_to_service(service.id) + dao_set_inbound_number_to_service(service.id, numbers[0]) res = InboundNumber.query.filter(InboundNumber.service_id == service.id).all() @@ -37,18 +39,21 @@ def test_allocate_inbound_number_to_service(notify_db, notify_db_session, sample def test_allocating_a_service_twice_will_raise_an_error(notify_db, notify_db_session, sample_inbound_numbers): + from tests.app.db import create_inbound_number + create_inbound_number(number='4', provider='mmg') service = create_service(service_name='test service') + numbers = dao_get_available_inbound_numbers() - dao_allocate_inbound_number_to_service(service.id) + dao_set_inbound_number_to_service(service.id, numbers[0]) - with pytest.raises(IndexError) as e: - dao_allocate_inbound_number_to_service(service.id) + with pytest.raises(IntegrityError) as e: + dao_set_inbound_number_to_service(service.id, numbers[1]) res = InboundNumber.query.filter(InboundNumber.service_id == service.id).all() assert len(res) == 1 assert res[0].service_id == service.id - assert str(e.value) == 'No inbound numbers available' + assert 'duplicate key value violates unique constraint' in str(e.value) def test_get_inbound_number_for_service(notify_db, notify_db_session, sample_inbound_numbers, sample_service): diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 2047f0f54..fe03e71bc 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -8,7 +8,7 @@ from sqlalchemy.exc import IntegrityError from freezegun import freeze_time from app import db from app.dao.inbound_numbers_dao import ( - dao_allocate_inbound_number_to_service, + dao_set_inbound_number_to_service, dao_get_available_inbound_numbers ) from app.dao.services_dao import ( @@ -907,6 +907,6 @@ def test_dao_allocating_inbound_number_shows_on_service(notify_db_session, sampl service = create_service(service_name='test service') - dao_allocate_inbound_number_to_service(service.id) + dao_set_inbound_number_to_service(service.id, inbound_numbers[0]) assert service.inbound_number.number == inbound_numbers[0].number diff --git a/tests/app/inbound_number/__init__.py b/tests/app/inbound_number/__init__.py new file mode 100644 index 000000000..e69de29bb