feat: support --fake for aerich upgrade (#398)
* feat: support `--fake` for aerich upgrade * Add `--fake` to downgrade * tests: check --fake result for aerich upgrade and downgrade * Update readme * Fix unittest failed because of `db_field_types` changed * refactor: improve type hints and document
This commit is contained in:
72
tests/assets/fake/_tests.py
Normal file
72
tests/assets/fake/_tests.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import pytest
|
||||
from models import NewModel
|
||||
from models_second import Config
|
||||
from settings import TORTOISE_ORM
|
||||
from tortoise import Tortoise
|
||||
from tortoise.exceptions import OperationalError
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def anyio_backend() -> str:
|
||||
return "asyncio"
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def init_connections():
|
||||
await Tortoise.init(TORTOISE_ORM)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
await Tortoise.close_connections()
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_init_db():
|
||||
m1 = await NewModel.filter(name="")
|
||||
assert isinstance(m1, list)
|
||||
m2 = await Config.filter(key="")
|
||||
assert isinstance(m2, list)
|
||||
await NewModel.create(name="")
|
||||
await Config.create(key="", label="", value={})
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_fake_field_1():
|
||||
assert "field_1" in NewModel._meta.fields_map
|
||||
assert "field_1" in Config._meta.fields_map
|
||||
with pytest.raises(OperationalError):
|
||||
await NewModel.create(name="", field_1=1)
|
||||
with pytest.raises(OperationalError):
|
||||
await Config.create(key="", label="", value={}, field_1=1)
|
||||
|
||||
obj1 = NewModel(name="", field_1=1)
|
||||
with pytest.raises(OperationalError):
|
||||
await obj1.save()
|
||||
obj1 = NewModel(name="")
|
||||
with pytest.raises(OperationalError):
|
||||
await obj1.save()
|
||||
with pytest.raises(OperationalError):
|
||||
obj1 = await NewModel.first()
|
||||
obj1 = await NewModel.all().first().values("id", "name")
|
||||
assert obj1 and obj1["id"]
|
||||
|
||||
obj2 = Config(key="", label="", value={}, field_1=1)
|
||||
with pytest.raises(OperationalError):
|
||||
await obj2.save()
|
||||
obj2 = Config(key="", label="", value={})
|
||||
with pytest.raises(OperationalError):
|
||||
await obj2.save()
|
||||
with pytest.raises(OperationalError):
|
||||
obj2 = await Config.first()
|
||||
obj2 = await Config.all().first().values("id", "key")
|
||||
assert obj2 and obj2["id"]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_fake_field_2():
|
||||
assert "field_2" in NewModel._meta.fields_map
|
||||
assert "field_2" in Config._meta.fields_map
|
||||
with pytest.raises(OperationalError):
|
||||
await NewModel.create(name="")
|
||||
with pytest.raises(OperationalError):
|
||||
await Config.create(key="", label="", value={})
|
||||
28
tests/assets/fake/db.py
Normal file
28
tests/assets/fake/db.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import asyncclick as click
|
||||
from settings import TORTOISE_ORM
|
||||
|
||||
from tests._utils import drop_db, init_db
|
||||
|
||||
|
||||
@click.group()
|
||||
def cli(): ...
|
||||
|
||||
|
||||
@cli.command()
|
||||
async def create():
|
||||
await init_db(TORTOISE_ORM, False)
|
||||
click.echo(f"Success to create databases for {TORTOISE_ORM['connections']}")
|
||||
|
||||
|
||||
@cli.command()
|
||||
async def drop():
|
||||
await drop_db(TORTOISE_ORM)
|
||||
click.echo(f"Dropped databases for {TORTOISE_ORM['connections']}")
|
||||
|
||||
|
||||
def main():
|
||||
cli()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
22
tests/assets/fake/settings.py
Normal file
22
tests/assets/fake/settings.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import os
|
||||
from datetime import date
|
||||
|
||||
from tortoise.contrib.test import MEMORY_SQLITE
|
||||
|
||||
DB_URL = (
|
||||
_u.replace("\\{\\}", f"aerich_fake_{date.today():%Y%m%d}")
|
||||
if (_u := os.getenv("TEST_DB"))
|
||||
else MEMORY_SQLITE
|
||||
)
|
||||
DB_URL_SECOND = (DB_URL + "_second") if DB_URL != MEMORY_SQLITE else MEMORY_SQLITE
|
||||
|
||||
TORTOISE_ORM = {
|
||||
"connections": {
|
||||
"default": DB_URL.replace(MEMORY_SQLITE, "sqlite://db.sqlite3"),
|
||||
"second": DB_URL_SECOND.replace(MEMORY_SQLITE, "sqlite://db_second.sqlite3"),
|
||||
},
|
||||
"apps": {
|
||||
"models": {"models": ["models", "aerich.models"], "default_connection": "default"},
|
||||
"models_second": {"models": ["models_second"], "default_connection": "second"},
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user