from functools import wraps from product.models.unsafe_methods import UnsafeMethods from product.connectors import mysql product_id = "1" releases = [{"release_id": product_id}] tracks = [ {"release_id": product_id, "tuid": 1}, {"release_id": product_id, "tuid": 2}, ] def UNSAFE_schema(function): """Decorator to set up and tear down the database.""" @wraps(function) def call_function_within_db_context(*args, **kwargs): UnsafeMethods._Track.__table__.create(mysql._db_engine) UnsafeMethods._Release.__table__.create(mysql._db_engine) with mysql.db_session() as session: session.bulk_save_objects([UnsafeMethods._Release(**release) for release in releases]) session.bulk_save_objects([UnsafeMethods._Track(**track) for track in tracks]) try: function_return = function(*args, **kwargs) finally: UnsafeMethods._Track.__table__.drop(mysql._db_engine) UnsafeMethods._Release.__table__.drop(mysql._db_engine) return function_return return call_function_within_db_context @UNSAFE_schema def test_UNSAFE_delete_product_by_product_id(): """Test the UNSAFE_delete_product_by_product_id method.""" with mysql.db_session() as session: UnsafeMethods.UNSAFE_delete_product_by_product_id(product_id, session) assert session.query(UnsafeMethods._Release).count() == 0