diff --git a/app/dao/inbound_numbers_dao.py b/app/dao/inbound_numbers_dao.py index f7b28e089..3b8db6ff5 100644 --- a/app/dao/inbound_numbers_dao.py +++ b/app/dao/inbound_numbers_dao.py @@ -15,6 +15,10 @@ def dao_get_inbound_number_for_service(service_id): return InboundNumber.query.filter(InboundNumber.service_id == service_id).first() +def dao_get_inbound_number(inbound_number_id): + return InboundNumber.query.filter(InboundNumber.id == inbound_number_id).first() + + @transactional def dao_set_inbound_number_to_service(service_id, inbound_number): inbound_number.service_id = service_id @@ -23,8 +27,8 @@ def dao_set_inbound_number_to_service(service_id, inbound_number): @transactional -def dao_set_inbound_number_active_flag_for_service(service_id, active): - inbound_number = dao_get_inbound_number_for_service(service_id) +def dao_set_inbound_number_active_flag(inbound_number_id, active): + inbound_number = dao_get_inbound_number(inbound_number_id) inbound_number.active = active db.session.add(inbound_number) diff --git a/app/models.py b/app/models.py index c9123a03a..0f4fddf57 100644 --- a/app/models.py +++ b/app/models.py @@ -252,6 +252,7 @@ class InboundNumber(db.Model): 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) + updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow) def serialize(self): def serialize_service(): @@ -267,6 +268,7 @@ class InboundNumber(db.Model): "service": serialize_service() if self.service else None, "active": self.active, "created_at": self.created_at.strftime(DATETIME_FORMAT), + "updated_at": self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None, } diff --git a/migrations/versions/0114_add_inbound_numbers.py b/migrations/versions/0114_add_inbound_numbers.py index 5004cdce2..8dd78fa5d 100644 --- a/migrations/versions/0114_add_inbound_numbers.py +++ b/migrations/versions/0114_add_inbound_numbers.py @@ -2,7 +2,7 @@ Revision ID: 0114_add_inbound_numbers Revises: 0113_job_created_by_nullable -Create Date: 2017-08-03 11:08:00.970476 +Create Date: 2017-08-10 17:30:01.507694 """ @@ -22,6 +22,7 @@ def upgrade(): sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), sa.Column('active', sa.Boolean(), nullable=False), sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('number') diff --git a/tests/app/dao/test_inbound_numbers_dao.py b/tests/app/dao/test_inbound_numbers_dao.py index 69d53bd7c..4869ce84a 100644 --- a/tests/app/dao/test_inbound_numbers_dao.py +++ b/tests/app/dao/test_inbound_numbers_dao.py @@ -4,9 +4,9 @@ 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_get_inbound_number, dao_set_inbound_number_to_service, - dao_set_inbound_number_active_flag_for_service + dao_set_inbound_number_active_flag ) from app.models import InboundNumber @@ -56,15 +56,12 @@ def test_after_setting_service_id_that_inbound_number_is_unavailable( assert len(res) == 0 -def test_get_inbound_number_for_service(notify_db, notify_db_session, sample_inbound_numbers): - service = create_service(service_name='test service') - inbound_number = create_inbound_number(number='4') +def test_get_inbound_number(notify_db, notify_db_session): + inbound_number = create_inbound_number(number='1') - dao_set_inbound_number_to_service(service.id, inbound_number) + res = dao_get_inbound_number(inbound_number.id) - res = dao_get_inbound_number_for_service(service.id) - - assert res.service_id == service.id + assert res.id == inbound_number.id def test_setting_a_service_twice_will_raise_an_error(notify_db, notify_db_session): @@ -78,9 +75,6 @@ def test_setting_a_service_twice_will_raise_an_error(notify_db, notify_db_sessio with pytest.raises(IntegrityError) as e: dao_set_inbound_number_to_service(service.id, numbers[1]) - res = dao_get_inbound_number_for_service(service.id) - - assert res.service_id == service.id assert 'duplicate key value violates unique constraint' in str(e.value) @@ -89,8 +83,8 @@ def test_set_inbound_number_active_flag(notify_db, notify_db_session, sample_ser inbound_number = create_inbound_number(number='1') dao_set_inbound_number_to_service(sample_service.id, inbound_number) - dao_set_inbound_number_active_flag_for_service(sample_service.id, active=active) + dao_set_inbound_number_active_flag(inbound_number.id, active=active) - inbound_number = dao_get_inbound_number_for_service(sample_service.id) + inbound_number = dao_get_inbound_number(inbound_number.id) assert inbound_number.active is active diff --git a/tests/app/test_model.py b/tests/app/test_model.py index 66b96ba4e..b8ffcf9b4 100644 --- a/tests/app/test_model.py +++ b/tests/app/test_model.py @@ -19,7 +19,7 @@ from tests.app.conftest import ( sample_template as create_sample_template, sample_notification_with_job as create_sample_notification_with_job ) -from tests.app.db import create_notification +from tests.app.db import create_notification, create_service, create_inbound_number @pytest.mark.parametrize('mobile_number', [ @@ -218,3 +218,12 @@ def test_email_notification_serializes_with_subject(client, sample_email_templat def test_letter_notification_serializes_with_subject(client, sample_letter_template): res = sample_letter_template.serialize() assert res['subject'] == 'Template subject' + + +def test_inbound_number_serializes_with_service(client, notify_db_session): + service = create_service() + inbound_number = create_inbound_number(number='1', service_id=service.id) + serialized_inbound_number = inbound_number.serialize() + assert serialized_inbound_number.get('id') == str(inbound_number.id) + assert serialized_inbound_number.get('service').get('id') == str(inbound_number.service.id) + assert serialized_inbound_number.get('service').get('name') == inbound_number.service.name