diff options
| author | Max Nanis | 2026-09-10 10:27:51 -0700 |
|---|---|---|
| committer | Max Nanis | 2026-09-10 10:27:51 -0700 |
| commit | bbc373bd2e9617c8da829b3a180e9c42f139a380 (patch) | |
| tree | 18fea752398e948ee5ddbc57f3ab871e8caae5fb /tests | |
| parent | 4dca7296742b607e74f16e2f6484c51163a41ace (diff) | |
| download | amt-jb-bbc373bd2e9617c8da829b3a180e9c42f139a380.tar.gz amt-jb-bbc373bd2e9617c8da829b3a180e9c42f139a380.zip | |
Less from init, more from generalresearch, basic db test from shared conftest
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 9 | ||||
| -rw-r--r-- | tests/fixtures/managers.py | 3 | ||||
| -rw-r--r-- | tests/test_postgres.py | 53 |
3 files changed, 59 insertions, 6 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 002aced..457f6a3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,6 +22,7 @@ if TYPE_CHECKING: pytest_plugins = [ + "test_utils.conftest", "tests.fixtures.amt", "tests.fixtures.flow", "tests.fixtures.http", @@ -83,7 +84,7 @@ def pe_id() -> str: @pytest.fixture(scope="session") -def settings() -> "Settings": +def settings() -> Settings: from jb.settings import Settings as JBSettings return JBSettings() @@ -176,7 +177,7 @@ def django_db_factory( @pytest.fixture(scope="session") -def pg_config(settings: "Settings") -> PostgresConfig: +def pg_config(settings: Settings) -> PostgresConfig: return PostgresConfig( dsn=settings.amt_jb_db, connect_timeout=1, @@ -188,7 +189,7 @@ def pg_config(settings: "Settings") -> PostgresConfig: @pytest.fixture(scope="session") -def redis(settings: "Settings"): +def redis(settings: Settings): from generalresearch.redis_helper import RedisConfig redis_config = RedisConfig( @@ -202,7 +203,7 @@ def redis(settings: "Settings"): # --- Connectors --- @pytest.fixture(scope="session") -def amt_client(settings: "Settings") -> MTurkClient: +def amt_client(settings: Settings) -> MTurkClient: import boto3 client = boto3.client( diff --git a/tests/fixtures/managers.py b/tests/fixtures/managers.py index 22eae5e..8f87e5d 100644 --- a/tests/fixtures/managers.py +++ b/tests/fixtures/managers.py @@ -1,11 +1,10 @@ from typing import TYPE_CHECKING import pytest +from generalresearch.managers.base 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.amt import AMTManager from jb.managers.assignment import AssignmentManager diff --git a/tests/test_postgres.py b/tests/test_postgres.py new file mode 100644 index 0000000..6db4f82 --- /dev/null +++ b/tests/test_postgres.py @@ -0,0 +1,53 @@ +import socket +import subprocess +from collections.abc import Callable +from typing import TYPE_CHECKING + +from generalresearch.pg_helper import PostgresConfig +from pydantic import PostgresDsn + +if TYPE_CHECKING: + from generalresearch.models.custom_types import InternalHostname, PostgresDict + + +def is_port_open(host: InternalHostname, port: int = 5432, timeout: int = 3): + try: + with socket.create_connection((host, port), timeout=timeout): + return True + except (TimeoutError, ConnectionRefusedError, OSError): + return False + + +def can_ping(host: InternalHostname): + return ( + subprocess.call( + ["ping", "-c", "1", str(host)], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + == 0 + ) + + +class TestPostgresDSN: + + def test_ping(self, postgres_instance_host: InternalHostname): + assert can_ping(host=postgres_instance_host) + + def test_port(self, postgres_instance_host: InternalHostname): + assert is_port_open(host=postgres_instance_host) + + def test_conn(self, postgres_instance: PostgresDsn): + config = PostgresConfig( + dsn=postgres_instance, + connect_timeout=1, + statement_timeout=1, + ) + res = config.execute_sql_query(query="SELECT 1;") + assert len(res) == 1 + + +class TestPostgresDjangoCreation: + + def test_ping(self, postgres_instance_dict: PostgresDict): + assert can_ping(host=postgres_instance_dict["host"]) |
