"""Utility functions to support database interactions in tests.""" from typing import Any from sqlalchemy.sql import text from store.connectors import mysql def seed_models(models: list[Any]) -> None: """Save the given model(s) to the DB. Args: models (list): list of model instances to save. """ if not hasattr(models, "__iter__"): models = [models] with mysql.db_session() as session: session.execute(text("SET FOREIGN_KEY_CHECKS=0;")) for model in models: session.add(model) session.flush() # detach the objects from this session so tests can interrogate them for model in models: session.expunge(model) session.execute(text("SET FOREIGN_KEY_CHECKS=1;")) def test_schema() -> None: """Test schema. This just truncates table and does not seed data. Individual test cases can use factories to seed data as needed. """ with mysql.db_session() as session: session.execute(text("SET FOREIGN_KEY_CHECKS=0;")) session.execute(text("TRUNCATE TABLE classification_detail;")) session.execute(text("TRUNCATE TABLE distribution_type;")) session.execute(text("TRUNCATE TABLE customer_master_master;")) session.execute( text("TRUNCATE TABLE customer_master_master_distribution_type;") ) session.execute(text("TRUNCATE TABLE store_classification_detail;")) session.execute(text("TRUNCATE TABLE customer_master;")) session.execute(text("TRUNCATE TABLE country;")) session.execute(text("SET FOREIGN_KEY_CHECKS=1;"))