Speed up usage DAO tests with minimal test data

This slowed me down when making changes to the DAO functions. It's
really not necessary to do 3 * 367 DB insertions for both tests.

Before:

    time pytest tests/app/dao/test_ft_billing_dao.py -k for_year
    ...
    pytest tests/app/dao/test_ft_billing_dao.py -k for_year  3.95s user 0.40s system 62% cpu 6.971 total

After:

    time pytest tests/app/dao/test_ft_billing_dao.py -k for_year
    ...
    pytest tests/app/dao/test_ft_billing_dao.py -k for_year  1.84s user 0.25s system 69% cpu 3.006 total
This commit is contained in:
Ben Thorner
2022-04-29 10:30:45 +01:00
parent c3bc1d0df9
commit 1fab73ef9e

View File

@@ -52,12 +52,9 @@ def set_up_yearly_data():
create_ft_billing(bst_date=dt, template=email_template, rate=0, billable_unit=0)
create_ft_billing(bst_date=dt, template=letter_template, rate=0.31, postage='second')
start_date = date(2016, 4, 1)
end_date = date(2017, 4, 1)
for n in range((end_date - start_date).days):
dt = start_date + timedelta(days=n)
# a selection of dates that represent the extreme ends of the financial year
# and some arbitrary dates in between
for dt in (date(2016, 4, 1), date(2016, 4, 29), date(2017, 2, 6), date(2017, 3, 31)):
create_ft_billing(bst_date=dt, template=sms_template, rate=0.162)
create_ft_billing(bst_date=dt, template=email_template, rate=0, billable_unit=0)
create_ft_billing(bst_date=dt, template=letter_template, rate=0.30, postage='second')
@@ -424,15 +421,15 @@ def test_get_rate_for_letters_when_page_count_is_zero(notify_db_session):
def test_fetch_monthly_billing_for_year(notify_db_session):
service = set_up_yearly_data()
create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2016)
create_annual_billing(service_id=service.id, free_sms_fragment_limit=1, financial_year_start=2016)
results = fetch_monthly_billing_for_year(service.id, 2016)
assert len(results) == 36
assert len(results) == 9 # 3 billed months for each type
assert str(results[0].month) == "2016-04-01"
assert results[0].notification_type == 'email'
assert results[0].notifications_sent == 30
assert results[0].billable_units == 30
assert results[0].notifications_sent == 2
assert results[0].billable_units == 2
assert results[0].chargeable_units == 0
assert results[0].rate == Decimal('0')
assert results[0].cost == Decimal('0')
@@ -441,27 +438,27 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert str(results[1].month) == "2016-04-01"
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 30
assert results[1].billable_units == 30
assert results[1].chargeable_units == 30
assert results[1].notifications_sent == 2
assert results[1].billable_units == 2
assert results[1].chargeable_units == 2
assert results[1].rate == Decimal('0.30')
assert results[1].cost == Decimal('9')
assert results[1].cost == Decimal('0.60')
assert results[1].free_allowance_used == 0
assert results[1].charged_units == 30
assert results[1].charged_units == 2
assert str(results[2].month) == "2016-04-01"
assert results[2].notification_type == 'sms'
assert results[2].notifications_sent == 30
assert results[2].billable_units == 30
assert results[2].chargeable_units == 30
assert results[2].notifications_sent == 2
assert results[2].billable_units == 2
assert results[2].chargeable_units == 2
assert results[2].rate == Decimal('0.162')
# free allowance is 10, so (30 - 10) * 0.162
assert results[2].cost == Decimal('3.24')
assert results[2].free_allowance_used == 10
assert results[2].charged_units == 20
# free allowance is 1
assert results[2].cost == Decimal('0.162')
assert results[2].free_allowance_used == 1
assert results[2].charged_units == 1
assert str(results[3].month) == "2016-05-01"
assert str(results[35].month) == "2017-03-01"
assert str(results[3].month) == "2017-02-01"
assert str(results[8].month) == "2017-03-01"
def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
@@ -542,8 +539,8 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert len(results) == 3
assert results[0].notification_type == 'email'
assert results[0].notifications_sent == 365
assert results[0].billable_units == 365
assert results[0].notifications_sent == 4
assert results[0].billable_units == 4
assert results[0].chargeable_units == 0
assert results[0].rate == Decimal('0')
assert results[0].cost == Decimal('0')
@@ -551,21 +548,21 @@ def test_fetch_billing_totals_for_year(notify_db_session):
assert results[0].charged_units == 0
assert results[1].notification_type == 'letter'
assert results[1].notifications_sent == 365
assert results[1].billable_units == 365
assert results[1].chargeable_units == 365
assert results[1].notifications_sent == 4
assert results[1].billable_units == 4
assert results[1].chargeable_units == 4
assert results[1].rate == Decimal('0.3')
assert results[1].cost == Decimal('109.5')
assert results[1].cost == Decimal('1.2')
assert results[1].free_allowance_used == 0
assert results[1].charged_units == 365
assert results[1].charged_units == 4
assert results[2].notification_type == 'sms'
assert results[2].notifications_sent == 365
assert results[2].billable_units == 365
assert results[2].chargeable_units == 365
assert results[2].notifications_sent == 4
assert results[2].billable_units == 4
assert results[2].chargeable_units == 4
assert results[2].rate == Decimal('0.162')
assert results[2].cost == Decimal('0')
assert results[2].free_allowance_used == 365
assert results[2].free_allowance_used == 4
assert results[2].charged_units == 0
@@ -580,7 +577,7 @@ def test_fetch_billing_totals_for_year_uses_current_annual_billing(notify_db_ses
if result.notification_type == 'sms'
)
assert result.chargeable_units == 365
assert result.chargeable_units == 4
assert result.cost > 0