import os from typing import TYPE_CHECKING from uuid import uuid4 from dotenv import load_dotenv import pytest from generalresearchutils.pg_helper import PostgresConfig from tests import generate_amt_id from _pytest.config import Config from jb.decorators import CLIENT_CONFIG from mypy_boto3_mturk import MTurkClient if TYPE_CHECKING: from jb.settings import Settings pytest_plugins = [ "tests.fixtures.amt", "tests.fixtures.flow", "tests.fixtures.http", "tests.fixtures.managers", "tests.fixtures.models", ] # --- IDs and Identifiers --- @pytest.fixture def amt_hit_id() -> str: return generate_amt_id() @pytest.fixture def amt_hit_type_id() -> str: return generate_amt_id() @pytest.fixture def amt_assignment_id() -> str: return generate_amt_id() @pytest.fixture def amt_worker_id() -> str: return generate_amt_id(length=21) @pytest.fixture def amt_group_id() -> str: return generate_amt_id() @pytest.fixture def tsid() -> str: return uuid4().hex @pytest.fixture def tsid1() -> str: return uuid4().hex @pytest.fixture def tsid2() -> str: return uuid4().hex @pytest.fixture def pe_id() -> str: # payout event / cashout request UUID return uuid4().hex # --- Settings --- @pytest.fixture(scope="session") def env_file_path(pytestconfig: Config) -> str: root_path = pytestconfig.rootpath env_path = os.path.join(root_path, ".env.test") if os.path.exists(env_path): load_dotenv(dotenv_path=env_path, override=True) return env_path @pytest.fixture(scope="session") def settings(env_file_path: str) -> "Settings": from jb.settings import Settings as JBSettings s = JBSettings(_env_file=env_file_path) return s # --- Database Connectors --- @pytest.fixture(scope="session") def redis(settings: "Settings"): from generalresearchutils.redis_helper import RedisConfig redis_config = RedisConfig( dsn=settings.redis, decode_responses=True, socket_timeout=settings.redis_timeout, socket_connect_timeout=settings.redis_timeout, ) return redis_config.create_redis_client() @pytest.fixture(scope="session") def pg_config(settings: "Settings") -> PostgresConfig: return PostgresConfig( dsn=settings.amt_jb_db, connect_timeout=1, statement_timeout=1, ) # --- Connectors --- @pytest.fixture(scope="session") def amt_client(settings: "Settings") -> MTurkClient: import boto3 client = boto3.client( service_name="mturk", region_name="us-east-1", endpoint_url=str(settings.amt_endpoint), aws_access_key_id=settings.amt_access_id, aws_secret_access_key=settings.amt_secret_key, config=CLIENT_CONFIG, ) # Confirm we're only using the Sandbox for any unittests assert "mturk-requester-sandbox" in client.meta.endpoint_url return client