-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import pytest | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
from app.db.base_model import Base | ||
from app.settings import get_settings | ||
|
||
settings = get_settings() | ||
|
||
tables = [ | ||
"Code", | ||
"User", | ||
"Role", | ||
"Permission", | ||
"Feedback", | ||
"Device", | ||
"Session", | ||
"ColumnStats", | ||
"FileFormat", | ||
"FileInfo", | ||
"Task", | ||
] | ||
|
||
test_engine = create_engine(settings.postgres_test_dsn.unicode_string()) | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def session_test(): | ||
Session = sessionmaker(test_engine, expire_on_commit=False) | ||
yield Session | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def create_tables(): | ||
Base.metadata.create_all(test_engine) | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def drop_tables_at_end(session_test): | ||
yield | ||
Base.metadata.drop_all(test_engine) | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
def clean_tables(request, session_test): | ||
if "fixture_name" in request.fixturenames: | ||
yield | ||
else: | ||
with session_test() as session: | ||
with session.connection(): | ||
for table_name in tables: | ||
table = Base.metadata.tables[table_name] | ||
session.query(table).delete() | ||
session.commit() | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pytest | ||
from app.db import tables | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def fetch_all_data_from_table(session_test): | ||
def fetch_all_data(model_name: str): | ||
with session_test() as session: | ||
with session.connection(): | ||
data = session.query(tables[model_name]).all() | ||
return data | ||
|
||
return fetch_all_data | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def fetch_data_by_id_from_table(session_test): | ||
def fetch_data_by_id(model_name: str, id: str | int): | ||
with session_test() as session: | ||
with session.connection(): | ||
data = session.get(tables[model_name], id) | ||
return data | ||
|
||
return fetch_data_by_id | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def fetch_first_from_table(session_test): | ||
def fetch_first(model_name: str): | ||
with session_test() as session: | ||
with session.connection(): | ||
data = session.query(tables[model_name]).first() | ||
return data | ||
|
||
return fetch_first |
Oops, something went wrong.