Files
notifications-api/tests/app/dao/test_email_branding_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

59 lines
1.8 KiB
Python

from app.dao.email_branding_dao import (
dao_get_email_branding_by_id,
dao_get_email_branding_by_name,
dao_get_email_branding_options,
dao_update_email_branding,
)
from app.models import EmailBranding
from tests.app.db import create_email_branding
def test_get_email_branding_options_gets_all_email_branding(notify_db_session):
email_branding_1 = create_email_branding(name='test_email_branding_1')
email_branding_2 = create_email_branding(name='test_email_branding_2')
email_branding = dao_get_email_branding_options()
assert len(email_branding) == 2
assert email_branding_1 == email_branding[0]
assert email_branding_2 == email_branding[1]
def test_get_email_branding_by_id_gets_correct_email_branding(notify_db_session):
email_branding = create_email_branding()
email_branding_from_db = dao_get_email_branding_by_id(email_branding.id)
assert email_branding_from_db == email_branding
def test_get_email_branding_by_name_gets_correct_email_branding(notify_db_session):
email_branding = create_email_branding(name="Crystal Gems")
email_branding_from_db = dao_get_email_branding_by_name("Crystal Gems")
assert email_branding_from_db == email_branding
def test_update_email_branding(notify_db_session):
updated_name = 'new name'
create_email_branding()
email_branding = EmailBranding.query.all()
assert len(email_branding) == 1
assert email_branding[0].name != updated_name
dao_update_email_branding(email_branding[0], name=updated_name)
email_branding = EmailBranding.query.all()
assert len(email_branding) == 1
assert email_branding[0].name == updated_name
def test_email_branding_has_no_domain(notify_db_session):
create_email_branding()
email_branding = EmailBranding.query.all()
assert not hasattr(email_branding, 'domain')