Test alembic automigration with add and drop table and column. Args: tmp_working_dir: directory where database and migrations are stored monkeypatch: pytest fixture to overwrite attributes model_registry: clean reflex ModelRegistry
(
tmp_working_dir: Path,
monkeypatch: pytest.MonkeyPatch,
model_registry: type[ModelRegistry],
)
| 144 | "ignore:This declarative base already contains a class with the same class name", |
| 145 | ) |
| 146 | def test_automigration( |
| 147 | tmp_working_dir: Path, |
| 148 | monkeypatch: pytest.MonkeyPatch, |
| 149 | model_registry: type[ModelRegistry], |
| 150 | ): |
| 151 | """Test alembic automigration with add and drop table and column. |
| 152 | |
| 153 | Args: |
| 154 | tmp_working_dir: directory where database and migrations are stored |
| 155 | monkeypatch: pytest fixture to overwrite attributes |
| 156 | model_registry: clean reflex ModelRegistry |
| 157 | """ |
| 158 | from sqlalchemy import select |
| 159 | from sqlalchemy.exc import OperationalError |
| 160 | from sqlalchemy.orm import ( |
| 161 | DeclarativeBase, |
| 162 | Mapped, |
| 163 | MappedAsDataclass, |
| 164 | declared_attr, |
| 165 | mapped_column, |
| 166 | ) |
| 167 | |
| 168 | alembic_ini = tmp_working_dir / "alembic.ini" |
| 169 | versions = tmp_working_dir / "alembic" / "versions" |
| 170 | monkeypatch.setattr(reflex.constants, "ALEMBIC_CONFIG", str(alembic_ini)) |
| 171 | |
| 172 | config_mock = mock.Mock() |
| 173 | config_mock.db_url = f"sqlite:///{tmp_working_dir}/reflex.db" |
| 174 | monkeypatch.setattr(reflex.model, "get_config", mock.Mock(return_value=config_mock)) |
| 175 | |
| 176 | assert alembic_ini.exists() is False |
| 177 | assert versions.exists() is False |
| 178 | alembic_init() |
| 179 | assert alembic_ini.exists() |
| 180 | assert versions.exists() |
| 181 | |
| 182 | class Base(DeclarativeBase): |
| 183 | @declared_attr.directive |
| 184 | def __tablename__(cls) -> str: |
| 185 | return cls.__name__.lower() |
| 186 | |
| 187 | assert model_registry.register(Base) |
| 188 | |
| 189 | class ModelBase(Base, MappedAsDataclass): |
| 190 | __abstract__ = True |
| 191 | id: Mapped[int | None] = mapped_column(primary_key=True, default=None) |
| 192 | |
| 193 | # initial table |
| 194 | class AlembicThing(ModelBase): # pyright: ignore[reportRedeclaration] |
| 195 | t1: Mapped[str] = mapped_column(default="") |
| 196 | |
| 197 | with get_engine().connect() as connection: |
| 198 | assert alembic_autogenerate(connection=connection, message="Initial Revision") |
| 199 | assert migrate() |
| 200 | version_scripts = list(versions.glob("*.py")) |
| 201 | assert len(version_scripts) == 1 |
| 202 | assert version_scripts[0].name.endswith("initial_revision.py") |
| 203 |
nothing calls this directly
no test coverage detected