diff options
| author | Max Nanis | 2026-02-25 16:20:18 -0500 |
|---|---|---|
| committer | Max Nanis | 2026-02-25 16:20:18 -0500 |
| commit | 04aee0dc7e908ce020d2d2c3f8ffb4a96424b883 (patch) | |
| tree | efb99622da9a962a73921a945373c019f98e6273 /tests/conftest.py | |
| parent | 8c1940445503fd6678d0961600f2be81622793a2 (diff) | |
| download | amt-jb-04aee0dc7e908ce020d2d2c3f8ffb4a96424b883.tar.gz amt-jb-04aee0dc7e908ce020d2d2c3f8ffb4a96424b883.zip | |
test_notification (for sns mgmt), along with more type hinting on pytest conftest
Diffstat (limited to 'tests/conftest.py')
| -rw-r--r-- | tests/conftest.py | 131 |
1 files changed, 81 insertions, 50 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 3318f1c..a33b149 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ import copy from datetime import datetime, timezone, timedelta import os -from typing import Optional +from typing import Optional, TYPE_CHECKING, Callable, Dict, Any from uuid import uuid4 from dotenv import load_dotenv import pytest @@ -20,6 +20,16 @@ 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 +from _pytest.config import Config + +if TYPE_CHECKING: + from jb.settings import Settings + from jb.managers.hit import HitQuestionManager, HitTypeManager, HitManager + from jb.managers.assignment import AssignmentManager + from jb.managers.bonus import BonusManager + + +# --- IDs and Identifiers --- @pytest.fixture @@ -67,7 +77,7 @@ def pe_id() -> str: @pytest.fixture(scope="session") -def env_file_path(pytestconfig): +def env_file_path(pytestconfig: Config) -> str: root_path = pytestconfig.rootpath env_path = os.path.join(root_path, ".env.test") @@ -78,7 +88,7 @@ def env_file_path(pytestconfig): @pytest.fixture(scope="session") -def settings(env_file_path) -> "Settings": +def settings(env_file_path: str) -> "Settings": from jb.settings import Settings as JBSettings s = JBSettings(_env_file=env_file_path) @@ -90,7 +100,7 @@ def settings(env_file_path) -> "Settings": @pytest.fixture(scope="session") -def redis(settings): +def redis(settings: "Settings"): from generalresearchutils.redis_helper import RedisConfig redis_config = RedisConfig( @@ -103,7 +113,7 @@ def redis(settings): @pytest.fixture(scope="session") -def pg_config(settings) -> PostgresConfig: +def pg_config(settings: "Settings") -> PostgresConfig: return PostgresConfig( dsn=settings.amt_jb_db, connect_timeout=1, @@ -115,8 +125,10 @@ def pg_config(settings) -> PostgresConfig: @pytest.fixture(scope="session") -def hqm(pg_config) -> "HitQuestionManager": - assert "/unittest-" in pg_config.dsn.path +def hqm(pg_config: PostgresConfig) -> "HitQuestionManager": + assert ( + pg_config.dsn.path and "/unittest-" in pg_config.dsn.path + ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" from jb.managers.hit import HitQuestionManager @@ -126,8 +138,10 @@ def hqm(pg_config) -> "HitQuestionManager": @pytest.fixture(scope="session") -def htm(pg_config) -> "HitTypeManager": - assert "/unittest-" in pg_config.dsn.path +def htm(pg_config: PostgresConfig) -> "HitTypeManager": + assert ( + pg_config.dsn.path and "/unittest-" in pg_config.dsn.path + ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" from jb.managers.hit import HitTypeManager @@ -137,8 +151,10 @@ def htm(pg_config) -> "HitTypeManager": @pytest.fixture(scope="session") -def hm(pg_config) -> "HitManager": - assert "/unittest-" in pg_config.dsn.path +def hm(pg_config: PostgresConfig) -> "HitManager": + assert ( + pg_config.dsn.path and "/unittest-" in pg_config.dsn.path + ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" from jb.managers.hit import HitManager @@ -148,8 +164,10 @@ def hm(pg_config) -> "HitManager": @pytest.fixture(scope="session") -def am(pg_config) -> "AssignmentManager": - assert "/unittest-" in pg_config.dsn.path +def am(pg_config: PostgresConfig) -> "AssignmentManager": + assert ( + pg_config.dsn.path and "/unittest-" in pg_config.dsn.path + ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" from jb.managers.assignment import AssignmentManager @@ -159,8 +177,10 @@ def am(pg_config) -> "AssignmentManager": @pytest.fixture(scope="session") -def bm(pg_config) -> "BonusManager": - assert "/unittest-" in pg_config.dsn.path +def bm(pg_config: PostgresConfig) -> "BonusManager": + assert ( + pg_config.dsn.path and "/unittest-" in pg_config.dsn.path + ), "pg_config must point to a unittest database (dsn path must contain '/unittest-')" from jb.managers.bonus import BonusManager @@ -178,7 +198,7 @@ def question() -> HitQuestion: @pytest.fixture -def question_record(hqm, question) -> HitQuestion: +def question_record(hqm: "HitQuestionManager", question: HitQuestion) -> HitQuestion: return hqm.get_or_create(question) @@ -197,14 +217,14 @@ def hit_type() -> HitType: @pytest.fixture -def hit_type_record(htm, hit_type) -> HitType: +def hit_type_record(htm: "HitTypeManager", hit_type: HitType) -> 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(htm, hit_type: HitType) -> HitType: +def hit_type_with_amt_id(htm: "HitTypeManager", 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" @@ -225,7 +245,9 @@ def amt_hit_id() -> str: @pytest.fixture -def hit(amt_hit_id, amt_hit_type_id, amt_group_id, question) -> Hit: +def hit( + amt_hit_id: str, amt_hit_type_id: str, amt_group_id: str, question: HitQuestion +) -> Hit: now = datetime.now(tz=timezone.utc) return Hit.model_validate( @@ -255,11 +277,11 @@ def hit(amt_hit_id, amt_hit_type_id, amt_group_id, question) -> Hit: @pytest.fixture def hit_record( - hm, - question_record, - hit_type_record, - hit, - amt_hit_id, + hm: "HitManager", + question_record: HitQuestion, + hit_type_record: HitType, + hit: Hit, + amt_hit_id: str, ) -> Hit: """ Returns a hit that exists in our db, but does not in amazon (the amt ids @@ -276,7 +298,9 @@ def hit_record( @pytest.fixture -def hit_in_amt(hm, question_record, hit_type_with_amt_id: HitType) -> Hit: +def hit_in_amt( + hm: "HitManager", question_record: HitQuestion, 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 @@ -290,7 +314,7 @@ def hit_in_amt(hm, question_record, hit_type_with_amt_id: HitType) -> Hit: @pytest.fixture -def assignment_stub(hit: Hit, amt_assignment_id, amt_worker_id): +def assignment_stub(hit: Hit, amt_assignment_id: str, amt_worker_id: str): now = datetime.now(tz=timezone.utc) return AssignmentStub( amt_assignment_id=amt_assignment_id, @@ -303,11 +327,27 @@ def assignment_stub(hit: Hit, amt_assignment_id, amt_worker_id): @pytest.fixture +def assignment_stub_record( + am: "AssignmentManager", hit_record: Hit, assignment_stub: AssignmentStub +) -> 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_record.id + am.create_stub(stub=assignment_stub) + return assignment_stub + + +@pytest.fixture def assignment_factory(hit: Hit): - def inner(amt_worker_id: str = None): + + def inner(amt_worker_id: str = None) -> Assignment: now = datetime.now(tz=timezone.utc) amt_assignment_id = generate_amt_id() amt_worker_id = amt_worker_id or generate_amt_id() + return Assignment( amt_assignment_id=amt_assignment_id, amt_hit_id=hit.amt_hit_id, @@ -324,7 +364,10 @@ def assignment_factory(hit: Hit): @pytest.fixture -def assignment_in_db_factory(am, assignment_factory): +def assignment_record_factory( + am: "AssignmentManager", assignment_factory: Callable[..., Assignment] +): + 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 @@ -335,22 +378,11 @@ def assignment_in_db_factory(am, assignment_factory): return inner -@pytest.fixture -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_record.id - am.create_stub(assignment_stub) - return assignment_stub - - -# --- HIT --- +# --- Response --- @pytest.fixture -def amt_response_metadata(): +def amt_response_metadata() -> Dict[str, Any]: req_id = str(uuid4()) return { "RequestId": req_id, @@ -367,7 +399,7 @@ def amt_response_metadata(): @pytest.fixture def create_hit_type_response( - amt_hit_type_id, amt_response_metadata + amt_hit_type_id: str, amt_response_metadata: Dict[str, Any] ) -> CreateHITTypeResponseTypeDef: return { "HITTypeId": amt_hit_type_id, @@ -377,7 +409,7 @@ def create_hit_type_response( @pytest.fixture def create_hit_with_hit_type_response( - amt_hit_type_id, amt_hit_id, amt_response_metadata + amt_hit_type_id: str, amt_hit_id: str, amt_response_metadata ) -> CreateHITWithHITTypeResponseTypeDef: amt_group_id = generate_amt_id(length=30) return { @@ -407,7 +439,7 @@ def create_hit_with_hit_type_response( @pytest.fixture def get_hit_response( - amt_hit_type_id, amt_hit_id, amt_response_metadata + amt_hit_type_id: str, amt_hit_id: str, amt_response_metadata ) -> GetHITResponseTypeDef: amt_group_id = generate_amt_id(length=30) return { @@ -447,13 +479,12 @@ def get_hit_response_reviewing(get_hit_response): @pytest.fixture def get_assignment_response( - amt_hit_type_id, - amt_hit_id, - amt_assignment_id, - amt_worker_id, + amt_hit_id: str, + amt_assignment_id: str, + amt_worker_id: str, get_hit_response, amt_response_metadata, - tsid, + tsid: str, ) -> GetAssignmentResponseTypeDef: hit_response = get_hit_response["HIT"] local_now = datetime.now(tz=tzlocal()) @@ -484,7 +515,7 @@ def get_assignment_response( @pytest.fixture def get_assignment_response_no_tsid( - get_assignment_response, amt_worker_id, amt_assignment_id + get_assignment_response, amt_worker_id: str, amt_assignment_id: str ): res = copy.deepcopy(get_assignment_response) res["Assignment"]["Answer"] = ( |
