"""PyTest fixtures. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ from typing import Any, Generator, Iterator from unittest.mock import MagicMock, patch import pytest from flask.ctx import AppContext from flask.testing import FlaskClient from owslogger import flask_logger from owsrequest import test_utils from sqlalchemy import text import application from vectororder import api from vectororder.connectors.mysql import art_db_connector, dd_db_connector from vectororder.constants import header as header_consts from vectororder.models.schemas import OrderData, OrderStatus @pytest.fixture def fixture_client() -> FlaskClient: """Create an api test client fixture.""" api.app.config["PROPAGATE_EXCEPTIONS"] = True return api.app.test_client() @pytest.fixture def context() -> AppContext: """Return flask app context, including a mock logger. Returns: AppContext: flask app context, including a mock logger. """ class MockLog: """Mock Logging Class.""" def error(self) -> None: pass def info(self) -> None: pass def warning(self) -> None: pass context_instance = application.app.app_context() context_instance.g.ows = flask_logger.Ows() context_instance.g.ows.log = MockLog return context_instance @pytest.fixture def client_headers() -> dict[str, str]: """Return test headers when using test client for functional tests. Returns: dict: header values. """ return { header_consts.CORRELATION_ID: "abc-123", header_consts.CONTENT_TYPE: header_consts.CONTENT_TYPE_JSON_VAL, } @pytest.fixture def grass_headers() -> dict[str, str]: """Return test headers when using test client for functional tests. Returns: dict: header values. """ return { header_consts.GRASS_ACCOUNT_TYPE: header_consts.GRASS_ACCOUNT_TYPE_VENDOR, header_consts.GRASS_ACCOUNT_ID: "1234", header_consts.CORRELATION_ID: "abc-123", header_consts.CONTENT_TYPE: header_consts.CONTENT_TYPE_JSON_VAL, header_consts.ORCHARD_USER_ID: "alw:123", } @pytest.fixture def app_context() -> Generator[AppContext, Any, None]: """App context fixture.""" with application.app.app_context() as app_context: app_context.g.ows = MagicMock() app_context.g.request_context = MagicMock() yield app_context @pytest.fixture def init_db_tables() -> None: """Ensure db tables are created before running tests and emptied after each test.""" with art_db_connector.db_session(turn_off_foreign_key_constraint=True) as session: session.execute(text("TRUNCATE TABLE vendor")) session.execute(text("TRUNCATE TABLE project")) session.execute(text("TRUNCATE TABLE delivery_history")) session.execute(text("TRUNCATE TABLE track")) session.execute(text("TRUNCATE TABLE releases")) session.execute(text("TRUNCATE TABLE customer_master_master")) session.execute(text("TRUNCATE TABLE customer_master_master_distribution_type")) session.execute(text("TRUNCATE TABLE distribution_format")) session.execute(text("TRUNCATE TABLE store_classification_detail")) session.execute(text("TRUNCATE TABLE classification_detail")) with dd_db_connector.db_session(turn_off_foreign_key_constraint=True) as session: session.execute(text("TRUNCATE TABLE encoding_queue_detail")) session.execute(text("TRUNCATE TABLE encoding_queue")) session.execute(text("TRUNCATE TABLE delivery_batch_detail")) session.execute(text("TRUNCATE TABLE direct_delivery_location")) @pytest.fixture def init_db_order_tables() -> None: """Ensure db tables are created before running tests and emptied after each test.""" with art_db_connector.db_session(turn_off_foreign_key_constraint=True) as session: session.execute(text("TRUNCATE TABLE encoding_order")) session.execute(text("TRUNCATE TABLE encoding_order_detail")) session.execute(text("TRUNCATE TABLE encoding_order_detail_dms")) @pytest.fixture def mock_order_data() -> OrderData: return OrderData( encoder_id=18, is_meta_update=False, order_status=OrderStatus.OPEN, priority=1, store_ids={1, 2, 3}, upcs=set(), user_id=101, ) @pytest.fixture def mock_invalid_order_data() -> OrderData: return OrderData( encoder_id=18, is_meta_update=False, order_status=OrderStatus.CLOSED, priority=1, store_ids={1, 2, 3}, upcs=set(), user_id=101, ) @pytest.fixture def mock_raise_for_status() -> Iterator[MagicMock]: """Mock raise_for_status on MockOwsResponse.""" with patch.object( test_utils.MockOwsResponse, "raise_for_status", create=True ) as mock_raise_for_status: yield mock_raise_for_status