aboutsummaryrefslogtreecommitdiff
path: root/tests/test_postgres.py
blob: de3f5d8efadab60c87ba69225ba5e540879e9a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import socket
import subprocess
from collections.abc import Callable
from typing import TYPE_CHECKING

from pydantic import PostgresDsn

from generalresearch.pg_helper import PostgresConfig

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"])

    def test_django_creation(
        self,
        django_db_factory: Callable[..., None],
    ):

        dsn = django_db_factory()
        assert isinstance(dsn, PostgresDsn)

    def test_django_tables(self, thl_web_rw: PostgresConfig):
        res = thl_web_rw.execute_sql_query(query="""
            SELECT COUNT(*)
            FROM information_schema.tables
            WHERE table_schema = 'public';
        """)
        assert len(res) == 1
        assert res[0]["count"] == 57

    def test_django_tables_only_gr(self, gr_db: PostgresConfig):
        """
        IMPORTANT: This can't really run with only the GR tables,
            that's because we have most of the database init fixtures
            as session scoped; and we can't ensure that this will
            run before any test that depends on the core thl
            migrations
        """

        res = gr_db.execute_sql_query(query="""
            SELECT COUNT(*)
            FROM information_schema.tables
            WHERE table_schema = 'public';
        """)
        assert len(res) == 1
        assert res[0]["count"] == 65

    def test_django_tables_with_gr(
        self, thl_web_rw: PostgresConfig, gr_db: PostgresConfig
    ):
        res = thl_web_rw.execute_sql_query(query="""
            SELECT COUNT(*) 
            FROM information_schema.tables
            WHERE table_schema = 'public';
        """)
        assert len(res) == 1
        assert res[0]["count"] == 65