split tests into packages
This commit is contained in:
0
tests/test_model_definition/pks_and_fks/__init__.py
Normal file
0
tests/test_model_definition/pks_and_fks/__init__.py
Normal file
@ -0,0 +1,47 @@
|
||||
import random
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
def key():
|
||||
return "".join(random.choice("abcdefgh123456") for _ in range(8))
|
||||
|
||||
|
||||
class Model(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "models"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id: str = ormar.String(primary_key=True, default=key, max_length=8)
|
||||
name: str = ormar.String(max_length=32)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="function")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pk_1():
|
||||
async with database:
|
||||
model = await Model.objects.create(name="NAME")
|
||||
assert isinstance(model.id, str)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pk_2():
|
||||
async with database:
|
||||
model = await Model.objects.create(name="NAME")
|
||||
assert await Model.objects.all() == [model]
|
||||
@ -0,0 +1,81 @@
|
||||
from random import choice
|
||||
from string import ascii_uppercase
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
import ormar
|
||||
from ormar import Float, String
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
def get_id() -> str:
|
||||
return "".join(choice(ascii_uppercase) for _ in range(12))
|
||||
|
||||
|
||||
class MainMeta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class PositionOrm(ormar.Model):
|
||||
class Meta(MainMeta):
|
||||
pass
|
||||
|
||||
name: str = String(primary_key=True, max_length=50)
|
||||
x: float = Float()
|
||||
y: float = Float()
|
||||
degrees: float = Float()
|
||||
|
||||
|
||||
class PositionOrmDef(ormar.Model):
|
||||
class Meta(MainMeta):
|
||||
pass
|
||||
|
||||
name: str = String(primary_key=True, max_length=50, default=get_id)
|
||||
x: float = Float()
|
||||
y: float = Float()
|
||||
degrees: float = Float()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = create_engine(DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def cleanup():
|
||||
yield
|
||||
async with database:
|
||||
await PositionOrm.objects.delete(each=True)
|
||||
await PositionOrmDef.objects.delete(each=True)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_creating_a_position(cleanup):
|
||||
async with database:
|
||||
instance = PositionOrm(name="my_pos", x=1.0, y=2.0, degrees=3.0,)
|
||||
await instance.save()
|
||||
assert instance.saved
|
||||
assert instance.name == "my_pos"
|
||||
|
||||
instance2 = PositionOrmDef(x=1.0, y=2.0, degrees=3.0,)
|
||||
await instance2.save()
|
||||
assert instance2.saved
|
||||
assert instance2.name is not None
|
||||
assert len(instance2.name) == 12
|
||||
|
||||
instance3 = PositionOrmDef(x=1.0, y=2.0, degrees=3.0,)
|
||||
await instance3.save()
|
||||
assert instance3.saved
|
||||
assert instance3.name is not None
|
||||
assert len(instance3.name) == 12
|
||||
assert instance2.name != instance3.name
|
||||
63
tests/test_model_definition/pks_and_fks/test_uuid_fks.py
Normal file
63
tests/test_model_definition/pks_and_fks/test_uuid_fks.py
Normal file
@ -0,0 +1,63 @@
|
||||
import uuid
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
import ormar
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
metadata = sqlalchemy.MetaData()
|
||||
db = databases.Database(DATABASE_URL)
|
||||
|
||||
|
||||
class User(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "user"
|
||||
metadata = metadata
|
||||
database = db
|
||||
|
||||
id: uuid.UUID = ormar.UUID(
|
||||
primary_key=True, default=uuid.uuid4, uuid_format="string"
|
||||
)
|
||||
username = ormar.String(index=True, unique=True, null=False, max_length=255)
|
||||
email = ormar.String(index=True, unique=True, nullable=False, max_length=255)
|
||||
hashed_password = ormar.String(null=False, max_length=255)
|
||||
is_active = ormar.Boolean(default=True, nullable=False)
|
||||
is_superuser = ormar.Boolean(default=False, nullable=False)
|
||||
|
||||
|
||||
class Token(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "token"
|
||||
metadata = metadata
|
||||
database = db
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
text = ormar.String(max_length=4, unique=True)
|
||||
user = ormar.ForeignKey(User, related_name="tokens")
|
||||
created_at = ormar.DateTime(server_default=sqlalchemy.func.now())
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = create_engine(DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_uuid_fk():
|
||||
async with db:
|
||||
async with db.transaction(force_rollback=True):
|
||||
user = await User.objects.create(
|
||||
username="User1",
|
||||
email="email@example.com",
|
||||
hashed_password="^$EDACVS(&A&Y@2131aa",
|
||||
is_active=True,
|
||||
is_superuser=False,
|
||||
)
|
||||
await Token.objects.create(text="AAAA", user=user)
|
||||
await Token.objects.order_by("-created_at").all()
|
||||
Reference in New Issue
Block a user