From d52a4515e4c9959b3c08d2465d66bbf4fb5eb889 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Wed, 18 Sep 2024 21:51:42 -0700 Subject: [PATCH] Add tests --- space2stats_api/src/tests/conftest.py | 20 ++++++++----- space2stats_api/src/tests/test_module.py | 37 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 space2stats_api/src/tests/test_module.py diff --git a/space2stats_api/src/tests/conftest.py b/space2stats_api/src/tests/conftest.py index a048902..b924a76 100644 --- a/space2stats_api/src/tests/conftest.py +++ b/space2stats_api/src/tests/conftest.py @@ -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): @@ -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) @@ -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 diff --git a/space2stats_api/src/tests/test_module.py b/space2stats_api/src/tests/test_module.py new file mode 100644 index 0000000..56e4d43 --- /dev/null +++ b/space2stats_api/src/tests/test_module.py @@ -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")