aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMax Nanis2026-02-26 20:29:41 -0500
committerMax Nanis2026-02-26 20:29:41 -0500
commitaf66829e26cb05f182bef36ac06d58c7baa0ec1e (patch)
tree2848a9223e7d4d680f3e93fc8dfcc7545f716abd /tests
parent0bf32fadd85d5938ae29d489efdd82e2cd137300 (diff)
downloadamt-jb-af66829e26cb05f182bef36ac06d58c7baa0ec1e.tar.gz
amt-jb-af66829e26cb05f182bef36ac06d58c7baa0ec1e.zip
AMTManager moved to fixture, and dectorator with parameters on tasks and init / non-static class appraoch. More assertion checks and typing. TestMTurkClient seperated from TestAMTManger
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py7
-rw-r--r--tests/fixtures/managers.py10
-rw-r--r--tests/managers/test_amt.py84
-rw-r--r--tests/managers/test_hit.py14
4 files changed, 97 insertions, 18 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 5138f49..25eb457 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -124,7 +124,7 @@ def pg_config(settings: "Settings") -> PostgresConfig:
def amt_client(settings: "Settings") -> MTurkClient:
import boto3
- return boto3.client(
+ client = boto3.client(
service_name="mturk",
region_name="us-east-1",
endpoint_url=str(settings.amt_endpoint),
@@ -132,3 +132,8 @@ def amt_client(settings: "Settings") -> MTurkClient:
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
diff --git a/tests/fixtures/managers.py b/tests/fixtures/managers.py
index 6bc2e51..a3187d7 100644
--- a/tests/fixtures/managers.py
+++ b/tests/fixtures/managers.py
@@ -2,14 +2,24 @@ from typing import TYPE_CHECKING
import pytest
from jb.managers import Permission
from generalresearchutils.pg_helper import PostgresConfig
+from mypy_boto3_mturk import MTurkClient
if TYPE_CHECKING:
from jb.managers.hit import HitQuestionManager, HitTypeManager, HitManager
from jb.managers.assignment import AssignmentManager
from jb.managers.bonus import BonusManager
+ from jb.managers.amt import AMTManager
# --- Managers ---
+@pytest.fixture(scope="session")
+def amtm(amt_client: MTurkClient) -> "AMTManager":
+ from jb.managers.amt import AMTManager
+
+ amtm = AMTManager(amt_client=amt_client)
+ assert "mturk-requester-sandbox" in amtm.amt_client.meta.endpoint_url
+
+ return amtm
@pytest.fixture(scope="session")
diff --git a/tests/managers/test_amt.py b/tests/managers/test_amt.py
index 63d3737..372cb07 100644
--- a/tests/managers/test_amt.py
+++ b/tests/managers/test_amt.py
@@ -2,35 +2,101 @@ from jb.managers.amt import AMTManager
from jb.models.hit import HitType, HitQuestion
from jb.managers.hit import HitQuestionManager, HitTypeManager, HitManager
+from mypy_boto3_mturk import MTurkClient
+from mypy_boto3_mturk.type_defs import (
+ GetAssignmentResponseTypeDef,
+ GetAccountBalanceResponseTypeDef,
+ ListHITsResponseTypeDef,
+)
+
+
+class TestMTurkClient:
+
+ def test_sandbox(self, amt_client: MTurkClient):
+ assert amt_client is not None
+ assert "mturk-requester-sandbox" in amt_client.meta.endpoint_url
+
+ balance: GetAccountBalanceResponseTypeDef = amt_client.get_account_balance()
+ assert balance["AvailableBalance"] == "10000.00"
+
+ # def test_create_hit()
+
+ # def test_delete_hit()
+
+ # def test_get_assignment(self, amt_client: MTurkClient):
+
+ # def test_get_hit
+
+ def test_list_hits(self, amt_client: MTurkClient, amtm: AMTManager):
+ res: ListHITsResponseTypeDef = amt_client.list_hits()
+ assert isinstance(res, dict)
+ assert isinstance(res.get("HITs"), list)
+
+ for hit in res["HITs"]:
+ assert isinstance(hit, dict)
+
+ assert isinstance(hit.get("HITId"), str)
+ assert isinstance(hit.get("HITTypeId"), str)
+ assert isinstance(hit.get("HITGroupId"), str)
+ assert isinstance(hit.get("Title"), str)
+
+ # botocore.errorfactory.RequestError: An error occurred
+ # (RequestError) when calling the DeleteHIT operation: This
+ # HIT is currently in the state 'Assignable'. This operation
+ # can be called with a status of: Reviewing, Reviewable
+ # (1772146028380 s)
+
+ if hit["HITStatus"] in ["Reviewing", "Reviewable"]:
+ print(
+ f"Deleting HIT with id {hit['HITId']} and status {hit['HITStatus']}"
+ )
+ amt_client.delete_hit(HITId=hit["HITId"])
+
+ elif hit["HITStatus"] == "Assignable":
+ # I don't know... just let expire I guess. To changes it's
+ # status you need to do manage Assignments (I think)
+
+ # amt_client.update_hit_review_status(HITId=hit["HITId"], Revert=True)
+ pass
+
+ amtm.expire_all_hits()
class TestAMTManager:
- def test_create_hit_type(self, hit_type: HitType):
+ def test_create_hit_type(self, amtm: AMTManager, hit_type: HitType):
assert hit_type.amt_hit_type_id is None
- AMTManager.create_hit_type(hit_type=hit_type)
+
+ amtm.create_hit_type(hit_type=hit_type)
assert hit_type.amt_hit_type_id is not None
def test_create_hit_with_hit_type(
self,
- hqm: HitQuestionManager,
+ amtm: AMTManager,
htm: HitTypeManager,
hm: HitManager,
hit_type_record_with_amt_id: HitType,
question_record: HitQuestion,
):
- hit_type = hit_type_record_with_amt_id
- hit_type = [
+ sandbox_amt_hit_type_id = hit_type_record_with_amt_id.amt_hit_type_id
+ assert isinstance(sandbox_amt_hit_type_id, str)
+
+ res = [
x
for x in htm.filter_active()
- if x.amt_hit_type_id == hit_type.amt_hit_type_id
- ][0]
+ if x.amt_hit_type_id == sandbox_amt_hit_type_id
+ ]
+
+ assert len(res) == 1
+ sandbox_hit_type = res[0]
- hit = AMTManager.create_hit_with_hit_type(
- hit_type=hit_type, question=question_record
+ hit = amtm.create_hit_with_hit_type(
+ hit_type=sandbox_hit_type, question=question_record
)
+
assert hit.amt_hit_id is not None
assert hit.id is None
+
hm.create(hit)
assert hit.id is not None
diff --git a/tests/managers/test_hit.py b/tests/managers/test_hit.py
index 56a4f53..974bd18 100644
--- a/tests/managers/test_hit.py
+++ b/tests/managers/test_hit.py
@@ -11,21 +11,19 @@ class TestHitQuestionManager:
class TestHitTypeManager:
- def test_create(self, htm: HitTypeManager, hit_type_record_with_amt_id: HitType):
+ def test_create(self, htm: HitTypeManager, hit_type_record: HitType):
- _ = hit_type_record_with_amt_id
-
- assert isinstance(hit_type_record_with_amt_id.id, int)
- assert isinstance(hit_type_record_with_amt_id.amt_hit_type_id, str)
+ assert isinstance(hit_type_record.id, int)
+ assert isinstance(hit_type_record.amt_hit_type_id, str)
count1 = len(htm.filter_active())
# assert count1 == 1
- hit_type_record_with_amt_id.min_active = 0
- htm.set_min_active(hit_type=hit_type_record_with_amt_id)
+ hit_type_record.min_active = 0
+ htm.set_min_active(hit_type=hit_type_record)
count2 = len(htm.filter_active())
- assert count2 == 0
+ assert count1 - 1 == count2
class TestHitManager: