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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
import pytest
from pydantic.main import BaseModel
from beanie import init_beanie
from beanie.executors.migrate import MigrationSettings, run_migrate
from beanie.odm.documents import Document
class Tag(BaseModel):
color: str
name: str
class OldNote(Document):
title: str
tag: Tag
class Settings:
name = "notes"
indexes = ["title"]
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(title=str(i), tag=Tag(name="test", color="red"))
await note.insert()
yield
await OldNote.delete_all()
async def test_remove_index_allowed(settings, notes, db):
migration_settings = MigrationSettings(
connection_uri=settings.mongodb_dsn,
database_name=settings.mongodb_db_name,
path="tests/migrations/migrations_for_test/remove_index",
allow_index_dropping=True,
)
await run_migrate(migration_settings)
await init_beanie(
database=db, document_models=[Note], allow_index_dropping=False
)
collection = Note.get_pymongo_collection()
index_info = await collection.index_information()
assert index_info == {
"_id_": {"key": [("_id", 1)], "v": 2},
}
async def test_remove_index_default(settings, notes, db):
migration_settings = MigrationSettings(
connection_uri=settings.mongodb_dsn,
database_name=settings.mongodb_db_name,
path="tests/migrations/migrations_for_test/remove_index",
)
await run_migrate(migration_settings)
await init_beanie(
database=db, document_models=[Note], allow_index_dropping=False
)
collection = Note.get_pymongo_collection()
index_info = await collection.index_information()
assert index_info == {
"_id_": {"key": [("_id", 1)], "v": 2},
"title_1": {"key": [("title", 1)], "v": 2},
}
async def test_remove_index_not_allowed(settings, notes, db):
migration_settings = MigrationSettings(
connection_uri=settings.mongodb_dsn,
database_name=settings.mongodb_db_name,
path="tests/migrations/migrations_for_test/remove_index",
allow_index_dropping=False,
)
await run_migrate(migration_settings)
await init_beanie(
database=db, document_models=[Note], allow_index_dropping=False
)
collection = Note.get_pymongo_collection()
index_info = await collection.index_information()
assert index_info == {
"_id_": {"key": [("_id", 1)], "v": 2},
"title_1": {"key": [("title", 1)], "v": 2},
}
|