Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alukach committed Sep 19, 2024
1 parent 10a68a5 commit d52a451
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
20 changes: 13 additions & 7 deletions space2stats_api/src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from moto import mock_aws
from pytest_postgresql.janitor import DatabaseJanitor

from space2stats.api.app import build_app


@pytest.fixture
def setup_benchmark_env(monkeypatch):
Expand Down Expand Up @@ -59,23 +61,27 @@ def database(postgresql_proc):
)
with psycopg.connect(db_url) as conn:
with conn.cursor() as cur:
cur.execute("""
cur.execute(
"""
CREATE TABLE IF NOT EXISTS space2stats (
hex_id TEXT PRIMARY KEY,
sum_pop_2020 INT,
sum_pop_f_10_2020 INT
);
""")
cur.execute("""
"""
)
cur.execute(
"""
INSERT INTO space2stats (hex_id, sum_pop_2020, sum_pop_f_10_2020)
VALUES ('hex_1', 100, 200), ('hex_2', 150, 250);
""")
"""
)

yield jan


@pytest.fixture(autouse=True)
def client(monkeypatch, database, test_bucket):
def mock_env(monkeypatch, database, test_bucket):
monkeypatch.setenv("PGHOST", database.host)
monkeypatch.setenv("PGPORT", str(database.port))
monkeypatch.setenv("PGDATABASE", database.dbname)
Expand All @@ -84,9 +90,9 @@ def client(monkeypatch, database, test_bucket):
monkeypatch.setenv("PGTABLENAME", "space2stats")
monkeypatch.setenv("S3_BUCKET_NAME", test_bucket)

from space2stats.api import build_app

@pytest.fixture
def client():
app = build_app()

with TestClient(app) as test_client:
yield test_client
37 changes: 37 additions & 0 deletions space2stats_api/src/tests/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from space2stats import StatsTable, Settings


def test_stats_table(mock_env):
with StatsTable.connect() as stats_table:
assert stats_table.table_name == "space2stats"
assert stats_table.conn.closed == 0
stats_table.conn.execute("SELECT 1")


def test_stats_table_connect(mock_env, database):
with StatsTable.connect(
PGHOST=database.host,
PGPORT=database.port,
PGDATABASE=database.dbname,
PGUSER=database.user,
PGPASSWORD=database.password,
PGTABLENAME="XYZ",
) as stats_table:
assert stats_table.table_name == "XYZ"
assert stats_table.conn.closed == 0
stats_table.conn.execute("SELECT 1")


def test_stats_table_settings(mock_env, database):
settings = Settings(
PGHOST=database.host,
PGPORT=database.port,
PGDATABASE=database.dbname,
PGUSER=database.user,
PGPASSWORD=database.password,
PGTABLENAME="ABC",
)
with StatsTable.connect(settings) as stats_table:
assert stats_table.table_name == "ABC"
assert stats_table.conn.closed == 0
stats_table.conn.execute("SELECT 1")

0 comments on commit d52a451

Please sign in to comment.