From f41e0f05ec3e7c86f273812dd8cb5759fcdd0ab3 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Wed, 26 Sep 2018 15:09:13 +0100 Subject: [PATCH 1/2] Make postage column part of ft_billing primary key Now that the postage column is populated and there are no null values, it can be added to the composite primary key of ft_billing. --- app/models.py | 2 +- migrations/versions/0235_add_postage_to_pk.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/0235_add_postage_to_pk.py diff --git a/app/models.py b/app/models.py index 0752f8197..8702b0573 100644 --- a/app/models.py +++ b/app/models.py @@ -1811,7 +1811,7 @@ class FactBilling(db.Model): rate_multiplier = db.Column(db.Integer(), nullable=False, primary_key=True) international = db.Column(db.Boolean, nullable=False, primary_key=True) rate = db.Column(db.Numeric(), nullable=False, primary_key=True) - postage = db.Column(db.String) + postage = db.Column(db.String, nullable=False, primary_key=True) billable_units = db.Column(db.Integer(), nullable=True) notifications_sent = db.Column(db.Integer(), nullable=True) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) diff --git a/migrations/versions/0235_add_postage_to_pk.py b/migrations/versions/0235_add_postage_to_pk.py new file mode 100644 index 000000000..072f2646e --- /dev/null +++ b/migrations/versions/0235_add_postage_to_pk.py @@ -0,0 +1,39 @@ +""" + +Revision ID: 0235_add_postage_to_pk +Revises: 0234_ft_billing_postage +Create Date: 2018-09-28 15:39:21.115358 + +""" +from alembic import op +import sqlalchemy as sa + + +revision = '0235_add_postage_to_pk' +down_revision = '0234_ft_billing_postage' + + +def upgrade(): + op.drop_constraint('ft_billing_pkey', 'ft_billing', type_='primary') + op.create_primary_key('ft_billing_pkey', 'ft_billing', ['bst_date', + 'template_id', + 'service_id', + 'notification_type', + 'provider', + 'rate_multiplier', + 'international', + 'rate', + 'postage']) + + +def downgrade(): + op.drop_constraint('ft_billing_pkey', 'ft_billing', type_='primary') + op.alter_column('ft_billing', 'postage', nullable=True) + op.create_primary_key('ft_billing_pkey', 'ft_billing', ['bst_date', + 'template_id', + 'service_id', + 'notification_type', + 'provider', + 'rate_multiplier', + 'international', + 'rate']) From 71d28035ddc6daec4b64164d01cdd863abd6f49a Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 28 Sep 2018 16:32:18 +0100 Subject: [PATCH 2/2] Update ft_billing with real postage data * Changed update_fact_billing DAO function to update the table with the real data for postage instead of hard-coding in 'second'. * Added a test for the create nightly billing task to test that rows with different postage are being inserted correctly. --- app/dao/fact_billing_dao.py | 2 +- tests/app/celery/test_reporting_tasks.py | 46 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index a8ebb850c..19769cbb3 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -237,7 +237,7 @@ def update_fact_billing(data, process_day): process_day, data.crown, data.letter_page_count, - "second") + data.postage) billing_record = create_billing_record(data, rate, process_day) table = FactBilling.__table__ ''' diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index 8da479555..32f7bb5cc 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -177,6 +177,52 @@ def test_create_nightly_billing_different_sent_by( assert record.rate_multiplier == 1.0 +def test_create_nightly_billing_different_letter_postage( + notify_db_session, + sample_letter_template, + mocker): + yesterday = datetime.now() - timedelta(days=1) + mocker.patch('app.dao.fact_billing_dao.get_rate', side_effect=mocker_get_rate) + + for i in range(2): + create_notification( + created_at=yesterday, + template=sample_letter_template, + status='delivered', + sent_by='dvla', + billable_units=2, + postage='first' + ) + create_notification( + created_at=yesterday, + template=sample_letter_template, + status='delivered', + sent_by='dvla', + billable_units=2, + postage='second' + ) + + records = FactBilling.query.all() + assert len(records) == 0 + # Celery expects the arguments to be a string or primitive type. + yesterday_str = datetime.strftime(yesterday, "%Y-%m-%d") + create_nightly_billing(yesterday_str) + + records = FactBilling.query.order_by('postage').all() + assert len(records) == 2 + assert records[0].notification_type == LETTER_TYPE + assert records[0].bst_date == datetime.date(yesterday) + assert records[0].postage == 'first' + assert records[0].notifications_sent == 2 + assert records[0].billable_units == 4 + + assert records[1].notification_type == LETTER_TYPE + assert records[1].bst_date == datetime.date(yesterday) + assert records[1].postage == 'second' + assert records[1].notifications_sent == 1 + assert records[1].billable_units == 2 + + def test_create_nightly_billing_letter( sample_service, sample_letter_template,