Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit cd2977a

Browse files
committed
test: insert classification
1 parent 014b13a commit cd2977a

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

tests/query_test/data_test.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import asyncio
2+
from datetime import date
3+
import os
4+
from uuid import uuid4
5+
6+
import pytest
7+
import pytest_asyncio
8+
from sqlalchemy import Engine, create_engine, text
9+
from apiserver.data.api.classifications import insert_classification
10+
11+
12+
from apiserver.env import Config, load_config
13+
from schema.model.model import (
14+
CLASS_END_DATE,
15+
CLASS_HIDDEN_DATE,
16+
CLASS_START_DATE,
17+
CLASS_TYPE,
18+
CLASSIFICATION_TABLE,
19+
)
20+
from test_util import Fixture
21+
from store.conn import get_conn
22+
from store.store import Store
23+
from test_resources import res_path
24+
25+
if not os.environ.get("QUERY_TEST"):
26+
pytest.skip(
27+
"Skipping store_test as QUERY_TEST is not set.", allow_module_level=True
28+
)
29+
30+
31+
@pytest.fixture(scope="session", autouse=True)
32+
def event_loop():
33+
"""Necessary for async tests with module-scoped fixtures"""
34+
loop = asyncio.get_event_loop()
35+
yield loop
36+
loop.close()
37+
38+
39+
@pytest.fixture(scope="module")
40+
def api_config() -> Fixture[Config]:
41+
test_config_path = res_path.joinpath("querytestenv.toml")
42+
yield load_config(test_config_path)
43+
44+
45+
@pytest.fixture(scope="module")
46+
def admin_engine(api_config) -> Fixture[Engine]:
47+
db_cluster = f"{api_config.DB_USER}:{api_config.DB_PASS}@{api_config.DB_HOST}:{api_config.DB_PORT}"
48+
admin_db_url = f"{db_cluster}/{api_config.DB_NAME_ADMIN}"
49+
50+
admin_engine = create_engine(
51+
f"postgresql+psycopg://{admin_db_url}", isolation_level="AUTOCOMMIT"
52+
)
53+
54+
yield admin_engine
55+
56+
57+
@pytest_asyncio.fixture
58+
async def new_db_store(api_config: Config, admin_engine: Engine):
59+
db_name = f"db_{uuid4()}"
60+
61+
with admin_engine.connect() as conn:
62+
create_db = text(f"CREATE DATABASE {db_name}")
63+
conn.execute(create_db)
64+
65+
modified_config = api_config.model_copy(update={"DB_NAME": db_name})
66+
67+
store = Store()
68+
store.init_objects(modified_config)
69+
# we don't run startup due to its overhead
70+
yield store
71+
# Ensure connections are GC'd
72+
del store
73+
74+
with admin_engine.connect() as conn:
75+
drop_db = text(f"DROP DATABASE {db_name}")
76+
conn.execute(drop_db)
77+
78+
79+
async def test_create_class(new_db_store: Store):
80+
async with get_conn(new_db_store) as conn:
81+
await insert_classification(conn, "points", date(2022, 1, 1))
82+
83+
query = text(f"""
84+
SELECT * FROM {CLASSIFICATION_TABLE};
85+
""")
86+
87+
res = await conn.execute(query)
88+
89+
res_item = res.mappings().first()
90+
91+
assert res_item is not None
92+
assert res_item[CLASS_TYPE] == "points"
93+
assert res_item[CLASS_START_DATE] == date(2022, 1, 1)
94+
assert res_item[CLASS_END_DATE] == date(2022, 5, 31)
95+
assert res_item[CLASS_HIDDEN_DATE] == date(2022, 5, 1)

0 commit comments

Comments
 (0)