MCPcopy Index your code
hub / github.com/reflex-dev/reflex / test_automigration

Function test_automigration

tests/units/test_sqlalchemy.py:146–296  ·  view source on GitHub ↗

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],
)

Source from the content-addressed store, hash-verified

144 "ignore:This declarative base already contains a class with the same class name",
145)
146def 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

Callers

nothing calls this directly

Calls 12

alembic_initFunction · 0.90
get_engineFunction · 0.90
alembic_autogenerateFunction · 0.90
migrateFunction · 0.90
sqla_sessionFunction · 0.90
existsMethod · 0.80
registerMethod · 0.80
endswithMethod · 0.80
clearMethod · 0.80
get_metadataMethod · 0.80
AlembicThingClass · 0.70
AlembicSecondClass · 0.70

Tested by

no test coverage detected