mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-04 16:20:06 -04:00
* 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.
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
import uuid
|
|
|
|
import pytest
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
from app.dao.letter_branding_dao import (
|
|
dao_create_letter_branding,
|
|
dao_get_all_letter_branding,
|
|
dao_get_letter_branding_by_id,
|
|
dao_update_letter_branding,
|
|
)
|
|
from app.models import LetterBranding
|
|
from tests.app.db import create_letter_branding
|
|
|
|
|
|
def test_dao_get_letter_branding_by_id(notify_db_session):
|
|
letter_branding = create_letter_branding()
|
|
result = dao_get_letter_branding_by_id(letter_branding.id)
|
|
|
|
assert result == letter_branding
|
|
|
|
|
|
def test_dao_get_letter_brand_by_id_raises_exception_if_does_not_exist(notify_db_session):
|
|
with pytest.raises(expected_exception=SQLAlchemyError):
|
|
dao_get_letter_branding_by_id(uuid.uuid4())
|
|
|
|
|
|
def test_dao_get_all_letter_branding(notify_db_session):
|
|
hm_gov = create_letter_branding()
|
|
test_branding = create_letter_branding(
|
|
name='test branding', filename='test-branding',
|
|
)
|
|
|
|
results = dao_get_all_letter_branding()
|
|
|
|
assert hm_gov in results
|
|
assert test_branding in results
|
|
assert len(results) == 2
|
|
|
|
|
|
def test_dao_get_all_letter_branding_returns_empty_list_if_no_brands_exist(notify_db_session):
|
|
assert dao_get_all_letter_branding() == []
|
|
|
|
|
|
def test_dao_create_letter_branding(notify_db_session):
|
|
data = {
|
|
'name': 'test-logo',
|
|
'filename': 'test-logo'
|
|
}
|
|
assert LetterBranding.query.count() == 0
|
|
dao_create_letter_branding(LetterBranding(**data))
|
|
|
|
assert LetterBranding.query.count() == 1
|
|
|
|
new_letter_branding = LetterBranding.query.first()
|
|
assert new_letter_branding.name == data['name']
|
|
assert new_letter_branding.filename == data['name']
|
|
|
|
|
|
def test_dao_update_letter_branding(notify_db_session):
|
|
create_letter_branding(name='original')
|
|
letter_branding = LetterBranding.query.first()
|
|
assert letter_branding.name == 'original'
|
|
dao_update_letter_branding(letter_branding.id, name='new name')
|
|
assert LetterBranding.query.first().name == 'new name'
|