From bbc373bd2e9617c8da829b3a180e9c42f139a380 Mon Sep 17 00:00:00 2001 From: Max Nanis Date: Thu, 10 Sep 2026 10:27:51 -0700 Subject: Less from init, more from generalresearch, basic db test from shared conftest --- tests/test_postgres.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/test_postgres.py (limited to 'tests/test_postgres.py') 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"]) -- cgit v1.2.3