mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 08:51:30 -05: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.
53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
import pytest
|
|
|
|
from app.dao.service_permissions_dao import (
|
|
dao_fetch_service_permissions,
|
|
dao_remove_service_permission,
|
|
)
|
|
from app.models import (
|
|
EMAIL_TYPE,
|
|
INBOUND_SMS_TYPE,
|
|
INTERNATIONAL_SMS_TYPE,
|
|
LETTER_TYPE,
|
|
SMS_TYPE,
|
|
)
|
|
from tests.app.db import create_service, create_service_permission
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
def service_without_permissions(notify_db_session):
|
|
return create_service(service_permissions=[])
|
|
|
|
|
|
def test_create_service_permission(service_without_permissions):
|
|
service_permissions = create_service_permission(
|
|
service_id=service_without_permissions.id, permission=SMS_TYPE)
|
|
|
|
assert len(service_permissions) == 1
|
|
assert service_permissions[0].service_id == service_without_permissions.id
|
|
assert service_permissions[0].permission == SMS_TYPE
|
|
|
|
|
|
def test_fetch_service_permissions_gets_service_permissions(service_without_permissions):
|
|
create_service_permission(service_id=service_without_permissions.id, permission=LETTER_TYPE)
|
|
create_service_permission(service_id=service_without_permissions.id, permission=INTERNATIONAL_SMS_TYPE)
|
|
create_service_permission(service_id=service_without_permissions.id, permission=SMS_TYPE)
|
|
|
|
service_permissions = dao_fetch_service_permissions(service_without_permissions.id)
|
|
|
|
assert len(service_permissions) == 3
|
|
assert all(sp.service_id == service_without_permissions.id for sp in service_permissions)
|
|
assert all(sp.permission in [LETTER_TYPE, INTERNATIONAL_SMS_TYPE, SMS_TYPE] for sp in service_permissions)
|
|
|
|
|
|
def test_remove_service_permission(service_without_permissions):
|
|
create_service_permission(service_id=service_without_permissions.id, permission=EMAIL_TYPE)
|
|
create_service_permission(service_id=service_without_permissions.id, permission=INBOUND_SMS_TYPE)
|
|
|
|
dao_remove_service_permission(service_without_permissions.id, EMAIL_TYPE)
|
|
|
|
permissions = dao_fetch_service_permissions(service_without_permissions.id)
|
|
assert len(permissions) == 1
|
|
assert permissions[0].permission == INBOUND_SMS_TYPE
|
|
assert permissions[0].service_id == service_without_permissions.id
|