- Added a scheduled task to create or update billing for the month, yesterday is used to calculate the start and end date for the month.

- The new task has not been added to the beat application yet.
- Added an updated_at column to the monthly billing table, we may want to only calculate from the last updated date rather than the entire month.
This commit is contained in:
Rebecca Law
2017-07-24 15:13:18 +01:00
parent 793248a74f
commit 3e2b8190b9
7 changed files with 93 additions and 12 deletions

View File

@@ -25,8 +25,8 @@ from app.celery.scheduled_tasks import (
send_scheduled_notifications,
switch_current_sms_provider_on_slow_delivery,
timeout_job_statistics,
timeout_notifications
)
timeout_notifications,
populate_monthly_billing)
from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient
from app.dao.jobs_dao import dao_get_job_by_id
from app.dao.notifications_dao import dao_get_scheduled_notifications
@@ -36,10 +36,10 @@ from app.dao.provider_details_dao import (
)
from app.models import (
Service, Template,
SMS_TYPE, LETTER_TYPE
)
SMS_TYPE, LETTER_TYPE,
MonthlyBilling)
from app.utils import get_london_midnight_in_utc
from tests.app.db import create_notification, create_service, create_template, create_job
from tests.app.db import create_notification, create_service, create_template, create_job, create_rate
from tests.app.conftest import (
sample_job as create_sample_job,
sample_notification_history as create_notification_history,
@@ -98,6 +98,8 @@ def test_should_have_decorated_tasks_functions():
'remove_transformed_dvla_files'
assert delete_dvla_response_files_older_than_seven_days.__wrapped__.__name__ == \
'delete_dvla_response_files_older_than_seven_days'
assert populate_monthly_billing.__wrapped__.__name__ == \
'populate_monthly_billing'
@pytest.fixture(scope='function')
@@ -607,3 +609,30 @@ def test_delete_dvla_response_files_older_than_seven_days_does_not_remove_files(
delete_dvla_response_files_older_than_seven_days()
remove_s3_mock.assert_not_called()
@freeze_time("2017-07-12 02:00:00")
def test_populate_monthly_billing(sample_template):
yesterday = datetime(2017, 7, 11, 13, 30)
create_rate(datetime(2016, 1, 1), 0.0123, 'sms')
create_notification(template=sample_template, status='delivered', created_at=yesterday)
create_notification(template=sample_template, status='delivered', created_at=yesterday - timedelta(days=1))
create_notification(template=sample_template, status='delivered', created_at=yesterday + timedelta(days=1))
# not included in billing
create_notification(template=sample_template, status='delivered', created_at=yesterday - timedelta(days=30))
assert len(MonthlyBilling.query.all()) == 0
populate_monthly_billing()
monthly_billing = MonthlyBilling.query.all()
assert len(monthly_billing) == 1
assert monthly_billing[0].service_id == sample_template.service_id
assert monthly_billing[0].year == 2017
assert monthly_billing[0].month == 'July'
assert monthly_billing[0].notification_type == 'sms'
assert len(monthly_billing[0].monthly_totals) == 1
assert sorted(monthly_billing[0].monthly_totals[0]) == sorted({'international': False,
'rate_multiplier': 1,
'billing_units': 3,
'rate': 0.0123,
'total_cost': 0.0369})

View File

@@ -1,8 +1,12 @@
from datetime import datetime
from app.dao.monthly_billing_dao import create_or_update_monthly_billing_sms, get_monthly_billing_sms
from app.dao.monthly_billing_dao import (
create_or_update_monthly_billing_sms,
get_monthly_billing_sms,
get_service_ids_that_need_sms_billing_populated
)
from app.models import MonthlyBilling
from tests.app.db import create_notification, create_rate
from tests.app.db import create_notification, create_rate, create_service, create_template
def test_add_monthly_billing(sample_template):
@@ -105,3 +109,18 @@ def assert_monthly_billing(monthly_billing, year, month, service_id, expected_le
assert monthly_billing.service_id == service_id
assert len(monthly_billing.monthly_totals) == expected_len
assert sorted(monthly_billing.monthly_totals[0]) == sorted(first_row)
def test_get_service_id():
service_1 = create_service(service_name="Service One")
template_1 = create_template(service=service_1)
service_2 = create_service(service_name="Service Two")
template_2 = create_template(service=service_2)
create_notification(template=template_1, created_at=datetime(2017, 6, 30, 13, 30), status='delivered')
create_notification(template=template_1, created_at=datetime(2017, 7, 1, 14, 30), status='delivered')
create_notification(template=template_2, created_at=datetime(2017, 7, 15, 13, 30))
create_notification(template=template_2, created_at=datetime(2017, 7, 31, 13, 30))
services = get_service_ids_that_need_sms_billing_populated(start_date=datetime(2017, 7, 1),
end_date=datetime(2017, 7, 16))
expected_services = [service_1.id, service_2.id]
assert sorted([x.service_id for x in services]) == sorted(expected_services)