diff options
Diffstat (limited to 'tests/fixtures')
| -rw-r--r-- | tests/fixtures/amt.py | 11 | ||||
| -rw-r--r-- | tests/fixtures/flow.py | 39 | ||||
| -rw-r--r-- | tests/fixtures/http.py | 21 | ||||
| -rw-r--r-- | tests/fixtures/managers.py | 8 | ||||
| -rw-r--r-- | tests/fixtures/models.py | 66 |
5 files changed, 71 insertions, 74 deletions
diff --git a/tests/fixtures/amt.py b/tests/fixtures/amt.py index 65df125..d7cc03d 100644 --- a/tests/fixtures/amt.py +++ b/tests/fixtures/amt.py @@ -1,17 +1,18 @@ -import pytest import copy - +from collections.abc import Callable from datetime import datetime, timedelta -from typing import Callable from uuid import uuid4 + +import pytest from dateutil.tz import tzlocal from mypy_boto3_mturk.type_defs import ( - GetHITResponseTypeDef, CreateHITTypeResponseTypeDef, - ResponseMetadataTypeDef, CreateHITWithHITTypeResponseTypeDef, GetAssignmentResponseTypeDef, + GetHITResponseTypeDef, + ResponseMetadataTypeDef, ) + from jb.managers.amt import APPROVAL_MESSAGE, NO_WORK_APPROVAL_MESSAGE from tests import generate_amt_id diff --git a/tests/fixtures/flow.py b/tests/fixtures/flow.py index 3fcca81..7bf8b4f 100644 --- a/tests/fixtures/flow.py +++ b/tests/fixtures/flow.py @@ -1,18 +1,21 @@ -from datetime import timezone, datetime -from typing import Dict, Callable, Any, Optional +from collections.abc import Callable +from datetime import datetime, timezone +from typing import Any from uuid import uuid4 import pytest import requests +from generalresearch.currency import USDCent +from generalresearch.models.thl.definitions import PayoutStatus from generalresearch.models.thl.payout import UserPayoutEvent -from generalresearch.models.thl.wallet.definitions import PayoutType from generalresearch.models.thl.wallet.cashout_method import ( - CashoutRequestResponse, CashoutRequestInfo, + CashoutRequestResponse, ) +from generalresearch.models.thl.wallet.definitions import PayoutType from mypy_boto3_mturk.type_defs import ( - GetHITResponseTypeDef, GetAssignmentResponseTypeDef, + GetHITResponseTypeDef, ) from jb.config import settings @@ -20,8 +23,6 @@ from jb.managers.amt import ( APPROVAL_MESSAGE, BONUS_MESSAGE, ) -from generalresearch.currency import USDCent -from generalresearch.models.thl.definitions import PayoutStatus @pytest.fixture @@ -31,16 +32,16 @@ def approved_assignment_stubs( amt_assignment_id: str, amt_hit_id: str, hit_response_reviewing: GetHITResponseTypeDef, -) -> Callable[..., list[Dict[str, Any]]]: +) -> Callable[..., list[dict[str, Any]]]: # These are the AMT_CLIENT stubs/mocks that need to be set when running # process_assignment_submitted() which will result in an approved # assignment and sent bonus def _inner( feedback: str = APPROVAL_MESSAGE, - override_response: Optional[str] = None, - override_approve_response: Optional[str] = None, - ) -> list[Dict[str, Any]]: + override_response: str | None = None, + override_approve_response: str | None = None, + ) -> list[dict[str, Any]]: response = override_response or assignment_response approve_response = ( @@ -84,11 +85,11 @@ def approved_assignment_stubs( @pytest.fixture def approved_assignment_stubs_w_bonus( - approved_assignment_stubs: Callable[..., list[Dict[str, Any]]], + approved_assignment_stubs: Callable[..., list[dict[str, Any]]], amt_worker_id: str, amt_assignment_id: str, pe_id: str, -) -> list[Dict[str, Any]]: +) -> list[dict[str, Any]]: now = datetime.now(tz=timezone.utc) stubs = approved_assignment_stubs().copy() @@ -132,16 +133,16 @@ def rejected_assignment_stubs( amt_assignment_id: str, amt_hit_id: str, hit_response_reviewing: GetHITResponseTypeDef, -) -> Callable[..., list[Dict[str, Any]]]: +) -> Callable[..., list[dict[str, Any]]]: # These are the AMT_CLIENT stubs/mocks that need to be set when running # process_assignment_submitted() which will result in a rejected # assignment def _inner( reject_reason: str, - override_response: Optional[str] = None, - override_reject_response: Optional[str] = None, - ) -> list[Dict[str, Any]]: + override_response: str | None = None, + override_reject_response: str | None = None, + ) -> list[dict[str, Any]]: response = override_response or assignment_response reject_response = ( @@ -233,7 +234,7 @@ def mock_thl_responses( elif url == wallet_url: class MockThlWalletResponse: - def json(self) -> Dict[str, Any]: + def json(self) -> dict[str, Any]: return { "wallet": { "amount": wallet_redeemable_amount, @@ -246,7 +247,7 @@ def mock_thl_responses( elif url == status_url: class MockThlStatusResponse: - def json(self) -> Dict[str, Any]: + def json(self) -> dict[str, Any]: return { "tsid": tsid, "product_id": str(settings.product_id), diff --git a/tests/fixtures/http.py b/tests/fixtures/http.py index 5f50580..4b0792c 100644 --- a/tests/fixtures/http.py +++ b/tests/fixtures/http.py @@ -1,20 +1,19 @@ +import json +import secrets +from collections.abc import AsyncGenerator +from typing import Any + import httpx -import redis import pytest +import redis import requests_mock from asgi_lifespan import LifespanManager -from httpx import AsyncClient, ASGITransport -from typing import Dict, Any, AsyncGenerator +from httpx import ASGITransport, AsyncClient +from jb.config import JB_EVENTS_STREAM, settings from jb.main import app -import json - -from httpx import AsyncClient -import secrets - -from jb.models.hit import Hit from jb.models.assignment import AssignmentStub -from jb.config import JB_EVENTS_STREAM, settings +from jb.models.hit import Hit from tests import generate_amt_id @@ -69,7 +68,7 @@ def generate_hex_id(length: int = 40) -> str: @pytest.fixture def mturk_event_body_record( hit_record: Hit, assignment_stub_record: AssignmentStub -) -> Dict[str, Any]: +) -> dict[str, Any]: return { "Type": "Notification", "Message": json.dumps( diff --git a/tests/fixtures/managers.py b/tests/fixtures/managers.py index d10b542..22eae5e 100644 --- a/tests/fixtures/managers.py +++ b/tests/fixtures/managers.py @@ -1,14 +1,16 @@ from typing import TYPE_CHECKING + import pytest -from jb.managers import Permission from generalresearch.pg_helper import PostgresConfig from mypy_boto3_mturk import MTurkClient +from jb.managers import Permission + if TYPE_CHECKING: - from jb.managers.hit import HitQuestionManager, HitTypeManager, HitManager + from jb.managers.amt import AMTManager from jb.managers.assignment import AssignmentManager from jb.managers.bonus import BonusManager - from jb.managers.amt import AMTManager + from jb.managers.hit import HitManager, HitQuestionManager, HitTypeManager # --- Managers --- diff --git a/tests/fixtures/models.py b/tests/fixtures/models.py index b818caa..157daec 100644 --- a/tests/fixtures/models.py +++ b/tests/fixtures/models.py @@ -1,23 +1,22 @@ -from datetime import timezone, datetime +from collections.abc import Callable, Generator +from datetime import datetime, timedelta, timezone +from typing import TYPE_CHECKING import pytest - -from jb.models.event import MTurkEvent +from generalresearch.currency import USDCent from generalresearch.pg_helper import PostgresConfig +from psycopg.errors import ForeignKeyViolation -from datetime import datetime, timezone, timedelta -from typing import Optional, TYPE_CHECKING, Callable, Generator from jb.managers.amt import AMTManager -from jb.models.assignment import AssignmentStub, Assignment -from generalresearch.currency import USDCent -from jb.models.definitions import HitStatus, HitReviewStatus, AssignmentStatus -from jb.models.hit import HitType, HitQuestion, Hit +from jb.models.assignment import Assignment, AssignmentStub +from jb.models.definitions import AssignmentStatus, HitReviewStatus, HitStatus +from jb.models.event import MTurkEvent +from jb.models.hit import Hit, HitQuestion, HitType from tests import generate_amt_id -from psycopg.errors import ForeignKeyViolation if TYPE_CHECKING: - from jb.managers.hit import HitQuestionManager, HitTypeManager, HitManager from jb.managers.assignment import AssignmentManager + from jb.managers.hit import HitManager, HitQuestionManager, HitTypeManager # --- MTurk Event --- @@ -76,10 +75,9 @@ def hit_type_record( yield ht try: - with pg_config.make_connection() as conn: - with conn.cursor() as c: - c.execute("DELETE FROM mtwerk_hittype WHERE id=%s", (ht.id,)) - conn.commit() + with pg_config.make_connection() as conn, conn.cursor() as c: + c.execute("DELETE FROM mtwerk_hittype WHERE id=%s", (ht.id,)) + conn.commit() except ForeignKeyViolation: pass # DB gets dropped anyway, don't care @@ -99,10 +97,9 @@ def hit_type_record_with_amt_id( yield ht try: - with pg_config.make_connection() as conn: - with conn.cursor() as c: - c.execute("DELETE FROM mtwerk_hittype WHERE id=%s", (ht.id,)) - conn.commit() + with pg_config.make_connection() as conn, conn.cursor() as c: + c.execute("DELETE FROM mtwerk_hittype WHERE id=%s", (ht.id,)) + conn.commit() except ForeignKeyViolation: pass # DB gets dropped anyway, don't care @@ -166,10 +163,9 @@ def hit_record( yield hit try: - with pg_config.make_connection() as conn: - with conn.cursor() as c: - c.execute("DELETE FROM mtwerk_hit WHERE id=%s", (hit.id,)) - conn.commit() + with pg_config.make_connection() as conn, conn.cursor() as c: + c.execute("DELETE FROM mtwerk_hit WHERE id=%s", (hit.id,)) + conn.commit() except ForeignKeyViolation: pass # DB gets dropped anyway, don't care @@ -228,12 +224,11 @@ def assignment_stub_record( yield assignment_stub try: - with pg_config.make_connection() as conn: - with conn.cursor() as c: - c.execute( - "DELETE FROM mtwerk_assignment WHERE id=%s", (assignment_stub.id,) - ) - conn.commit() + with pg_config.make_connection() as conn, conn.cursor() as c: + c.execute( + "DELETE FROM mtwerk_assignment WHERE id=%s", (assignment_stub.id,) + ) + conn.commit() except ForeignKeyViolation: pass # DB gets dropped anyway, don't care @@ -255,19 +250,18 @@ def assignment_record( yield assignment try: - with pg_config.make_connection() as conn: - with conn.cursor() as c: - c.execute("DELETE FROM mtwerk_assignment WHERE id=%s", (assignment.id,)) - conn.commit() + with pg_config.make_connection() as conn, conn.cursor() as c: + c.execute("DELETE FROM mtwerk_assignment WHERE id=%s", (assignment.id,)) + conn.commit() except ForeignKeyViolation: pass # DB gets dropped anyway, don't care @pytest.fixture -def assignment_factory(hit_record: Hit) -> Callable[[Optional[str]], Assignment]: +def assignment_factory(hit_record: Hit) -> Callable[[str | None], Assignment]: - def _inner(amt_worker_id: Optional[str] = None) -> Assignment: + def _inner(amt_worker_id: str | None = None) -> Assignment: now = datetime.now(tz=timezone.utc) amt_assignment_id = generate_amt_id() amt_worker_id = amt_worker_id or generate_amt_id() @@ -292,7 +286,7 @@ def assignment_record_factory( am: "AssignmentManager", assignment_factory: Callable[..., Assignment] ) -> Callable[..., Assignment]: - def _inner(hit_id: int, amt_worker_id: Optional[str] = None) -> Assignment: + def _inner(hit_id: int, amt_worker_id: str | None = None) -> Assignment: a = assignment_factory(amt_worker_id=amt_worker_id) a.hit_id = hit_id am.create_stub(a) |
