aboutsummaryrefslogtreecommitdiff
path: root/tests/fixtures
diff options
context:
space:
mode:
authorMax Nanis2026-09-13 19:03:55 +0000
committerMax Nanis2026-09-13 19:03:55 +0000
commit8fbe8d439b418796932aaa88297e006c714e4d13 (patch)
tree6c800476edc1e771fc570b559d483df8d59d4324 /tests/fixtures
parent12f6fee851b68e86af658dfa17e4a0daed457dd1 (diff)
parent2c94f248d2438071a918fa9a30bf114ef9aa29b4 (diff)
downloadamt-jb-8fbe8d439b418796932aaa88297e006c714e4d13.tar.gz
amt-jb-8fbe8d439b418796932aaa88297e006c714e4d13.zip
Merges pull request #3
Off of Amazon!!!
Diffstat (limited to 'tests/fixtures')
-rw-r--r--tests/fixtures/amt.py11
-rw-r--r--tests/fixtures/flow.py43
-rw-r--r--tests/fixtures/http.py39
-rw-r--r--tests/fixtures/managers.py9
-rw-r--r--tests/fixtures/models.py68
5 files changed, 86 insertions, 84 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 dd2f83e..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 generalresearchutils.models.thl.payout import UserPayoutEvent
-from generalresearchutils.models.thl.wallet import PayoutType
-from generalresearchutils.models.thl.wallet.cashout_method import (
- CashoutRequestResponse,
+from generalresearch.currency import USDCent
+from generalresearch.models.thl.definitions import PayoutStatus
+from generalresearch.models.thl.payout import UserPayoutEvent
+from generalresearch.models.thl.wallet.cashout_method import (
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 generalresearchutils.currency import USDCent
-from generalresearchutils.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..e38c853 100644
--- a/tests/fixtures/http.py
+++ b/tests/fixtures/http.py
@@ -1,20 +1,20 @@
+import json
+import secrets
+from collections.abc import AsyncGenerator
+from typing import Any
+
import httpx
-import redis
import pytest
import requests_mock
from asgi_lifespan import LifespanManager
-from httpx import AsyncClient, ASGITransport
-from typing import Dict, Any, AsyncGenerator
+from generalresearch.redis_helper import RedisConfig
+from httpx import ASGITransport, AsyncClient
+from jb.config import JB_EVENTS_STREAM, settings
+from jb.decorators import get_redis_config
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
@@ -24,7 +24,8 @@ def anyio_backend():
@pytest.fixture(scope="session")
-async def httpxclient() -> AsyncGenerator[AsyncClient, None]:
+async def httpxclient(redis_config: RedisConfig) -> AsyncGenerator[AsyncClient, None]:
+ app.dependency_overrides[get_redis_config] = lambda: redis_config
# limiter.enabled = True
# limiter.reset()
app.testing = True
@@ -38,6 +39,8 @@ async def httpxclient() -> AsyncGenerator[AsyncClient, None]:
yield client
await client.aclose()
+ app.dependency_overrides.clear()
+
@pytest.fixture()
def no_limit():
@@ -69,7 +72,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(
@@ -93,9 +96,11 @@ def mturk_event_body_record(
@pytest.fixture()
-def clean_mturk_events_redis_stream(redis: redis.Redis):
- redis.xtrim(JB_EVENTS_STREAM, maxlen=0)
- assert redis.xlen(JB_EVENTS_STREAM) == 0
+def clean_mturk_events_redis_stream(redis_config: RedisConfig):
+ redis_client = redis_config.create_redis_client()
+
+ redis_client.xtrim(JB_EVENTS_STREAM, maxlen=0)
+ assert redis_client.xlen(JB_EVENTS_STREAM) == 0
yield
- redis.xtrim(JB_EVENTS_STREAM, maxlen=0)
- assert redis.xlen(JB_EVENTS_STREAM) == 0
+ redis_client.xtrim(JB_EVENTS_STREAM, maxlen=0)
+ assert redis_client.xlen(JB_EVENTS_STREAM) == 0
diff --git a/tests/fixtures/managers.py b/tests/fixtures/managers.py
index a3187d7..8f87e5d 100644
--- a/tests/fixtures/managers.py
+++ b/tests/fixtures/managers.py
@@ -1,14 +1,15 @@
from typing import TYPE_CHECKING
+
import pytest
-from jb.managers import Permission
-from generalresearchutils.pg_helper import PostgresConfig
+from generalresearch.managers.base import Permission
+from generalresearch.pg_helper import PostgresConfig
from mypy_boto3_mturk import MTurkClient
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 671c7b3..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 generalresearch.currency import USDCent
+from generalresearch.pg_helper import PostgresConfig
+from psycopg.errors import ForeignKeyViolation
-from jb.models.event import MTurkEvent
-from generalresearchutils.pg_helper import PostgresConfig
-
-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 generalresearchutils.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)