2017-06-02 12:21:12 +01:00
|
|
|
from datetime import datetime, timedelta
|
2017-05-31 14:49:14 +01:00
|
|
|
|
|
|
|
|
from freezegun import freeze_time
|
|
|
|
|
|
2017-06-02 12:21:12 +01:00
|
|
|
from app.dao.inbound_sms_dao import (
|
|
|
|
|
dao_get_inbound_sms_for_service,
|
|
|
|
|
dao_count_inbound_sms_for_service,
|
2017-06-06 17:11:59 +01:00
|
|
|
delete_inbound_sms_created_more_than_a_week_ago,
|
2017-11-09 15:23:45 +00:00
|
|
|
dao_get_inbound_sms_by_id,
|
2018-03-22 12:41:17 +00:00
|
|
|
dao_get_paginated_inbound_sms_for_service_for_public_api,
|
2017-11-09 15:23:45 +00:00
|
|
|
dao_get_paginated_inbound_sms_for_service
|
2017-06-02 12:21:12 +01:00
|
|
|
)
|
2017-05-31 14:49:14 +01:00
|
|
|
from tests.app.db import create_inbound_sms, create_service
|
|
|
|
|
|
2017-06-02 12:21:12 +01:00
|
|
|
from app.models import InboundSms
|
|
|
|
|
|
2017-05-31 14:49:14 +01:00
|
|
|
|
|
|
|
|
def test_get_all_inbound_sms(sample_service):
|
|
|
|
|
inbound = create_inbound_sms(sample_service)
|
|
|
|
|
|
|
|
|
|
res = dao_get_inbound_sms_for_service(sample_service.id)
|
|
|
|
|
assert len(res) == 1
|
|
|
|
|
assert res[0] == inbound
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def test_get_all_inbound_sms_by_page(sample_service):
|
|
|
|
|
inbound = create_inbound_sms(sample_service)
|
|
|
|
|
|
|
|
|
|
res = dao_get_paginated_inbound_sms_for_service(sample_service.id)
|
|
|
|
|
assert len(res.items) == 1
|
|
|
|
|
assert res.has_next is False
|
|
|
|
|
assert res.per_page == 50
|
|
|
|
|
assert res.items[0] == inbound
|
|
|
|
|
|
|
|
|
|
|
2017-05-31 14:49:14 +01:00
|
|
|
def test_get_all_inbound_sms_when_none_exist(sample_service):
|
|
|
|
|
res = dao_get_inbound_sms_for_service(sample_service.id)
|
|
|
|
|
assert len(res) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_all_inbound_sms_limits_and_orders(sample_service):
|
|
|
|
|
with freeze_time('2017-01-01'):
|
2017-11-28 17:21:21 +00:00
|
|
|
create_inbound_sms(sample_service)
|
2017-05-31 14:49:14 +01:00
|
|
|
with freeze_time('2017-01-03'):
|
|
|
|
|
three = create_inbound_sms(sample_service)
|
|
|
|
|
with freeze_time('2017-01-02'):
|
|
|
|
|
two = create_inbound_sms(sample_service)
|
|
|
|
|
|
|
|
|
|
res = dao_get_inbound_sms_for_service(sample_service.id, limit=2)
|
|
|
|
|
assert len(res) == 2
|
|
|
|
|
assert res[0] == three
|
|
|
|
|
assert res[0].created_at == datetime(2017, 1, 3)
|
|
|
|
|
assert res[1] == two
|
|
|
|
|
assert res[1].created_at == datetime(2017, 1, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_all_inbound_sms_filters_on_service(notify_db_session):
|
|
|
|
|
service_one = create_service(service_name='one')
|
|
|
|
|
service_two = create_service(service_name='two')
|
|
|
|
|
|
|
|
|
|
sms_one = create_inbound_sms(service_one)
|
2017-11-28 17:21:21 +00:00
|
|
|
create_inbound_sms(service_two)
|
2017-05-31 14:49:14 +01:00
|
|
|
|
|
|
|
|
res = dao_get_inbound_sms_for_service(service_one.id)
|
|
|
|
|
assert len(res) == 1
|
|
|
|
|
assert res[0] == sms_one
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_count_inbound_sms_for_service(notify_db_session):
|
|
|
|
|
service_one = create_service(service_name='one')
|
|
|
|
|
service_two = create_service(service_name='two')
|
|
|
|
|
|
|
|
|
|
create_inbound_sms(service_one)
|
|
|
|
|
create_inbound_sms(service_one)
|
|
|
|
|
create_inbound_sms(service_two)
|
|
|
|
|
|
|
|
|
|
assert dao_count_inbound_sms_for_service(service_one.id) == 2
|
2017-06-02 12:21:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2017-01-01 12:00:00")
|
|
|
|
|
def test_should_delete_inbound_sms_older_than_seven_days(sample_service):
|
|
|
|
|
older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)
|
|
|
|
|
create_inbound_sms(sample_service, created_at=older_than_seven_days)
|
|
|
|
|
delete_inbound_sms_created_more_than_a_week_ago()
|
|
|
|
|
|
|
|
|
|
assert len(InboundSms.query.all()) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2017-01-01 12:00:00")
|
|
|
|
|
def test_should_not_delete_inbound_sms_before_seven_days(sample_service):
|
|
|
|
|
yesterday = datetime.utcnow() - timedelta(days=1)
|
|
|
|
|
just_before_seven_days = datetime.utcnow() - timedelta(days=6, hours=23, minutes=59, seconds=59)
|
|
|
|
|
older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)
|
|
|
|
|
|
|
|
|
|
create_inbound_sms(sample_service, created_at=yesterday)
|
|
|
|
|
create_inbound_sms(sample_service, created_at=just_before_seven_days)
|
|
|
|
|
create_inbound_sms(sample_service, created_at=older_than_seven_days)
|
|
|
|
|
|
|
|
|
|
delete_inbound_sms_created_more_than_a_week_ago()
|
|
|
|
|
|
|
|
|
|
assert len(InboundSms.query.all()) == 2
|
2017-06-06 17:11:59 +01:00
|
|
|
|
|
|
|
|
|
2017-12-21 17:18:35 +00:00
|
|
|
def test_get_inbound_sms_by_id_returns(sample_service):
|
|
|
|
|
inbound_sms = create_inbound_sms(service=sample_service)
|
|
|
|
|
inbound_from_db = dao_get_inbound_sms_by_id(inbound_sms.service.id, inbound_sms.id)
|
2017-11-09 15:23:45 +00:00
|
|
|
|
2017-12-21 17:18:35 +00:00
|
|
|
assert inbound_sms == inbound_from_db
|
2017-11-09 15:23:45 +00:00
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def test_dao_get_paginated_inbound_sms_for_service_for_public_api(sample_service):
|
2017-12-21 17:18:35 +00:00
|
|
|
inbound_sms = create_inbound_sms(service=sample_service)
|
2018-03-22 12:41:17 +00:00
|
|
|
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(inbound_sms.service.id)
|
2017-11-09 15:23:45 +00:00
|
|
|
|
2017-12-21 17:18:35 +00:00
|
|
|
assert inbound_sms == inbound_from_db[0]
|
2017-11-09 15:23:45 +00:00
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_return_only_for_service(sample_service):
|
2017-12-21 17:18:35 +00:00
|
|
|
inbound_sms = create_inbound_sms(service=sample_service)
|
2017-11-09 15:23:45 +00:00
|
|
|
another_service = create_service(service_name='another service')
|
|
|
|
|
another_inbound_sms = create_inbound_sms(another_service)
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(inbound_sms.service.id)
|
2017-11-09 15:23:45 +00:00
|
|
|
|
2017-12-21 17:18:35 +00:00
|
|
|
assert inbound_sms in inbound_from_db
|
2017-11-09 15:23:45 +00:00
|
|
|
assert another_inbound_sms not in inbound_from_db
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_no_inbound_sms_returns_empty_list(sample_service):
|
|
|
|
|
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(sample_service.id)
|
2017-11-09 15:23:45 +00:00
|
|
|
|
|
|
|
|
assert inbound_from_db == []
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_page_size_returns_correct_size(sample_service):
|
2017-11-09 15:23:45 +00:00
|
|
|
inbound_sms_list = [
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
]
|
|
|
|
|
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(
|
2017-11-09 15:23:45 +00:00
|
|
|
sample_service.id,
|
|
|
|
|
older_than=reversed_inbound_sms[1].id,
|
|
|
|
|
page_size=2
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert len(inbound_from_db) == 2
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_returns_correct_list(sample_service):
|
2017-11-09 15:23:45 +00:00
|
|
|
inbound_sms_list = [
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
]
|
|
|
|
|
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(
|
2017-11-09 15:23:45 +00:00
|
|
|
sample_service.id,
|
|
|
|
|
older_than=reversed_inbound_sms[1].id,
|
|
|
|
|
page_size=2
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expected_inbound_sms = reversed_inbound_sms[2:]
|
|
|
|
|
|
|
|
|
|
assert expected_inbound_sms == inbound_from_db
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_end_returns_empty_list(sample_service):
|
2017-11-09 15:23:45 +00:00
|
|
|
inbound_sms_list = [
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
create_inbound_sms(sample_service),
|
|
|
|
|
]
|
|
|
|
|
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
|
2017-06-06 17:11:59 +01:00
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(
|
2017-11-09 15:23:45 +00:00
|
|
|
sample_service.id,
|
|
|
|
|
older_than=reversed_inbound_sms[1].id,
|
|
|
|
|
page_size=2
|
|
|
|
|
)
|
2017-06-06 17:11:59 +01:00
|
|
|
|
2017-11-09 15:23:45 +00:00
|
|
|
assert inbound_from_db == []
|