aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMax Nanis2026-09-10 01:00:39 -0700
committerMax Nanis2026-09-10 01:00:39 -0700
commit4dca7296742b607e74f16e2f6484c51163a41ace (patch)
tree0839c16d0905deb587d3ee25ff93fd3ccf7eeeb6 /tests
parent832aecaddce80e312095ecdb572d7756eb9df5e9 (diff)
downloadamt-jb-4dca7296742b607e74f16e2f6484c51163a41ace.tar.gz
amt-jb-4dca7296742b607e74f16e2f6484c51163a41ace.zip
using model_validator on GRLSettings. Allows null default values, then to asser them on load. Required so pydantic_settings can be loaded in tests without params
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py125
-rw-r--r--tests/http/test_auth.py4
2 files changed, 103 insertions, 26 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 2a3a580..002aced 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,12 +1,18 @@
+from __future__ import annotations
+
import os
+import subprocess
+import sys
+from collections.abc import Callable
+from pathlib import Path
from typing import TYPE_CHECKING
from uuid import uuid4
import pytest
-from _pytest.config import Config
-from dotenv import load_dotenv
-from generalresearch.pg_helper import PostgresConfig
+from generalresearch.models.custom_types import PostgresDict
+from generalresearch.pg_helper import PostgresConfig, PostgresDsn
from mypy_boto3_mturk import MTurkClient
+from pytest import TempPathFactory
from jb.decorators import CLIENT_CONFIG
from tests import generate_amt_id
@@ -77,26 +83,108 @@ def pe_id() -> str:
@pytest.fixture(scope="session")
-def env_file_path(pytestconfig: Config) -> str:
- root_path = pytestconfig.rootpath
- env_path = os.path.join(root_path, ".env.test")
+def settings() -> "Settings":
+ from jb.settings import Settings as JBSettings
+
+ return JBSettings()
- if os.path.exists(env_path):
- load_dotenv(dotenv_path=env_path, override=True)
- return env_path
+# --- Database Connectors ---
@pytest.fixture(scope="session")
-def settings(env_file_path: str) -> "Settings":
- from jb.settings import Settings as JBSettings
+def django_db_factory(
+ postgres_instance: PostgresDsn,
+ gr_repo: Callable[..., Path],
+ django_settings_file: Callable[..., tuple[str, Path]],
+ postgres_instance_dict: PostgresDict,
+ tmp_path_factory: TempPathFactory,
+) -> Callable[..., PostgresDsn | None]:
+
+ _ran = {}
+
+ def _inner(
+ django_project: str = "generalresearch.thl_django",
+ ) -> PostgresDsn | None:
+
+ if _ran.get(django_project, False):
+ print(f"Already ran django_db_factory:{django_project}")
+ return postgres_instance
+ _ran[django_project] = True
+
+ _cwd = None
+ _manage_path = "generalresearch.thl_django.app.manage"
+ _settings_module, _settings_dir = django_settings_file(
+ extra_installed_apps=[
+ "generalresearch.thl_django",
+ ],
+ )
+
+ pythonpath = str(_settings_dir)
+ if existing_pythonpath := os.environ.get("PYTHONPATH"):
+ pythonpath += os.pathsep + existing_pythonpath
+
+ env = {
+ **os.environ,
+ "DJANGO_SETTINGS_MODULE": _settings_module,
+ "PYTHONPATH": pythonpath,
+ }
+
+ # we check right after. if we check now, we won't print if bad
+ res1 = subprocess.run( # noqa: PLW1510
+ [
+ sys.executable,
+ "-m",
+ _manage_path,
+ "makemigrations",
+ f"--settings={_settings_module}",
+ ],
+ cwd=str(_cwd) if _cwd is not None else None,
+ env=env,
+ capture_output=True,
+ text=True,
+ )
+
+ if res1.returncode != 0:
+ print("STDOUT:", res1.stdout)
+ print("STDERR:", res1.stderr)
+ res1.check_returncode()
+
+ res2 = subprocess.run( # noqa: PLW1510
+ [
+ sys.executable,
+ "-m",
+ _manage_path,
+ "migrate",
+ f"--settings={_settings_module}",
+ ],
+ env=env,
+ cwd=str(_cwd) if _cwd is not None else None,
+ capture_output=True,
+ text=True,
+ )
+
+ if res2.returncode != 0:
+ print("STDOUT:", res2.stdout)
+ print("STDERR:", res2.stderr)
+ res2.check_returncode()
+
+ # 3. Return the Dsn so the factory gives a way to connect
+ return postgres_instance
+
+ return _inner
- s = JBSettings(_env_file=env_file_path)
- return s
+@pytest.fixture(scope="session")
+def pg_config(settings: "Settings") -> PostgresConfig:
+ return PostgresConfig(
+ dsn=settings.amt_jb_db,
+ connect_timeout=1,
+ statement_timeout=1,
+ )
-# --- Database Connectors ---
+# --- Redis ---
@pytest.fixture(scope="session")
@@ -112,15 +200,6 @@ def redis(settings: "Settings"):
return redis_config.create_redis_client()
-@pytest.fixture(scope="session")
-def pg_config(settings: "Settings") -> PostgresConfig:
- return PostgresConfig(
- dsn=settings.amt_jb_db,
- connect_timeout=1,
- statement_timeout=1,
- )
-
-
# --- Connectors ---
@pytest.fixture(scope="session")
def amt_client(settings: "Settings") -> MTurkClient:
diff --git a/tests/http/test_auth.py b/tests/http/test_auth.py
index ebda742..1fa1335 100644
--- a/tests/http/test_auth.py
+++ b/tests/http/test_auth.py
@@ -1,4 +1,3 @@
-import secrets
from urllib.parse import parse_qs, urlparse
import pytest
@@ -38,8 +37,7 @@ class FakeGRApiManager:
@pytest.fixture
def email() -> str:
- email = secrets.token_urlsafe(16) + "@gmail.com"
- return email.lower()
+ return "unittest@generalresearch.com"
@pytest.fixture