aboutsummaryrefslogtreecommitdiff
path: root/test_utils
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils')
-rw-r--r--test_utils/conftest.py91
-rw-r--r--test_utils/grliq/conftest.py8
-rw-r--r--test_utils/incite/collections/conftest.py3
-rw-r--r--test_utils/incite/conftest.py7
-rw-r--r--test_utils/incite/mergers/conftest.py2
-rw-r--r--test_utils/managers/conftest.py2
-rw-r--r--test_utils/managers/gr/conftest.py2
-rw-r--r--test_utils/managers/thl/conftest.py2
-rw-r--r--test_utils/managers/upk/conftest.py2
-rw-r--r--test_utils/models/conftest.py7
-rw-r--r--test_utils/models/contest/conftest.py7
-rw-r--r--test_utils/models/gr/conftest.py2
-rw-r--r--test_utils/models/ledger/conftest.py3
-rw-r--r--test_utils/models/network/conftest.py6
-rw-r--r--test_utils/models/thl/conftest.py17
-rw-r--r--test_utils/spectrum/conftest.py16
16 files changed, 103 insertions, 74 deletions
diff --git a/test_utils/conftest.py b/test_utils/conftest.py
index 378b9cc..ffe458c 100644
--- a/test_utils/conftest.py
+++ b/test_utils/conftest.py
@@ -6,16 +6,17 @@ import stat
import subprocess
import sys
import tempfile
-from datetime import datetime, timedelta, timezone
+from collections.abc import Callable, Generator
+from datetime import UTC, datetime, timedelta, timezone
from os.path import join as pjoin
from pathlib import Path
-from typing import Callable, Generator
from uuid import uuid4
import pytest
from _pytest.config import Config
from dotenv import load_dotenv
from pydantic import MariaDBDsn, PostgresDsn, TypeAdapter
+from pytest import TempPathFactory
from generalresearch.config import GRLBaseSettings
from generalresearch.currency import USDCent
@@ -93,7 +94,7 @@ def postgres_instance(settings: GRLBaseSettings) -> Generator[PostgresDsn]:
from psycopg import connect
from psycopg.sql import SQL, Identifier
- now = datetime.now(timezone.utc)
+ now = datetime.now(UTC)
ts: str = now.strftime("%Y-%m-%d")
db_name = f"unittest-{ts}-{uuid4().hex[:6]}"
@@ -152,38 +153,48 @@ def postgres_instance_host(
yield value
-# @pytest.fixture(scope="session")
-# def git_key_path(settings: GRLBaseSettings) -> Path:
-# return Path('/tmp/')
-
-
@pytest.fixture(scope="session")
def git_key_path(
+ tmp_path_factory: TempPathFactory,
settings: GRLBaseSettings,
) -> Generator[Path]:
+ # We are using the tmp_path_factory because unlike the tmp_path (which
+ # is function scoped), this is session scoped.
- assert settings.git_creds
- with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_id_rsa") as f:
- f.write(settings.git_creds)
- key_path = f.name
-
- os.chmod(key_path, stat.S_IRUSR | stat.S_IWUSR)
+ assert settings.git_creds, "Must define key to download alternative models"
+ fn = tmp_path_factory.mktemp("keys") / "git_creds"
+ fn.write_text(settings.git_creds, encoding="utf-8")
+ os.chmod(fn, stat.S_IRUSR | stat.S_IWUSR)
- yield Path(key_path)
+ yield Path(fn)
- os.unlink(key_path)
+ os.unlink(fn)
@pytest.fixture(scope="session")
-def gr_repo(git_key_path: Path) -> Callable[..., Path]:
+def gr_repo(
+ git_key_path: Path,
+ tmp_path_factory: TempPathFactory,
+) -> Callable[..., Path | None]:
repo_url = "ssh://code.g-r-l.com/general-research/gr-carer.git"
- repo_path = Path("/tmp/gr-carer")
+
+ _ran = {}
+ if _ran.get(repo_url, False):
+ print(f"Already ran django_db_factory.{repo_url}")
+ return
+
+ _ran[repo_url] = True
+
+ fn = tmp_path_factory.mktemp("repos")
+ repo_path = fn / "gr-carer"
+ repo_path.mkdir(parents=True, exist_ok=True)
def _inner() -> Path:
+
ssh_cmd = (
f"ssh -i {git_key_path} "
"-o IdentitiesOnly=yes "
- "-o StrictHostKeyChecking=no " # or accept-new, see note below
+ "-o StrictHostKeyChecking=no "
)
env = {"GIT_SSH_COMMAND": ssh_cmd}
@@ -196,6 +207,11 @@ def gr_repo(git_key_path: Path) -> Callable[..., Path]:
env=env,
)
+ result = subprocess.run(
+ ["cat", git_key_path], capture_output=True, text=True, check=False
+ )
+ print(repr(result.stdout))
+
return repo_path
return _inner
@@ -206,21 +222,29 @@ def django_db_factory(
postgres_instance: PostgresDsn,
postgres_instance_dict: PostgresDict,
gr_repo: Callable[..., Path],
-) -> Callable[..., PostgresDsn]:
+) -> Callable[..., PostgresDsn | None]:
+
+ _ran = {}
import django
+ from django.apps import apps
from django.conf import settings as django_settings
from django.core.management import call_command
- def _inner(django_project: str = "generalresearch.thl_django"):
+ 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
+ _ran[django_project] = True
if "gr" in django_project:
# We need model files that are NOT in this repo.
gr_path = gr_repo()
sys.path.insert(0, str(gr_path))
- print(sys.path)
-
# 1. Bootstrapping Django settings
if not django_settings.configured:
django_settings.configure(
@@ -242,10 +266,11 @@ def django_db_factory(
)
django.setup()
- # for model in apps.get_models():
- # print(f"Discovered model: {model._meta.label}")
+ for model in apps.get_models():
+ print(f"Discovered model: {model._meta.label}")
# 2. Run migrations directly during fixture activation
+ call_command("makemigrations", "gr", interactive=False)
call_command("migrate")
# 3. Return the Dsn so the factory gives a way to connect
@@ -276,37 +301,37 @@ def spectrum_rw(settings: GRLBaseSettings) -> SqlHelper:
@pytest.fixture
def start() -> datetime:
- return datetime(year=1900, month=1, day=1, tzinfo=timezone.utc)
+ return datetime(year=1900, month=1, day=1, tzinfo=UTC)
@pytest.fixture
def utc_now() -> datetime:
- return datetime.now(tz=timezone.utc)
+ return datetime.now(tz=UTC)
@pytest.fixture
def utc_hour_ago() -> datetime:
- return datetime.now(tz=timezone.utc) - timedelta(hours=1)
+ return datetime.now(tz=UTC) - timedelta(hours=1)
@pytest.fixture
def utc_day_ago() -> datetime:
- return datetime.now(tz=timezone.utc) - timedelta(hours=24)
+ return datetime.now(tz=UTC) - timedelta(hours=24)
@pytest.fixture
def utc_90days_ago() -> datetime:
- return datetime.now(tz=timezone.utc) - timedelta(days=90)
+ return datetime.now(tz=UTC) - timedelta(days=90)
@pytest.fixture
def utc_60days_ago() -> datetime:
- return datetime.now(tz=timezone.utc) - timedelta(days=60)
+ return datetime.now(tz=UTC) - timedelta(days=60)
@pytest.fixture
def utc_30days_ago() -> datetime:
- return datetime.now(tz=timezone.utc) - timedelta(days=30)
+ return datetime.now(tz=UTC) - timedelta(days=30)
# === Clean up ===
@@ -322,7 +347,7 @@ def delete_df_collection(
DFCollectionType,
)
- def _inner(coll: "DFCollection"):
+ def _inner(coll: DFCollection):
match coll.data_type:
case DFCollectionType.LEDGER:
for table in [
diff --git a/test_utils/grliq/conftest.py b/test_utils/grliq/conftest.py
index e8175a5..7665b52 100644
--- a/test_utils/grliq/conftest.py
+++ b/test_utils/grliq/conftest.py
@@ -1,7 +1,7 @@
from __future__ import annotations
-from datetime import datetime, timedelta, timezone
-from typing import Callable
+from collections.abc import Callable
+from datetime import UTC, datetime, timedelta, timezone
from uuid import uuid4
import pytest
@@ -83,7 +83,7 @@ def grliq_data() -> GrlIqData:
g.id = None
g.uuid = uuid4().hex
- g.created_at = datetime.now(tz=timezone.utc)
+ g.created_at = datetime.now(tz=UTC)
g.timestamp = g.created_at - timedelta(seconds=10)
return g
@@ -117,7 +117,7 @@ def grliq_data_factory(grliq_dm: GrlIqDataManager) -> Callable[..., GrlIqData]:
product_user_id = product_user_id or uuid4().hex
uuid = uuid or uuid4().hex
mid = mid or uuid4().hex
- created_at = created_at or datetime.now(tz=timezone.utc)
+ created_at = created_at or datetime.now(tz=UTC)
res["data"].product_id = product_id
res["data"].product_user_id = product_user_id
diff --git a/test_utils/incite/collections/conftest.py b/test_utils/incite/collections/conftest.py
index 88eef72..631bb7b 100644
--- a/test_utils/incite/collections/conftest.py
+++ b/test_utils/incite/collections/conftest.py
@@ -1,7 +1,8 @@
from __future__ import annotations
+from collections.abc import Callable
from datetime import datetime, timedelta
-from typing import TYPE_CHECKING, Callable
+from typing import TYPE_CHECKING
import pytest
diff --git a/test_utils/incite/conftest.py b/test_utils/incite/conftest.py
index 12e57c5..87ea7ae 100644
--- a/test_utils/incite/conftest.py
+++ b/test_utils/incite/conftest.py
@@ -1,11 +1,12 @@
from __future__ import annotations
-from datetime import datetime, timedelta, timezone
+from collections.abc import Callable
+from datetime import UTC, datetime, timedelta, timezone
from os.path import join as pjoin
from pathlib import Path
from random import choice as randchoice
from shutil import rmtree
-from typing import TYPE_CHECKING, Callable
+from typing import TYPE_CHECKING
from uuid import uuid4
import pytest
@@ -166,7 +167,7 @@ def incite_item_factory(
for _ in range(5):
item_time = fake.date_time_between(
- start_date=item.start, end_date=item.finish, tzinfo=timezone.utc
+ start_date=item.start, end_date=item.finish, tzinfo=UTC
)
match data_type:
diff --git a/test_utils/incite/mergers/conftest.py b/test_utils/incite/mergers/conftest.py
index e9970c2..c0f0bcf 100644
--- a/test_utils/incite/mergers/conftest.py
+++ b/test_utils/incite/mergers/conftest.py
@@ -1,7 +1,7 @@
from __future__ import annotations
+from collections.abc import Callable
from datetime import datetime, timedelta
-from typing import Callable
import pytest
diff --git a/test_utils/managers/conftest.py b/test_utils/managers/conftest.py
index d2e5d20..4dacb29 100644
--- a/test_utils/managers/conftest.py
+++ b/test_utils/managers/conftest.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Callable
+from collections.abc import Callable
import pytest
diff --git a/test_utils/managers/gr/conftest.py b/test_utils/managers/gr/conftest.py
index 37da164..4da8fe3 100644
--- a/test_utils/managers/gr/conftest.py
+++ b/test_utils/managers/gr/conftest.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Callable
+from collections.abc import Callable
import pytest
import redis.asyncio as redis_async
diff --git a/test_utils/managers/thl/conftest.py b/test_utils/managers/thl/conftest.py
index 5b70961..21b2007 100644
--- a/test_utils/managers/thl/conftest.py
+++ b/test_utils/managers/thl/conftest.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Callable
+from collections.abc import Callable
import pytest
from pydantic import PostgresDsn
diff --git a/test_utils/managers/upk/conftest.py b/test_utils/managers/upk/conftest.py
index d8f956c..7eabee1 100644
--- a/test_utils/managers/upk/conftest.py
+++ b/test_utils/managers/upk/conftest.py
@@ -1,4 +1,4 @@
-from typing import Callable, Generator
+from collections.abc import Callable, Generator
import pytest
diff --git a/test_utils/models/conftest.py b/test_utils/models/conftest.py
index 93e2f44..3a9e45c 100644
--- a/test_utils/models/conftest.py
+++ b/test_utils/models/conftest.py
@@ -1,10 +1,11 @@
from __future__ import annotations
-from datetime import datetime, timedelta, timezone
+from collections.abc import Callable
+from datetime import UTC, datetime, timedelta, timezone
from decimal import Decimal
from random import choice as randchoice
from random import randint
-from typing import TYPE_CHECKING, Callable
+from typing import TYPE_CHECKING
from uuid import uuid4
import pytest
@@ -124,7 +125,7 @@ def wall_factory(wall_manager: WallManager) -> Callable[..., Wall]:
) -> Wall:
assert session.started <= datetime.now(
- tz=timezone.utc
+ tz=UTC
), "Session can't start in the future"
if session.wall_events:
diff --git a/test_utils/models/contest/conftest.py b/test_utils/models/contest/conftest.py
index bfbe9f8..0060946 100644
--- a/test_utils/models/contest/conftest.py
+++ b/test_utils/models/contest/conftest.py
@@ -1,14 +1,13 @@
from __future__ import annotations
-from datetime import datetime, timezone
+from collections.abc import Callable
+from datetime import UTC, datetime, timezone
from decimal import Decimal
-from typing import Callable
from uuid import uuid4
import pytest
from pytest import FixtureRequest as Request
-
from generalresearch.currency import USDCent
from generalresearch.managers.thl.contest_manager import ContestManager
from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
@@ -136,7 +135,7 @@ def milestone_contest_create() -> MilestoneContestCreate:
),
],
end_condition=MilestoneContestEndCondition(
- ends_at=datetime(year=2030, month=1, day=1, tzinfo=timezone.utc),
+ ends_at=datetime(year=2030, month=1, day=1, tzinfo=UTC),
max_winners=5,
),
entry_trigger=ContestEntryTrigger.TASK_COMPLETE,
diff --git a/test_utils/models/gr/conftest.py b/test_utils/models/gr/conftest.py
index df97306..90b86aa 100644
--- a/test_utils/models/gr/conftest.py
+++ b/test_utils/models/gr/conftest.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Callable
+from collections.abc import Callable
from uuid import uuid4
import pytest
diff --git a/test_utils/models/ledger/conftest.py b/test_utils/models/ledger/conftest.py
index 14a7465..b428468 100644
--- a/test_utils/models/ledger/conftest.py
+++ b/test_utils/models/ledger/conftest.py
@@ -1,9 +1,10 @@
from __future__ import annotations
+from collections.abc import Callable
from datetime import datetime
from decimal import Decimal
from random import randint
-from typing import TYPE_CHECKING, Callable
+from typing import TYPE_CHECKING
from uuid import uuid4
import pytest
diff --git a/test_utils/models/network/conftest.py b/test_utils/models/network/conftest.py
index bebc691..cabd8dc 100644
--- a/test_utils/models/network/conftest.py
+++ b/test_utils/models/network/conftest.py
@@ -1,5 +1,5 @@
import os
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta, timezone
from uuid import uuid4
import pytest
@@ -90,7 +90,7 @@ def rdns_result(dig_raw_output: str) -> RDNSResult:
def rdns_run(rdns_result: RDNSResult, scan_group_id: str):
r = rdns_result
ip = "45.33.32.156"
- utc_now = datetime.now(tz=timezone.utc)
+ utc_now = datetime.now(tz=UTC)
config = RDNSRunCommand(command="dig", options=RDNSRunCommandOptions(ip=ip))
return RDNSRun(
tool_version="1.2.3",
@@ -121,7 +121,7 @@ def mtr_result(mtr_raw_output: str) -> MTRResult:
@pytest.fixture(scope="session")
def mtr_run(mtr_result: MTRResult, scan_group_id: str):
r = mtr_result
- utc_now = datetime.now(tz=timezone.utc)
+ utc_now = datetime.now(tz=UTC)
config = MTRRunCommand(
command="mtr",
options=MTRRunCommandOptions(
diff --git a/test_utils/models/thl/conftest.py b/test_utils/models/thl/conftest.py
index cf8d2fa..a2adcce 100644
--- a/test_utils/models/thl/conftest.py
+++ b/test_utils/models/thl/conftest.py
@@ -1,11 +1,12 @@
from __future__ import annotations
-from datetime import datetime, timezone
+from collections.abc import Callable
+from datetime import UTC, datetime, timezone
from decimal import ROUND_DOWN, Decimal
from random import choice as rand_choice
from random import choice as rchoice
from random import randint, random
-from typing import Any, Callable
+from typing import Any
from uuid import uuid4
import faker
@@ -105,9 +106,9 @@ def wall_factory(
user_id = user_id or fake.random_int(min=1, max=2_147_483_648)
started = started or fake.date_time_between(
- start_date=datetime(year=1900, month=1, day=1, tzinfo=timezone.utc),
- end_date=datetime.now(tz=timezone.utc),
- tzinfo=timezone.utc,
+ start_date=datetime(year=1900, month=1, day=1, tzinfo=UTC),
+ end_date=datetime.now(tz=UTC),
+ tzinfo=UTC,
)
if session_id is None:
@@ -199,9 +200,9 @@ def session_factory(session_manager: SessionManager):
) -> Session:
"""To be used in tests, where we don't care about certain fields"""
started = started or fake.date_time_between(
- start_date=datetime(year=1900, month=1, day=1, tzinfo=timezone.utc),
- end_date=datetime(year=2000, month=1, day=1, tzinfo=timezone.utc),
- tzinfo=timezone.utc,
+ start_date=datetime(year=1900, month=1, day=1, tzinfo=UTC),
+ end_date=datetime(year=2000, month=1, day=1, tzinfo=UTC),
+ tzinfo=UTC,
)
user = user or User(
user_id=fake.random_int(min=1, max=2_147_483_648), uuid=uuid4().hex
diff --git a/test_utils/spectrum/conftest.py b/test_utils/spectrum/conftest.py
index 0afc3f5..9c067d3 100644
--- a/test_utils/spectrum/conftest.py
+++ b/test_utils/spectrum/conftest.py
@@ -1,6 +1,6 @@
import logging
import time
-from datetime import datetime, timezone
+from datetime import UTC, datetime, timezone
from typing import TYPE_CHECKING
import pytest
@@ -19,7 +19,7 @@ if TYPE_CHECKING:
@pytest.fixture(scope="session")
-def spectrum_rw(settings: "GRLBaseSettings") -> SqlHelper:
+def spectrum_rw(settings: GRLBaseSettings) -> SqlHelper:
logging.info(f"{settings.spectrum_rw_db=}")
assert settings.spectrum_rw_db is not None
@@ -49,11 +49,11 @@ def spectrum_survey_manager(spectrum_rw: SqlHelper) -> SpectrumSurveyManager:
def setup_spectrum_surveys(
spectrum_rw: SqlHelper, spectrum_survey_manager, spectrum_criteria_manager
) -> None:
- now = datetime.now(timezone.utc)
+ now = datetime.now(UTC)
# make sure these example surveys exist in db
surveys = [SpectrumSurvey.model_validate_json(x) for x in SURVEYS_JSON]
for s in surveys:
- s.modified_api = datetime.now(tz=timezone.utc)
+ s.modified_api = datetime.now(tz=UTC)
spectrum_survey_manager.create_or_update(surveys)
spectrum_criteria_manager.update(CONDITIONS)
@@ -66,10 +66,10 @@ def setup_spectrum_surveys(
["687", "GRL", "x", "x", "x", "x"],
commit=True,
)
- supplier687_pk = spectrum_rw.execute_sql_query(
- f"""
- select id from `{spectrum_rw.db}`.spectrum_supplier where supplier_id = '687'"""
- )[0]["id"]
+ supplier687_pk = spectrum_rw.execute_sql_query(f"""
+ select id from `{spectrum_rw.db}`.spectrum_supplier where supplier_id = '687'""")[
+ 0
+ ]["id"]
conn = spectrum_rw.make_connection()
c = conn.cursor()
c.executemany(