-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_fsa_lite.py
82 lines (58 loc) · 2.34 KB
/
test_fsa_lite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import annotations
import os
from pathlib import Path
import pytest
from flask import Flask
from flask_sqlalchemy_lite import SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase
from flask_alembic import Alembic
@pytest.fixture
def app(app: Flask) -> Flask:
app.config["SQLALCHEMY_ENGINES"] = {
"default": "sqlite://",
"other": "sqlite://",
}
return app
@pytest.fixture
def db(app: Flask) -> SQLAlchemy:
return SQLAlchemy(app)
class Model(DeclarativeBase):
pass
class Other(DeclarativeBase):
pass
@pytest.mark.usefixtures("app_ctx")
def test_metadata_required(app: Flask) -> None:
"""Passing a metadata is required with Flask-SQLAlchemy-Lite."""
alembic = Alembic(app)
with pytest.raises(RuntimeError, match="pass 'metadatas' when"):
assert alembic.migration_context
@pytest.mark.usefixtures("app_ctx", "db")
def test_uses_engines(app: Flask) -> None:
"""Engines from Flask-SQLAlchemy-Lite are used if none are passed."""
alembic = Alembic(app, metadatas=Model.metadata)
assert alembic.migration_context
@pytest.mark.usefixtures("app_ctx")
def test_engine_required(app: Flask) -> None:
"""Flask-SQLAlchemy-Lite must configure an engine."""
del app.config["SQLALCHEMY_ENGINES"]
SQLAlchemy(app, require_default_engine=False)
alembic = Alembic(app, metadatas=Model.metadata)
with pytest.raises(RuntimeError, match="engines configured"):
assert alembic.migration_context
@pytest.mark.usefixtures("app_ctx", "db")
def test_missing_engine(app: Flask) -> None:
"""There must be an engine matching each metadata name."""
alembic = Alembic(
app, metadatas={"default": Model.metadata, "missing": Other.metadata}
)
with pytest.raises(RuntimeError, match="Missing engine config"):
assert alembic.migration_context
@pytest.mark.usefixtures("app_ctx", "db")
def test_override_engines(tmp_path: Path, app: Flask) -> None:
"""A passed engine overrides one from Flask-SQLAlchemy-Lite."""
db_path = os.fspath(tmp_path / "default.sqlite")
engine = create_engine(f"sqlite:///{db_path}")
alembic = Alembic(app, metadatas=Model.metadata, engines=engine)
assert alembic.migration_context.connection is not None
assert alembic.migration_context.connection.engine.url.database == db_path