Remove references to monthly_billing table from api

This commit is contained in:
Pea Tyczynska
2018-07-23 15:14:37 +01:00
parent c0f309a2a6
commit ca2b350a99
10 changed files with 15 additions and 1040 deletions

View File

@@ -4,18 +4,12 @@ import json
import pytest
from app.billing.rest import _transform_billing_for_month_sms
from app.dao.monthly_billing_dao import (
create_or_update_monthly_billing,
get_monthly_billing_by_notification_type,
)
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, FactBilling
from app.models import FactBilling
from app.dao.date_util import get_current_financial_year_start_year, get_month_start_and_end_date_in_utc
from app.dao.annual_billing_dao import dao_get_free_sms_fragment_limit_for_year
from tests.app.db import (
create_notification,
create_rate,
create_monthly_billing_entry,
create_annual_billing,
create_template,
create_service,
@@ -36,253 +30,6 @@ def _assert_dict_equals(actual, expected_dict):
assert actual == expected_dict
def test_get_yearly_billing_summary_returns_correct_breakdown(client, sample_template):
create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE)
create_notification(
template=sample_template, created_at=IN_MAY_2016,
billable_units=1, rate_multiplier=2, status='delivered'
)
create_notification(
template=sample_template, created_at=IN_JUN_2016,
billable_units=2, rate_multiplier=3, status='delivered'
)
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)
create_notification(template=letter_template, created_at=IN_JUN_2016, status='delivered', billable_units=1)
create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_MAY_2016)
create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_JUN_2016)
response = client.get(
'/service/{}/billing/yearly-usage-summary?year=2016'.format(sample_template.service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json) == 3
_assert_dict_equals(resp_json[0], {
'notification_type': EMAIL_TYPE,
'billing_units': 0,
'rate': 0,
'letter_total': 0
})
_assert_dict_equals(resp_json[1], {
'notification_type': LETTER_TYPE,
'billing_units': 2,
'rate': 0,
'letter_total': 0.66
})
_assert_dict_equals(resp_json[2], {
'notification_type': SMS_TYPE,
'billing_units': 8,
'rate': 0.12,
'letter_total': 0
})
def test_get_yearly_billing_usage_breakdown_returns_400_if_missing_year(client, sample_service):
response = client.get(
'/service/{}/billing/yearly-usage-summary'.format(sample_service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 400
assert json.loads(response.get_data(as_text=True)) == {
'message': 'No valid year provided', 'result': 'error'
}
def test_get_yearly_usage_by_month_returns_400_if_missing_year(client, sample_service):
response = client.get(
'/service/{}/billing/monthly-usage'.format(sample_service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 400
assert json.loads(response.get_data(as_text=True)) == {
'message': 'No valid year provided', 'result': 'error'
}
def test_get_yearly_usage_by_month_returns_empty_list_if_no_usage(client, sample_template):
create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE)
response = client.get(
'/service/{}/billing/monthly-usage?year=2016'.format(sample_template.service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 200
results = json.loads(response.get_data(as_text=True))
assert results == []
def test_get_yearly_usage_by_month_returns_correctly(client, sample_template):
create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE)
create_notification(
template=sample_template, created_at=IN_MAY_2016,
billable_units=1, rate_multiplier=2, status='delivered'
)
create_notification(
template=sample_template, created_at=IN_JUN_2016,
billable_units=2, rate_multiplier=3, status='delivered'
)
create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_MAY_2016)
create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_JUN_2016)
response = client.get(
'/service/{}/billing/monthly-usage?year=2016'.format(sample_template.service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
_assert_dict_equals(resp_json[0], {
'billing_units': 0,
'month': 'May',
'notification_type': LETTER_TYPE,
'rate': 0
})
_assert_dict_equals(resp_json[1], {
'billing_units': 2,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12
})
_assert_dict_equals(resp_json[2], {
'billing_units': 0,
'month': 'June',
'notification_type': LETTER_TYPE,
'rate': 0
})
_assert_dict_equals(resp_json[3], {
'billing_units': 6,
'month': 'June',
'notification_type': SMS_TYPE,
'rate': 0.12
})
def test_transform_billing_for_month_returns_empty_if_no_monthly_totals(sample_service):
create_monthly_billing_entry(
service=sample_service,
monthly_totals=[],
start_date=APR_2016_MONTH_START,
end_date=APR_2016_MONTH_END,
notification_type=SMS_TYPE
)
transformed_billing_data = _transform_billing_for_month_sms(get_monthly_billing_by_notification_type(
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
))
_assert_dict_equals(transformed_billing_data, {
'notification_type': SMS_TYPE,
'billing_units': 0,
'month': 'April',
'rate': 0,
})
def test_transform_billing_for_month_formats_monthly_totals_correctly(sample_service):
create_monthly_billing_entry(
service=sample_service,
monthly_totals=[{
"billing_units": 12,
"rate": 0.0158,
"rate_multiplier": 5,
"total_cost": 2.1804,
"international": False
}],
start_date=APR_2016_MONTH_START,
end_date=APR_2016_MONTH_END,
notification_type=SMS_TYPE
)
transformed_billing_data = _transform_billing_for_month_sms(get_monthly_billing_by_notification_type(
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
))
_assert_dict_equals(transformed_billing_data, {
'notification_type': SMS_TYPE,
'billing_units': 60,
'month': 'April',
'rate': 0.0158,
})
def test_transform_billing_sums_billable_units(sample_service):
create_monthly_billing_entry(
service=sample_service,
monthly_totals=[{
'billing_units': 1321,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 1
}, {
'billing_units': 1,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 1
}],
start_date=APR_2016_MONTH_START,
end_date=APR_2016_MONTH_END,
notification_type=SMS_TYPE
)
transformed_billing_data = _transform_billing_for_month_sms(get_monthly_billing_by_notification_type(
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
))
_assert_dict_equals(transformed_billing_data, {
'notification_type': SMS_TYPE,
'billing_units': 1322,
'month': 'April',
'rate': 0.12,
})
def test_transform_billing_calculates_with_different_rate_multipliers(sample_service):
create_monthly_billing_entry(
service=sample_service,
monthly_totals=[{
'billing_units': 1321,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 1
}, {
'billing_units': 1,
'international': False,
'month': 'May',
'notification_type': SMS_TYPE,
'rate': 0.12,
'rate_multiplier': 3
}],
start_date=APR_2016_MONTH_START,
end_date=APR_2016_MONTH_END,
notification_type=SMS_TYPE
)
transformed_billing_data = _transform_billing_for_month_sms(get_monthly_billing_by_notification_type(
sample_service.id, APR_2016_MONTH_START, SMS_TYPE
))
_assert_dict_equals(transformed_billing_data, {
'notification_type': SMS_TYPE,
'billing_units': 1324,
'month': 'April',
'rate': 0.12,
})
def test_create_update_free_sms_fragment_limit_invalid_schema(client, sample_service):
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
@@ -480,21 +227,6 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(client, notify_db_session):
assert len(ft_email) == 0
def test_compare_ft_billing_to_monthly_billing(client, notify_db_session):
service = set_up_yearly_data()
monthly_billing_response = client.get('/service/{}/billing/monthly-usage?year=2016'.format(service.id),
headers=[create_authorization_header()])
ft_billing_response = client.get('service/{}/billing/ft-monthly-usage?year=2016'.format(service.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
monthly_billing_json_resp = json.loads(monthly_billing_response.get_data(as_text=True))
ft_billing_json_resp = json.loads(ft_billing_response.get_data(as_text=True))
assert monthly_billing_json_resp == ft_billing_json_resp
def set_up_yearly_data():
service = create_service()
sms_template = create_template(service=service, template_type="sms")
@@ -527,55 +259,9 @@ def set_up_yearly_data():
notification_type='letter',
rate=0.33)
start_date, end_date = get_month_start_and_end_date_in_utc(datetime(2016, int(mon), 1))
create_monthly_billing_entry(service=service, start_date=start_date,
end_date=end_date,
notification_type='sms',
monthly_totals=[
{"rate": 0.0162, "international": False,
"rate_multiplier": 1, "billing_units": int(d),
"total_cost": 0.0162 * int(d)},
{"rate": 0.0162, "international": False,
"rate_multiplier": 2, "billing_units": int(d),
"total_cost": 0.0162 * int(d)}]
)
create_monthly_billing_entry(service=service, start_date=start_date,
end_date=end_date,
notification_type='email',
monthly_totals=[
{"rate": 0, "international": False,
"rate_multiplier": 1, "billing_units": int(d),
"total_cost": 0}]
)
create_monthly_billing_entry(service=service, start_date=start_date,
end_date=end_date,
notification_type='letter',
monthly_totals=[
{"rate": 0.33, "international": False,
"rate_multiplier": 1, "billing_units": int(d),
"total_cost": 0.33 * int(d)}]
)
return service
def test_get_yearly_billing_usage_summary_from_ft_billing_compare_to_monthly_billing(
client, notify_db_session
):
service = set_up_yearly_data()
monthly_billing_response = client.get('/service/{}/billing/yearly-usage-summary?year=2016'.format(service.id),
headers=[create_authorization_header()])
ft_billing_response = client.get('service/{}/billing/ft-yearly-usage-summary?year=2016'.format(service.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
monthly_billing_json_resp = json.loads(monthly_billing_response.get_data(as_text=True))
ft_billing_json_resp = json.loads(ft_billing_response.get_data(as_text=True))
assert len(monthly_billing_json_resp) == 3
assert len(ft_billing_json_resp) == 3
for i in range(0, 3):
assert sorted(monthly_billing_json_resp[i]) == sorted(ft_billing_json_resp[i])
def test_get_yearly_billing_usage_summary_from_ft_billing_returns_400_if_missing_year(client, sample_service):
response = client.get(
'/service/{}/billing/ft-yearly-usage-summary'.format(sample_service.id),