import os import uuid from collections.abc import Generator from typing import Any import pytest from aws_testing_utils.lambda_handler import LambdaHandler from aws_testing_utils.step_function_handler import StepFunctionHandler from test_fixtures import deselect_items_by_tag from test_fixtures.mysql import MySQLConnection from test_fixtures.neo4j import Neo4jConnection from test_fixtures.snowflake import SnowflakeConnection from config import SFN_STATE_MACHINE_NAME, SNOWFLAKE_CONFIG, SNOWFLAKE_KEY_SECRET _ART_RELATIONS_SECRET = "qa/e2e-test-secrets/db/art-relations" _ROYALTY_ACCOUNTING_SECRET = "qa/e2e-test-secrets/db/royalty-accounting" @pytest.fixture(scope="session") def art_relations_db() -> Generator[MySQLConnection, None, None]: conn = MySQLConnection(_ART_RELATIONS_SECRET) yield conn conn.close() @pytest.fixture(scope="session") def royalty_accounting_db() -> Generator[MySQLConnection, None, None]: conn = MySQLConnection(_ROYALTY_ACCOUNTING_SECRET) yield conn conn.close() @pytest.fixture(scope="session") def neo4j_db() -> Generator[Neo4jConnection, None, None]: conn = Neo4jConnection( username_secret="qa/e2e-test-secrets/neo4j/suite-username", password_secret="qa/e2e-test-secrets/neo4j/suite-password", hostname_secret="qa/e2e-test-secrets/neo4j/suite-hostname", ) yield conn conn.close() @pytest.fixture(scope="session") def snowflake_db() -> Generator[SnowflakeConnection, None, None]: conn = SnowflakeConnection(SNOWFLAKE_KEY_SECRET, **SNOWFLAKE_CONFIG) yield conn conn.close() @pytest.fixture(scope="session") def lambda_handler() -> LambdaHandler: return LambdaHandler() @pytest.fixture(scope="session") def sfn_handler() -> StepFunctionHandler: return StepFunctionHandler(SFN_STATE_MACHINE_NAME) _ORIGINATING_VENDOR_ID = 6971 _DESTINATION_VENDOR_ID = 7123 @pytest.fixture def transfer_job(art_relations_db: MySQLConnection) -> Generator[dict[str, Any], None, None]: project = art_relations_db.fetchone( "SELECT project_id FROM project WHERE vendor_id = %s LIMIT 1", (_ORIGINATING_VENDOR_ID,), ) assert project, f"No project found for vendor_id {_ORIGINATING_VENDOR_ID}" created_by = str(uuid.uuid4()) art_relations_db.execute( """ INSERT INTO project_transfer_job (project_id, originating_vendor_id, destination_vendor_id, created_by_identity_id) VALUES (%s, %s, %s, %s) """, (project["project_id"], _ORIGINATING_VENDOR_ID, _DESTINATION_VENDOR_ID, created_by), ) job = art_relations_db.fetchone( "SELECT * FROM project_transfer_job WHERE job_id = LAST_INSERT_ID()", ) assert job is not None yield job art_relations_db.execute( "DELETE FROM project_transfer_job WHERE job_id = %s", (job["job_id"],), ) _SFN_LAMBDA_NAME = "state-machine" def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None: lambda_names_env = os.environ.get("LAMBDA_FUNCTION_NAMES", "").strip() active_tags = {name.strip() for name in lambda_names_env.split(",")} if lambda_names_env else set() deselect_items_by_tag( config=config, items=items, marker_name="lambda_name", active_tags=active_tags, always_deselect_when_missing={_SFN_LAMBDA_NAME}, )