add large binary field, tests and docs

This commit is contained in:
collerek
2021-04-28 17:04:29 +02:00
parent 638af9ad4c
commit 11ed5fd322
13 changed files with 148 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import datetime
import decimal
import uuid
from base64 import b64encode
from enum import Enum
import databases
@ -22,6 +23,10 @@ uuid1 = uuid.uuid4()
uuid2 = uuid.uuid4()
blob = b"test"
blob2 = b"test2icac89uc98"
class EnumTest(Enum):
val1 = "Val1"
val2 = "Val2"
@ -57,6 +62,7 @@ class Organisation(ormar.Model):
random_json: pydantic.Json = ormar.JSON(choices=["aa", '{"aa":"bb"}'])
random_uuid: uuid.UUID = ormar.UUID(choices=[uuid1, uuid2])
enum_string: str = ormar.String(max_length=100, choices=list(EnumTest))
blob_col: bytes = ormar.LargeBinary(max_length=100000, choices=[blob, blob2])
@app.on_event("startup")
@ -111,6 +117,7 @@ def test_all_endpoints():
"random_json": '{"aa":"bb"}',
"random_uuid": str(uuid1),
"enum_string": EnumTest.val1.value,
"blob_col": blob.decode("utf-8"),
},
)

View File

@ -1,6 +1,6 @@
import asyncio
import uuid
import datetime
import uuid
from typing import List
import databases
@ -9,7 +9,7 @@ import pytest
import sqlalchemy
import ormar
from ormar.exceptions import QueryDefinitionError, NoMatch, ModelError
from ormar.exceptions import ModelError, NoMatch, QueryDefinitionError
from tests.settings import DATABASE_URL
database = databases.Database(DATABASE_URL, force_rollback=True)
@ -26,6 +26,20 @@ class JsonSample(ormar.Model):
test_json = ormar.JSON(nullable=True)
blob = b"test"
blob2 = b"test2icac89uc98"
class LargeBinarySample(ormar.Model):
class Meta:
tablename = "my_bolbs"
metadata = metadata
database = database
id: int = ormar.Integer(primary_key=True)
test_binary = ormar.LargeBinary(max_length=100000, choices=[blob, blob2])
class UUIDSample(ormar.Model):
class Meta:
tablename = "uuids"
@ -102,15 +116,8 @@ class Country(ormar.Model):
)
@pytest.fixture(scope="module")
def event_loop():
loop = asyncio.get_event_loop()
yield loop
loop.close()
@pytest.fixture(autouse=True, scope="module")
async def create_test_database():
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
metadata.drop_all(engine)
metadata.create_all(engine)
@ -151,6 +158,19 @@ async def test_json_column():
assert items[1].test_json == dict(aa=12)
@pytest.mark.asyncio
async def test_binary_column():
async with database:
async with database.transaction(force_rollback=True):
await LargeBinarySample.objects.create(test_binary=blob)
await LargeBinarySample.objects.create(test_binary=blob2)
items = await LargeBinarySample.objects.all()
assert len(items) == 2
assert items[0].test_binary == blob
assert items[1].test_binary == blob2
@pytest.mark.asyncio
async def test_uuid_column():
async with database:

View File

@ -57,7 +57,7 @@ def create_test_database():
def assert_type(book: Book):
print(book)
_ = str(book)
@pytest.mark.asyncio