Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
treiher committed Sep 22, 2023
1 parent 4065fab commit bba9d32
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 11 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ jobs:
test_backend:
name: Backend Tests
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"]
os: ["ubuntu-20.04"]
include:
- python-version: "3.9"
os: "windows-2022"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
3 changes: 2 additions & 1 deletion tests/backend/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

@pytest.fixture(name="client")
def fixture_client(tmp_path: Path) -> Generator[Client, None, None]:
app.config["DATABASE"] = f"sqlite:///{tmp_path}/valens.db"
test_db = tmp_path / "test.db"
app.config["DATABASE"] = f"sqlite:///{test_db}"
app.config["SECRET_KEY"] = b"TEST_KEY"
app.config["TESTING"] = True

Expand Down
3 changes: 2 additions & 1 deletion tests/backend/assets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

@pytest.fixture(name="client")
def fixture_client(tmp_path: Path) -> Generator[Client, None, None]:
app.config["DATABASE"] = f"sqlite:///{tmp_path}/valens.db"
test_db = tmp_path / "test.db"
app.config["DATABASE"] = f"sqlite:///{test_db}"
app.config["SECRET_KEY"] = b"TEST_KEY"
app.config["TESTING"] = True

Expand Down
2 changes: 1 addition & 1 deletion tests/backend/demo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

def test_run(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(app, "run", lambda x, y: None)
demo.run(f"sqlite:///{tmp_path}/db")
demo.run(rf"sqlite:///{tmp_path}/db")
6 changes: 4 additions & 2 deletions tests/backend/migrations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def constraints(connection: sqlite3.Connection) -> list[str]:
)

with app.app_context():
migrations_db = f"{tmp_path}/migrations.db"
migrations_db = tmp_path / "migrations.db"
migrations_db.touch()
app.config["DATABASE"] = f"sqlite:///{migrations_db}"

connection = sqlite3.connect(migrations_db)
Expand All @@ -99,7 +100,8 @@ def constraints(connection: sqlite3.Connection) -> list[str]:

migrated_constraints = constraints(connection)

models_db = f"{tmp_path}/models.db"
models_db = tmp_path / "models.db"
models_db.touch()
app.config["DATABASE"] = f"sqlite:///{models_db}"

db.init()
Expand Down
11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from pathlib import Path

from tempfile import NamedTemporaryFile
from tempfile import TemporaryDirectory

import _pytest
import pytest
Expand All @@ -17,11 +18,15 @@ def alembic_config() -> dict[str, str]:

@pytest.fixture()
def alembic_engine() -> object:
with NamedTemporaryFile() as tmp_file:
app.config["DATABASE"] = f"sqlite:///{tmp_file.name}"
with TemporaryDirectory() as tmp_dir:
tmp_file = Path(tmp_dir) / "test.db"
tmp_file.touch()
app.config["DATABASE"] = f"sqlite:///{tmp_file}"
print("XXX", app.config["DATABASE"], repr(app.config["DATABASE"]))
with app.app_context():
db.init()
yield db.get_engine()
tmp_file.unlink()


def pytest_addoption(parser: _pytest.config.argparsing.Parser) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/web_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _fixture_backend() -> Generator[None, None, None]:
config = create_config_file(data_dir, db_file)

with app.app_context():
app.config["DATABASE"] = f"sqlite:///{db_file}"
app.config["DATABASE"] = rf"sqlite:///{db_file}"
app.config["SECRET_KEY"] = b"TEST_KEY"
tests.utils.init_db_data()

Expand Down
2 changes: 1 addition & 1 deletion valens/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def check_config_file(environ: dict[str, str]) -> None:
def create_config_file(config_directory: Path, database_file: Path) -> Path:
config = config_directory / "config.py"
config.write_text(
f"DATABASE = 'sqlite:///{database_file}'\nSECRET_KEY = {os.urandom(24)!r}\n",
f"DATABASE = r'sqlite:///{database_file}'\nSECRET_KEY = {os.urandom(24)!r}\n",
encoding="utf-8",
)
return config
3 changes: 3 additions & 0 deletions valens/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def upgrade_lock_file() -> Path:


def get_engine() -> Engine:
print("get_engine()", current_app.config["DATABASE"], repr(current_app.config["DATABASE"]))
config.check_app_config()
db_dir().mkdir(exist_ok=True)
return create_engine(current_app.config["DATABASE"])
Expand Down Expand Up @@ -64,6 +65,8 @@ def remove_session() -> None:
def init() -> None:
print("Creating database")

print("database.init()", current_app.config["DATABASE"], db_file().exists())
print(db_file().read_text())
models.Base.query = get_scoped_session().query_property()
models.Base.metadata.create_all(bind=get_engine())

Expand Down

0 comments on commit bba9d32

Please sign in to comment.