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
|
import pytest
from pydantic.main import BaseModel
from beanie import init_beanie
from beanie.executors.migrate import MigrationSettings, run_migrate
from beanie.migrations.models import RunningDirections
from beanie.odm.documents import Document
from beanie.odm.models import InspectionStatuses
class Tag(BaseModel):
color: str
name: str
class OldNote(Document):
name: str
tag: Tag
class Settings:
name = "notes"
class Note(Document):
title: str
tag: Tag
class Settings:
name = "notes"
@pytest.fixture()
async def notes(db):
await init_beanie(database=db, document_models=[OldNote])
await OldNote.delete_all()
for i in range(10):
note = OldNote(name=str(i), tag=Tag(name="test", color="red"))
await note.insert()
yield
# await OldNote.delete_all()
async def test_migration_rename_field(settings, notes, db):
migration_settings = MigrationSettings(
connection_uri=settings.mongodb_dsn,
database_name=settings.mongodb_db_name,
path="tests/migrations/migrations_for_test/rename_field",
)
await run_migrate(migration_settings)
await init_beanie(database=db, document_models=[Note])
inspection = await Note.inspect_collection()
assert inspection.status == InspectionStatuses.OK
note = await Note.find_one({})
assert note.title == "0"
migration_settings.direction = RunningDirections.BACKWARD
await run_migrate(migration_settings)
inspection = await OldNote.inspect_collection()
assert inspection.status == InspectionStatuses.OK
note = await OldNote.find_one({})
assert note.name == "0"
|