Files
notifications-api/tests/app/dao/test_inbound_numbers_dao.py
Leo Hemsted 6181c60f75 remove usage of notify_db fixture in unit tests
* notify_db fixture creates the database connection and ensures the test
  db exists and has migrations applied etc. It will run once per session
  (test run).
* notify_db_session fixture runs after your test finishes and deletes
  all non static (eg type table) data.

In unit tests that hit the database (ie: most of them), 99% of the time
we will need to use notify_db_session to ensure everything is reset. The
only time we don't need to use it is when we're querying things such as
"ensure get X works when database is empty". This is such a low
percentage of tests that it's easier for us to just use
notify_db_session every time, and ensure that all our tests run much
more consistently, at the cost of a small bit of performance when
running tests.

We used to use notify_db to access the session object for manually
adding, committing, etc. To dissuade usage of that fixture I've moved
that to the `notify_db_session`. I've then removed all uses of notify_db
that I could find in the codebase.

As a note, if you're writing a test that uses a `sample_x` fixture, all
of those fixtures rely on notify_db_session so you'll get the teardown
functionality for free. If you're just calling eg `create_x` db.py
functions, then you'll need to make you add notify_db_session fixture to
your test, even if you aren't manually accessing the session.
2022-05-04 11:36:54 +01:00

108 lines
3.8 KiB
Python

import pytest
from sqlalchemy.exc import IntegrityError
from app.dao.inbound_numbers_dao import (
dao_allocate_number_for_service,
dao_get_available_inbound_numbers,
dao_get_inbound_number_for_service,
dao_get_inbound_numbers,
dao_set_inbound_number_active_flag,
dao_set_inbound_number_to_service,
)
from app.models import InboundNumber
from tests.app.db import create_inbound_number, create_service
def test_get_inbound_numbers(notify_db_session, sample_inbound_numbers):
res = dao_get_inbound_numbers()
assert len(res) == len(sample_inbound_numbers)
assert res == sample_inbound_numbers
def test_get_available_inbound_numbers(notify_db_session):
inbound_number = create_inbound_number(number='1')
res = dao_get_available_inbound_numbers()
assert len(res) == 1
assert res[0] == inbound_number
def test_set_service_id_on_inbound_number(notify_db_session, sample_inbound_numbers):
service = create_service(service_name='test service')
numbers = dao_get_available_inbound_numbers()
dao_set_inbound_number_to_service(service.id, numbers[0])
res = InboundNumber.query.filter(InboundNumber.service_id == service.id).all()
assert len(res) == 1
assert res[0].service_id == service.id
def test_after_setting_service_id_that_inbound_number_is_unavailable(
notify_db_session, sample_inbound_numbers):
service = create_service(service_name='test service')
numbers = dao_get_available_inbound_numbers()
assert len(numbers) == 1
dao_set_inbound_number_to_service(service.id, numbers[0])
res = dao_get_available_inbound_numbers()
assert len(res) == 0
def test_setting_a_service_twice_will_raise_an_error(notify_db_session):
create_inbound_number(number='1')
create_inbound_number(number='2')
service = create_service(service_name='test service')
numbers = dao_get_available_inbound_numbers()
dao_set_inbound_number_to_service(service.id, numbers[0])
with pytest.raises(IntegrityError) as e:
dao_set_inbound_number_to_service(service.id, numbers[1])
assert 'duplicate key value violates unique constraint' in str(e.value)
@pytest.mark.parametrize("active", [True, False])
def test_set_inbound_number_active_flag(notify_db_session, sample_service, active):
inbound_number = create_inbound_number(number='1')
dao_set_inbound_number_to_service(sample_service.id, inbound_number)
dao_set_inbound_number_active_flag(sample_service.id, active=active)
inbound_number = dao_get_inbound_number_for_service(sample_service.id)
assert inbound_number.active is active
def test_dao_allocate_number_for_service(notify_db_session):
number = '078945612'
inbound_number = create_inbound_number(number=number)
service = create_service()
updated_inbound_number = dao_allocate_number_for_service(service_id=service.id, inbound_number_id=inbound_number.id)
assert service.get_inbound_number() == number
assert updated_inbound_number.service_id == service.id
def test_dao_allocate_number_for_service_raises_if_inbound_number_already_taken(notify_db_session, sample_service):
number = '078945612'
inbound_number = create_inbound_number(number=number, service_id=sample_service.id)
service = create_service(service_name="Service needs an inbound number")
with pytest.raises(Exception) as exc:
dao_allocate_number_for_service(service_id=service.id, inbound_number_id=inbound_number.id)
assert 'is not available' in str(exc.value)
def test_dao_allocate_number_for_service_raises_if_invalid_inbound_number(notify_db_session, fake_uuid):
service = create_service(service_name="Service needs an inbound number")
with pytest.raises(Exception) as exc:
dao_allocate_number_for_service(service_id=service.id, inbound_number_id=fake_uuid)
assert 'is not available' in str(exc.value)