Refactor code to add updated_at

This commit is contained in:
Ken Tsang
2017-08-10 17:51:47 +01:00
parent 2cfe85a2af
commit cfabab0785
5 changed files with 28 additions and 18 deletions

View File

@@ -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)

View File

@@ -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,
}

View File

@@ -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')

View File

@@ -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

View File

@@ -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