From 3c392596a3232f4266f9a99be401fbbc4c50183a Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Fri, 4 Aug 2017 12:13:10 +0100 Subject: [PATCH] Add backref in InboundNumber model --- app/models.py | 12 ++++++++- tests/app/conftest.py | 11 ++++++++- tests/app/dao/test_inbound_numbers_dao.py | 30 ++++++----------------- tests/app/dao/test_services_dao.py | 14 +++++++++++ 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/app/models.py b/app/models.py index d81dfa4c7..80e8ac805 100644 --- a/app/models.py +++ b/app/models.py @@ -249,10 +249,20 @@ class InboundNumber(db.Model): number = db.Column(db.String(11), unique=True, nullable=False) provider = db.Column(db.String(), nullable=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), unique=True, index=True, nullable=True) - service = db.relationship('Service') + service = db.relationship(Service, backref=db.backref("inbound_number", uselist=False)) active = db.Column(db.Boolean, index=False, unique=False, nullable=False, default=True) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) + def serialize(self): + serialized = { + "id": str(self.id), + "number": self.number, + "provider": self.provider, + "service_id": self.service_id, + "active": self.active, + "created_at": self.created_at.strftime(DATETIME_FORMAT), + } + class ServicePermission(db.Model): __tablename__ = "service_permissions" diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 30a9d4210..3d6402da2 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -39,7 +39,7 @@ from app.dao.invited_user_dao import save_invited_user from app.dao.provider_rates_dao import create_provider_rates from app.clients.sms.firetext import FiretextClient from tests import create_authorization_header -from tests.app.db import create_user, create_template, create_notification, create_api_key +from tests.app.db import create_user, create_template, create_notification, create_api_key, create_inbound_number @pytest.yield_fixture @@ -983,6 +983,15 @@ def sample_provider_rate(notify_db, notify_db_session, valid_from=None, rate=Non ) +@pytest.fixture +def sample_inbound_numbers(notify_db, notify_db_session, sample_service): + inbound_numbers = [] + inbound_numbers.append(create_inbound_number(number='1', provider='mmg')) + inbound_numbers.append(create_inbound_number(number='2', provider='mmg', active=False)) + inbound_numbers.append(create_inbound_number(number='3', provider='firetext', service_id=sample_service.id)) + return inbound_numbers + + @pytest.fixture def restore_provider_details(notify_db, notify_db_session): """ diff --git a/tests/app/dao/test_inbound_numbers_dao.py b/tests/app/dao/test_inbound_numbers_dao.py index 9299ddd52..969dccceb 100644 --- a/tests/app/dao/test_inbound_numbers_dao.py +++ b/tests/app/dao/test_inbound_numbers_dao.py @@ -8,24 +8,10 @@ from app.dao.inbound_numbers_dao import ( ) from app.models import InboundNumber -from tests.app.db import create_inbound_number, create_service +from tests.app.db import create_service -@pytest.fixture -def service_1(notify_db, notify_db_session): - return create_service() - - -@pytest.fixture -def sample_inbound_numbers(notify_db, notify_db_session, service_1): - inbound_numbers = [] - inbound_numbers.append(create_inbound_number(number='1', provider='mmg')) - inbound_numbers.append(create_inbound_number(number='2', provider='mmg', active=False)) - inbound_numbers.append(create_inbound_number(number='3', provider='firetext', service_id=service_1.id)) - return inbound_numbers - - -def test_get_inbound_numbers(notify_db, notify_db_session, sample_inbound_numbers, service_1): +def test_get_inbound_numbers(notify_db, notify_db_session, sample_inbound_numbers): res = dao_get_inbound_numbers() assert len(res) == 3 @@ -39,8 +25,7 @@ def test_get_available_inbound_numbers(notify_db, notify_db_session, sample_inbo assert res[0] == sample_inbound_numbers[0] -def test_allocate_inbound_number_to_service( - notify_db, notify_db_session, sample_inbound_numbers): +def test_allocate_inbound_number_to_service(notify_db, notify_db_session, sample_inbound_numbers): service = create_service(service_name='test service') dao_allocate_inbound_number_to_service(service.id) @@ -51,8 +36,7 @@ def test_allocate_inbound_number_to_service( assert res[0].service_id == service.id -def test_allocating_a_service_twice_will_raise_an_error( - notify_db, notify_db_session, sample_inbound_numbers): +def test_allocating_a_service_twice_will_raise_an_error(notify_db, notify_db_session, sample_inbound_numbers): service = create_service(service_name='test service') dao_allocate_inbound_number_to_service(service.id) @@ -67,8 +51,8 @@ def test_allocating_a_service_twice_will_raise_an_error( assert str(e.value) == 'No inbound numbers available' -def test_get_inbound_number_for_service(notify_db, notify_db_session, sample_inbound_numbers, service_1): - res = dao_get_inbound_number_for_service(service_1.id) +def test_get_inbound_number_for_service(notify_db, notify_db_session, sample_inbound_numbers, sample_service): + res = dao_get_inbound_number_for_service(sample_service.id) assert len(res) == 1 - assert res[0] == sample_inbound_numbers[2] + assert res[0].service_id == sample_service.id diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index ee4f4f912..2047f0f54 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -7,6 +7,10 @@ from sqlalchemy.orm.exc import FlushError, NoResultFound 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_get_available_inbound_numbers +) from app.dao.services_dao import ( dao_create_service, dao_add_user_to_service, @@ -896,3 +900,13 @@ def test_dao_fetch_services_by_sms_sender(notify_db_session): services = dao_fetch_services_by_sms_sender('foo') assert {foo1.id, foo2.id} == {x.id for x in services} + + +def test_dao_allocating_inbound_number_shows_on_service(notify_db_session, sample_inbound_numbers): + inbound_numbers = dao_get_available_inbound_numbers() + + service = create_service(service_name='test service') + + dao_allocate_inbound_number_to_service(service.id) + + assert service.inbound_number.number == inbound_numbers[0].number