Skip to content

Commit

Permalink
Made pytest to be self-sufficient, e.g. test SQLite3 DB is created/in…
Browse files Browse the repository at this point in the history
…itialized/deleted automatically in temporary location.
  • Loading branch information
vmdocua committed Oct 15, 2023
1 parent a42fb3a commit ce4a9a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ jobs:
poetry build
shell: bash

- name: Setup test DB
run: |
> db/db_dev.sqlite3
poetry run setup_db
shell: bash

- name: Run pytest
run: |
poetry run pytest --cov=. --cov-report=xml
Expand Down
20 changes: 19 additions & 1 deletion repromon_app/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import logging
import threading
import tempfile

import pytest
from fastapi import FastAPI
Expand All @@ -10,6 +12,7 @@
from repromon_app.db import db_init
from repromon_app.service import SecSysService
from repromon_app.srv import create_fastapi_app
from repromon_tools import setup_db

logger = logging.getLogger(__name__)
#
Expand All @@ -30,15 +33,30 @@
@pytest.fixture(scope="session", autouse=True)
def init_config():
logger.debug("init_config()")
#
temp = tempfile.NamedTemporaryFile(prefix='repromon_db_test_sqlite3_', delete=False)
db_path = temp.name
logger.info(f"create temporary file: {db_path}")
db_url = f"sqlite:///{db_path}"
os.environ['DB_URL'] = db_url
logger.info(f"override DB_URL env with this value: {db_url}")
#
app_config_init()
yield
#
if os.path.isfile(db_path):
os.remove(db_path)
print(f"Deleted temporary DB file: {db_path}")


@pytest.fixture(scope="session", autouse=True)
def init_db():
logger.debug("init_db()")
logger.debug("Initialize DB...")
db_init(app_config().db.dict(), threading.get_ident)
# db_init(app_config().db.dict(), threading.get_ident)
logger.debug("Execute setup_db first")
setup_db.main()
logger.debug("Done, setup_db")
svc: SecSysService = SecSysService()

global _apikey_tester1, _apikey_tester2, _apikey_tester3
Expand Down

0 comments on commit ce4a9a0

Please sign in to comment.