Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Add fixture to clear tables between test (#680)
Browse files Browse the repository at this point in the history
* Add fixture to clear tables between test runs

* Update CHANGELOG

* Add missing ordering to customer_details logs query in test.

* update import path

Co-authored-by: Paul Sanders <pau@ethyca.com>
Co-authored-by: Dawn Pattison <pattisdr@users.noreply.github.com>
Co-authored-by: Sean Preston <sean@ethyca.com>
  • Loading branch information
4 people authored Jul 7, 2022
1 parent 01670fd commit 5f509a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The types of changes are:
* `fidesops worker` command for running a Celery worker [#673](https://github.com/ethyca/fidesops/pull/673/)

### Developer Experience
* Add fixture to clear tables between test [#680](https://github.com/ethyca/fidesops/pull/680)
* Reduce the size of the docker image [#707](https://github.com/ethyca/fidesops/pull/707)
* Parallelize CI safe checks to reduce run time [#717](https://github.com/ethyca/fidesops/pull/717)
* Add dependabot to keep dependencies up to date [#718](https://github.com/ethyca/fidesops/pull/718)
Expand Down
31 changes: 30 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
from fideslib.db.session import Session, get_db_engine, get_db_session
from fideslib.models.client import ClientDetail
from fideslib.oauth.jwt import generate_jwe
from sqlalchemy.exc import IntegrityError
from sqlalchemy_utils.functions import create_database, database_exists, drop_database

from fidesops.api.v1.scope_registry import SCOPE_REGISTRY
from fidesops.core.config import config
from fidesops.db.base import Base
from fidesops.db.database import init_db
from fidesops.main import app
from fidesops.models.privacy_request import generate_request_callback_jwe
Expand Down Expand Up @@ -91,6 +93,34 @@ def db() -> Generator:
logger.debug(f"Database at: {engine.url} successfully dropped")


@pytest.fixture(autouse=True)
def clear_db_tables(db):
"""Clear data from tables between tests.
If relationships are not set to cascade on delete they will fail with an
IntegrityError if there are relationsips present. This function stores tables
that fail with this error then recursively deletes until no more IntegrityErrors
are present.
"""
yield

def delete_data(tables):
redo = []
for table in tables:
try:
db.execute(table.delete())
except IntegrityError:
redo.append(table)
finally:
db.commit()

if redo:
delete_data(redo)

db.commit() # make sure all transactions are closed before starting deletes
delete_data(Base.metadata.sorted_tables)


@pytest.fixture(scope="session")
def cache() -> Generator:
yield get_cache()
Expand All @@ -115,7 +145,6 @@ def oauth_client(db: Session) -> Generator:
db.commit()
db.refresh(client)
yield client
client.delete(db)


def generate_auth_header_for_user(user, scopes) -> Dict[str, str]:
Expand Down

0 comments on commit 5f509a5

Please sign in to comment.