diff options
| author | Max Nanis | 2026-02-24 17:26:15 -0500 |
|---|---|---|
| committer | Max Nanis | 2026-02-24 17:26:15 -0500 |
| commit | 8c1940445503fd6678d0961600f2be81622793a2 (patch) | |
| tree | b9173562b8824b5eaa805e446d9d780e1f23fb2a /tests/conftest.py | |
| parent | 25d8c3c214baf10f6520cc1351f78473150e5d7a (diff) | |
| download | amt-jb-8c1940445503fd6678d0961600f2be81622793a2.tar.gz amt-jb-8c1940445503fd6678d0961600f2be81622793a2.zip | |
Extensive use of type checking. Movement of pytest conf towards handling managers (for db agnostic unittest). Starting to organize pytests.
Diffstat (limited to 'tests/conftest.py')
| -rw-r--r-- | tests/conftest.py | 228 |
1 files changed, 181 insertions, 47 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 985c9dc..3318f1c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,9 @@ import copy from datetime import datetime, timezone, timedelta +import os from typing import Optional from uuid import uuid4 - +from dotenv import load_dotenv import pytest from dateutil.tz import tzlocal from mypy_boto3_mturk.type_defs import ( @@ -11,62 +12,179 @@ from mypy_boto3_mturk.type_defs import ( CreateHITWithHITTypeResponseTypeDef, GetAssignmentResponseTypeDef, ) - -from jb.decorators import HQM, HTM, HM, AM +from jb.managers import Permission +from generalresearchutils.pg_helper import PostgresConfig from jb.managers.amt import AMTManager, APPROVAL_MESSAGE, NO_WORK_APPROVAL_MESSAGE from jb.models.assignment import AssignmentStub, Assignment -from jb.models.currency import USDCent +from generalresearchutils.currency import USDCent from jb.models.definitions import HitStatus, HitReviewStatus, AssignmentStatus from jb.models.hit import HitType, HitQuestion, Hit from tests import generate_amt_id @pytest.fixture -def amt_hit_type_id(): - return generate_amt_id() - - -@pytest.fixture -def amt_hit_id(): +def amt_hit_type_id() -> str: return generate_amt_id() @pytest.fixture -def amt_assignment_id(): +def amt_assignment_id() -> str: return generate_amt_id() @pytest.fixture -def amt_worker_id(): +def amt_worker_id() -> str: return generate_amt_id(length=21) @pytest.fixture -def amt_group_id(): +def amt_group_id() -> str: return generate_amt_id() @pytest.fixture -def tsid(): +def tsid() -> str: return uuid4().hex @pytest.fixture -def tsid1(): +def tsid1() -> str: return uuid4().hex @pytest.fixture -def tsid2(): +def tsid2() -> str: return uuid4().hex @pytest.fixture -def pe_id(): +def pe_id() -> str: # payout event / cashout request UUID return uuid4().hex +# --- Settings --- + + +@pytest.fixture(scope="session") +def env_file_path(pytestconfig): + 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) -> "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): + 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) -> PostgresConfig: + return PostgresConfig( + dsn=settings.amt_jb_db, + connect_timeout=1, + statement_timeout=1, + ) + + +# --- Managers --- + + +@pytest.fixture(scope="session") +def hqm(pg_config) -> "HitQuestionManager": + assert "/unittest-" in pg_config.dsn.path + + from jb.managers.hit import HitQuestionManager + + return HitQuestionManager( + pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] + ) + + +@pytest.fixture(scope="session") +def htm(pg_config) -> "HitTypeManager": + assert "/unittest-" in pg_config.dsn.path + + from jb.managers.hit import HitTypeManager + + return HitTypeManager( + pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] + ) + + +@pytest.fixture(scope="session") +def hm(pg_config) -> "HitManager": + assert "/unittest-" in pg_config.dsn.path + + from jb.managers.hit import HitManager + + return HitManager( + pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] + ) + + +@pytest.fixture(scope="session") +def am(pg_config) -> "AssignmentManager": + assert "/unittest-" in pg_config.dsn.path + + from jb.managers.assignment import AssignmentManager + + return AssignmentManager( + pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] + ) + + +@pytest.fixture(scope="session") +def bm(pg_config) -> "BonusManager": + assert "/unittest-" in pg_config.dsn.path + + from jb.managers.bonus import BonusManager + + return BonusManager( + pg_config=pg_config, permissions=[Permission.READ, Permission.CREATE] + ) + + +# --- Question --- + + +@pytest.fixture +def question() -> HitQuestion: + return HitQuestion(url="https://jamesbillings67.com/work/", height=1200) + + +@pytest.fixture +def question_record(hqm, question) -> HitQuestion: + return hqm.get_or_create(question) + + +# --- HITType --- + + @pytest.fixture def hit_type() -> HitType: return HitType( @@ -78,42 +196,38 @@ def hit_type() -> HitType: ) -from jb.models.hit import HitType +@pytest.fixture +def hit_type_record(htm, hit_type) -> HitType: + hit_type.amt_hit_type_id = generate_amt_id() + + return htm.get_or_create(hit_type) @pytest.fixture -def hit_type_with_amt_id(hit_type: HitType) -> HitType: +def hit_type_with_amt_id(htm, hit_type: HitType) -> HitType: # This is a real hit type I've previously registered with amt (sandbox). # It will always exist hit_type.amt_hit_type_id = "3217B3DC4P5YW9DRV9R3X8O56V041J" # Get or create our db - HTM.get_or_create(hit_type) + htm.get_or_create(hit_type) # this call adds the pk int id ---^ return hit_type -@pytest.fixture -def question(): - return HitQuestion(url="https://jamesbillings67.com/work/", height=1200) +# --- HIT --- @pytest.fixture -def hit_in_amt(hit_type_with_amt_id: HitType, question: HitQuestion) -> Hit: - # Actually create a new HIT in amt (sandbox) - question = HQM.get_or_create(question) - hit = AMTManager.create_hit_with_hit_type( - hit_type=hit_type_with_amt_id, question=question - ) - # Create it in the DB - HM.create(hit) - return hit +def amt_hit_id() -> str: + return generate_amt_id() @pytest.fixture -def hit(amt_hit_id, amt_hit_type_id, amt_group_id, question): +def hit(amt_hit_id, amt_hit_type_id, amt_group_id, question) -> Hit: now = datetime.now(tz=timezone.utc) + return Hit.model_validate( dict( amt_hit_id=amt_hit_id, @@ -140,24 +254,41 @@ def hit(amt_hit_id, amt_hit_type_id, amt_group_id, question): @pytest.fixture -def hit_in_db( - hit_type: HitType, amt_hit_type_id, amt_hit_id, question: HitQuestion, hit: Hit +def hit_record( + hm, + question_record, + hit_type_record, + hit, + amt_hit_id, ) -> Hit: """ Returns a hit that exists in our db, but does not in amazon (the amt ids are random). The mtwerk_hittype and mtwerk_question records will also exist (in the db) """ - question = HQM.get_or_create(question) - hit_type.amt_hit_type_id = amt_hit_type_id - HTM.create(hit_type) - hit.hit_type_id = hit_type.id + + hit.hit_type_id = hit_type_record.id hit.amt_hit_id = amt_hit_id - hit.question_id = question.id - HM.create(hit) + hit.question_id = question_record.id + + hm.create(hit) + return hit + + +@pytest.fixture +def hit_in_amt(hm, question_record, hit_type_with_amt_id: HitType) -> Hit: + # Actually create a new HIT in amt (sandbox) + hit = AMTManager.create_hit_with_hit_type( + hit_type=hit_type_with_amt_id, question=question_record + ) + # Create it in the DB + hm.create(hit) return hit +# --- Assignment --- + + @pytest.fixture def assignment_stub(hit: Hit, amt_assignment_id, amt_worker_id): now = datetime.now(tz=timezone.utc) @@ -193,28 +324,31 @@ def assignment_factory(hit: Hit): @pytest.fixture -def assignment_in_db_factory(assignment_factory): +def assignment_in_db_factory(am, assignment_factory): def inner(hit_id: int, amt_worker_id: Optional[str] = None): a = assignment_factory(amt_worker_id=amt_worker_id) a.hit_id = hit_id - AM.create_stub(a) - AM.update_answer(a) + am.create_stub(a) + am.update_answer(a) return a return inner @pytest.fixture -def assignment_stub_in_db(hit_in_db, assignment_stub) -> AssignmentStub: +def assignment_stub_in_db(am, hit_record, assignment_stub) -> AssignmentStub: """ Returns an AssignmentStub that exists in our db, but does not in amazon (the amt ids are random). The mtwerk_hit, mtwerk_hittype, and mtwerk_question records will also exist (in the db) """ - assignment_stub.hit_id = hit_in_db.id - AM.create_stub(assignment_stub) + assignment_stub.hit_id = hit_record.id + am.create_stub(assignment_stub) return assignment_stub +# --- HIT --- + + @pytest.fixture def amt_response_metadata(): req_id = str(uuid4()) |
