-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
long2ice
committed
May 24, 2020
1 parent
3a76486
commit 354e861
Showing
13 changed files
with
277 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,61 @@ | ||
import asyncio | ||
import os | ||
|
||
import pytest | ||
from tortoise.contrib.test import finalizer, initializer | ||
from tortoise import Tortoise, expand_db_url, generate_schema_for_client | ||
from tortoise.backends.asyncpg.schema_generator import AsyncpgSchemaGenerator | ||
from tortoise.backends.mysql.schema_generator import MySQLSchemaGenerator | ||
from tortoise.backends.sqlite.schema_generator import SqliteSchemaGenerator | ||
|
||
from aerich.ddl.mysql import MysqlDDL | ||
from aerich.ddl.postgres import PostgresDDL | ||
from aerich.ddl.sqlite import SqliteDDL | ||
from aerich.migrate import Migrate | ||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def initialize_tests(request): | ||
db_url = os.getenv("TEST_DB", "sqlite://:memory:") | ||
initializer(["tests.models"], db_url=db_url) | ||
request.addfinalizer(finalizer) | ||
db_url = os.getenv("TEST_DB", "sqlite://:memory:") | ||
tortoise_orm = { | ||
"connections": {"default": expand_db_url(db_url, True)}, | ||
"apps": { | ||
"models": { | ||
"models": ["tests.models", "aerich.models"], | ||
"default_connection": "default", | ||
}, | ||
}, | ||
} | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
def reset_migrate(): | ||
Migrate.upgrade_operators = [] | ||
Migrate.downgrade_operators = [] | ||
Migrate._upgrade_fk_m2m_index_operators = [] | ||
Migrate._downgrade_fk_m2m_index_operators = [] | ||
Migrate._upgrade_m2m = [] | ||
Migrate._downgrade_m2m = [] | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def loop(): | ||
loop = asyncio.get_event_loop() | ||
return loop | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def initialize_tests(loop, request): | ||
tortoise_orm['connections']['diff_models'] = "sqlite://:memory:" | ||
tortoise_orm['apps']['diff_models'] = {"models": ["tests.diff_models"], "default_connection": "diff_models"} | ||
|
||
loop.run_until_complete(Tortoise.init(config=tortoise_orm, _create_db=True)) | ||
loop.run_until_complete( | ||
generate_schema_for_client(Tortoise.get_connection("default"), safe=True) | ||
) | ||
|
||
client = Tortoise.get_connection("default") | ||
if client.schema_generator is MySQLSchemaGenerator: | ||
Migrate.ddl = MysqlDDL(client) | ||
elif client.schema_generator is SqliteSchemaGenerator: | ||
Migrate.ddl = SqliteDDL(client) | ||
elif client.schema_generator is AsyncpgSchemaGenerator: | ||
Migrate.ddl = PostgresDDL(client) | ||
|
||
request.addfinalizer(lambda: loop.run_until_complete(Tortoise._drop_databases())) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,12 @@ authors = ["long2ice <[email protected]>"] | |
|
||
[tool.poetry.dependencies] | ||
python = "^3.8" | ||
tortoise-orm = {git = "https://github.com/tortoise-orm/tortoise-orm.git", branch = "develop"} | ||
tortoise-orm = "*" | ||
asyncclick = "*" | ||
pydantic = "*" | ||
|
||
[tool.poetry.dev-dependencies] | ||
taskipy = "*" | ||
asynctest = "*" | ||
flake8 = "*" | ||
isort = "*" | ||
black = "^19.10b0" | ||
|
@@ -21,6 +20,7 @@ aiomysql = "*" | |
asyncpg = "*" | ||
pytest-xdist = "*" | ||
mypy = "*" | ||
pytest-asyncio = "*" | ||
|
||
[tool.taskipy.tasks] | ||
export = "poetry export -f requirements.txt --without-hashes > requirements.txt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +0,0 @@ | ||
TORTOISE_ORM = { | ||
"connections": {"default": "mysql://root:[email protected]:3306/test"}, | ||
"apps": { | ||
"models": {"models": ["tests.models", "aerich.models"], "default_connection": "default"} | ||
}, | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
from enum import IntEnum | ||
|
||
from tortoise import Model, fields | ||
|
||
|
||
class ProductType(IntEnum): | ||
article = 1 | ||
page = 2 | ||
|
||
|
||
class PermissionAction(IntEnum): | ||
create = 1 | ||
delete = 2 | ||
update = 3 | ||
read = 4 | ||
|
||
|
||
class Status(IntEnum): | ||
on = 1 | ||
off = 0 | ||
|
||
|
||
class User(Model): | ||
username = fields.CharField(max_length=20,) | ||
password = fields.CharField(max_length=200) | ||
last_login = fields.DatetimeField(description="Last Login", default=datetime.datetime.now) | ||
is_active = fields.BooleanField(default=True, description="Is Active") | ||
is_superuser = fields.BooleanField(default=False, description="Is SuperUser") | ||
avatar = fields.CharField(max_length=200, default="") | ||
intro = fields.TextField(default="") | ||
|
||
|
||
class Category(Model): | ||
slug = fields.CharField(max_length=200) | ||
user = fields.ForeignKeyField("diff_models.User", description="User") | ||
created_at = fields.DatetimeField(auto_now_add=True) | ||
|
||
|
||
class Product(Model): | ||
categories = fields.ManyToManyField("diff_models.Category") | ||
name = fields.CharField(max_length=50) | ||
view_num = fields.IntField(description="View Num") | ||
sort = fields.IntField() | ||
is_reviewed = fields.BooleanField(description="Is Reviewed") | ||
type = fields.IntEnumField(ProductType, description="Product Type") | ||
image = fields.CharField(max_length=200) | ||
body = fields.TextField() | ||
created_at = fields.DatetimeField(auto_now_add=True) | ||
|
||
|
||
class Config(Model): | ||
label = fields.CharField(max_length=200) | ||
key = fields.CharField(max_length=20) | ||
value = fields.JSONField() | ||
status: Status = fields.IntEnumField(Status, default=Status.on) |
Oops, something went wrong.