From c57f33a02d684600d721b59d7b15828a0202b362 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Tue, 10 Jul 2018 11:31:18 +0100 Subject: [PATCH] Use real letter_rates in tests Added the letter_rate table to the list of tables which does not get deleted after each test run and changed the tests to use the real letter rates. Also removed the letter rate DAO since this was only being used in tests, so was no longer needed. --- app/dao/letter_rate_dao.py | 23 ----------- tests/app/billing/test_billing.py | 5 +-- tests/app/celery/test_reporting_tasks.py | 31 +++------------ tests/app/dao/test_ft_billing_dao.py | 7 +--- tests/app/dao/test_letter_rate_dao.py | 41 -------------------- tests/app/dao/test_monthly_billing.py | 12 +++--- tests/app/dao/test_notification_usage_dao.py | 5 +-- tests/app/db.py | 23 ----------- tests/conftest.py | 3 +- 9 files changed, 18 insertions(+), 132 deletions(-) delete mode 100644 app/dao/letter_rate_dao.py delete mode 100644 tests/app/dao/test_letter_rate_dao.py diff --git a/app/dao/letter_rate_dao.py b/app/dao/letter_rate_dao.py deleted file mode 100644 index e69001a44..000000000 --- a/app/dao/letter_rate_dao.py +++ /dev/null @@ -1,23 +0,0 @@ -from app import db -from app.dao.dao_utils import transactional -from app.models import LetterRate - - -@transactional -def dao_create_letter_rate(letter_rate): - db.session.add(letter_rate) - - -def get_letter_rates_for_daterange(date, crown, sheet_count, post_class='second'): - rates = LetterRate.query.filter( - LetterRate.start_date <= date - ).filter((LetterRate.end_date == None) | # noqa - (LetterRate.end_date > date) - ).filter( - LetterRate.crown == crown - ).filter( - LetterRate.sheet_count == sheet_count - ).filter( - LetterRate.post_class == post_class - ).all() - return rates diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index f931edbde..02de27604 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -17,7 +17,6 @@ from tests.app.db import ( create_rate, create_monthly_billing_entry, create_annual_billing, - create_letter_rate, create_template, create_service, create_ft_billing @@ -47,8 +46,6 @@ def test_get_yearly_billing_summary_returns_correct_breakdown(client, sample_tem template=sample_template, created_at=IN_JUN_2016, billable_units=2, rate_multiplier=3, status='delivered' ) - create_letter_rate(crown=False, start_date=IN_MAY_2016, end_date=IN_JUN_2016) - create_letter_rate(crown=False, start_date=IN_JUN_2016, rate=0.41) letter_template = create_template(service=sample_template.service, template_type=LETTER_TYPE) create_notification(template=letter_template, created_at=IN_MAY_2016, status='delivered', billable_units=1) @@ -76,7 +73,7 @@ def test_get_yearly_billing_summary_returns_correct_breakdown(client, sample_tem 'notification_type': LETTER_TYPE, 'billing_units': 2, 'rate': 0, - 'letter_total': 0.72 + 'letter_total': 0.66 }) _assert_dict_equals(resp_json[2], { 'notification_type': SMS_TYPE, diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index eb0292f74..ecd4002f1 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -11,7 +11,6 @@ from app.models import ( ) from decimal import Decimal import pytest -from app.dao.letter_rate_dao import dao_create_letter_rate from app.models import LetterRate, Rate from app import db from freezegun import freeze_time @@ -316,38 +315,20 @@ def test_create_nightly_billing_consolidate_from_3_days_delta( assert records[-1].bst_date == date(2018, 1, 14) -def test_get_rate_for_letter_latest(notify_db, notify_db_session): - letter_rate = LetterRate(start_date=datetime(2017, 12, 1), - rate=Decimal(0.33), - crown=True, - sheet_count=1, - post_class='second') - - dao_create_letter_rate(letter_rate) - letter_rate = LetterRate(start_date=datetime(2016, 12, 1), - end_date=datetime(2017, 12, 1), - rate=Decimal(0.30), - crown=True, - sheet_count=1, - post_class='second') - dao_create_letter_rate(letter_rate) - +def test_get_rate_for_letter_latest(notify_db_session): non_letter_rates = [(r.notification_type, r.valid_from, r.rate) for r in Rate.query.order_by(desc(Rate.valid_from)).all()] - letter_rates = [(r.start_date, r.crown, r.sheet_count, r.rate) for r in - LetterRate.query.order_by(desc(LetterRate.start_date)).all()] + + # letter rates should be passed into the get_rate function as a tuple of start_date, crown, sheet_count & rate + new_letter_rate = (datetime(2017, 12, 1), True, 1, Decimal(0.33)) + old_letter_rate = (datetime(2016, 12, 1), True, 1, Decimal(0.30)) + letter_rates = [new_letter_rate, old_letter_rate] rate = get_rate(non_letter_rates, letter_rates, LETTER_TYPE, datetime(2018, 1, 1), True, 1) assert rate == Decimal(0.33) def test_get_rate_for_sms_and_email(notify_db, notify_db_session): - letter_rate = LetterRate(start_date=datetime(2017, 12, 1), - rate=Decimal(0.33), - crown=True, - sheet_count=1, - post_class='second') - dao_create_letter_rate(letter_rate) sms_rate = Rate(valid_from=datetime(2017, 12, 1), rate=Decimal(0.15), notification_type=SMS_TYPE) diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 018cc2290..d286da29f 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -18,7 +18,6 @@ from tests.app.db import ( create_template, create_notification, create_rate, - create_letter_rate ) @@ -223,18 +222,16 @@ def test_get_rates_for_billing(notify_db_session): create_rate(start_date=datetime.utcnow(), value=12, notification_type='email') create_rate(start_date=datetime.utcnow(), value=22, notification_type='sms') create_rate(start_date=datetime.utcnow(), value=33, notification_type='email') - create_letter_rate(start_date=datetime.utcnow()) non_letter_rates, letter_rates = get_rates_for_billing() assert len(non_letter_rates) == 3 - assert len(letter_rates) == 1 + assert len(letter_rates) == 10 def test_get_rate(notify_db_session): create_rate(start_date=datetime.utcnow(), value=1.2, notification_type='email') create_rate(start_date=datetime.utcnow(), value=2.2, notification_type='sms') create_rate(start_date=datetime.utcnow(), value=3.3, notification_type='email') - create_letter_rate(start_date=datetime.utcnow(), rate=4.4) non_letter_rates, letter_rates = get_rates_for_billing() rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type='sms', date=datetime.utcnow()) @@ -245,7 +242,7 @@ def test_get_rate(notify_db_session): date=datetime.utcnow()) assert rate == 2.2 - assert letter_rate == Decimal('4.4') + assert letter_rate == Decimal('0.3') def test_fetch_monthly_billing_for_year(notify_db_session): diff --git a/tests/app/dao/test_letter_rate_dao.py b/tests/app/dao/test_letter_rate_dao.py deleted file mode 100644 index f3fd0d139..000000000 --- a/tests/app/dao/test_letter_rate_dao.py +++ /dev/null @@ -1,41 +0,0 @@ -from datetime import datetime - -from decimal import Decimal - -from app.dao.letter_rate_dao import dao_create_letter_rate, get_letter_rates_for_daterange -from app.models import LetterRate - - -def test_dao_create_letter_rate(notify_db_session): - letter_rate = LetterRate(start_date=datetime(2017, 12, 1), - rate=0.33, - crown=True, - sheet_count=1, - post_class='second') - - dao_create_letter_rate(letter_rate) - - rates = LetterRate.query.all() - assert len(rates) == 1 - - -def test_get_letter_rates_for_daterange(notify_db_session): - letter_rate = LetterRate(start_date=datetime(2017, 12, 1), - rate=0.33, - crown=True, - sheet_count=1, - post_class='second') - - dao_create_letter_rate(letter_rate) - letter_rate = LetterRate(start_date=datetime(2016, 12, 1), - end_date=datetime(2017, 12, 1), - rate=0.30, - crown=True, - sheet_count=1, - post_class='second') - - dao_create_letter_rate(letter_rate) - - results = get_letter_rates_for_daterange(date=datetime(2017, 12, 2), crown=True, sheet_count=1, post_class='second') - assert len(results) == 1 - assert results[0].rate == Decimal('0.33') diff --git a/tests/app/dao/test_monthly_billing.py b/tests/app/dao/test_monthly_billing.py index e41057501..f422a0b76 100644 --- a/tests/app/dao/test_monthly_billing.py +++ b/tests/app/dao/test_monthly_billing.py @@ -18,7 +18,7 @@ from tests.app.db import ( create_service, create_template, create_monthly_billing_entry, - create_letter_rate) +) FEB_2016_MONTH_START = datetime(2016, 2, 1) FEB_2016_MONTH_END = datetime(2016, 2, 29, 23, 59, 59, 99999) @@ -154,7 +154,6 @@ def test_add_monthly_billing_for_single_month_populates_correctly( ): create_rate(start_date=JAN_2017_MONTH_START, value=0.0158, notification_type=SMS_TYPE) letter_template = sample_letter_template(sample_template.service) - create_letter_rate(crown=False) create_notification( template=sample_template, created_at=JAN_2017_MONTH_START, billable_units=1, rate_multiplier=2, status='delivered' @@ -201,8 +200,8 @@ def test_add_monthly_billing_for_single_month_populates_correctly( "billing_units": 1, "rate_multiplier": 1, "international": False, - "rate": 0.31, - "total_cost": 1 * 0.31 + "rate": 0.33, + "total_cost": 1 * 0.33 }) @@ -286,7 +285,6 @@ def test_add_monthly_billing_with_multiple_rates_populate_correctly( create_notification(template=sample_email_template, created_at=JAN_2017_MONTH_START, status='delivered') create_notification(template=letter_template, created_at=JAN_2017_MONTH_START, status='delivered', billable_units=1) - create_letter_rate(start_date=JAN_2017_MONTH_START, crown=False) create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=JAN_2017_MONTH_START) @@ -329,8 +327,8 @@ def test_add_monthly_billing_with_multiple_rates_populate_correctly( "billing_units": 1, "rate_multiplier": 1, "international": False, - "rate": 0.31, - "total_cost": 0.31 + "rate": 0.33, + "total_cost": 0.33 }) diff --git a/tests/app/dao/test_notification_usage_dao.py b/tests/app/dao/test_notification_usage_dao.py index 66a790365..4e1bb27ab 100644 --- a/tests/app/dao/test_notification_usage_dao.py +++ b/tests/app/dao/test_notification_usage_dao.py @@ -14,7 +14,7 @@ from app.models import ( Rate, SMS_TYPE, ) -from tests.app.db import create_notification, create_rate, create_letter_rate, create_template, create_service +from tests.app.db import create_notification, create_rate, create_template, create_service def test_get_rates_for_daterange(notify_db, notify_db_session): @@ -217,7 +217,6 @@ def test_get_monthly_billing_data_where_start_date_before_rate_returns_empty( def test_billing_letter_data_per_month_query( notify_db_session ): - create_letter_rate() service = create_service() template = create_template(service=service, template_type='letter') create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21), @@ -232,5 +231,5 @@ def test_billing_letter_data_per_month_query( end_date=datetime(2017, 2, 28)) assert len(results) == 1 - assert results[0].rate == 0.31 + assert results[0].rate == 0.3 assert results[0].billing_units == 3 diff --git a/tests/app/db.py b/tests/app/db.py index 65cf0e4e6..ead311efc 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -32,7 +32,6 @@ from app.models import ( SMS_TYPE, KEY_TYPE_NORMAL, AnnualBilling, - LetterRate, InvitedOrganisationUser, FactBilling, FactNotificationStatus, @@ -475,28 +474,6 @@ def create_annual_billing( return annual_billing -def create_letter_rate( - start_date=datetime(2017, 1, 1, 00, 00, 00), - end_date=None, - sheet_count=1, - rate=0.31, - crown=True, - post_class='second' -): - rate = LetterRate( - start_date=start_date, - end_date=end_date, - sheet_count=sheet_count, - rate=rate, - crown=crown, - post_class=post_class - ) - db.session.add(rate) - db.session.commit() - - return rate - - def create_organisation(name='test_org_1', active=True): data = { 'name': name, diff --git a/tests/conftest.py b/tests/conftest.py index 7ed1ba895..3797ab6d9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -105,7 +105,8 @@ def notify_db_session(notify_db): "notification_status_types", "service_permission_types", "auth_type", - "invite_status_type"]: + "invite_status_type", + "letter_rates"]: notify_db.engine.execute(tbl.delete()) notify_db.session.commit()