Draft 0.11.0 (#594)
* fix for #584 * fix for #580 * fix typing * connect to db in test * refactor test * remove async mark * connect client * fix mypy * fix mypy * update deps * check py3.10? * remove py3.6, bump version
This commit is contained in:
@ -176,14 +176,15 @@ async def test_queryset_method():
|
||||
year=1930, title="Book 3"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_count_method():
|
||||
async with database:
|
||||
await sample_data()
|
||||
|
||||
count = await Author.objects.select_related("books").count()
|
||||
count = await Author.objects.select_related("books").count()
|
||||
assert count == 1
|
||||
|
||||
# The legacy functionality
|
||||
count = await Author.objects.select_related("books").count(distinct=False)
|
||||
count = await Author.objects.select_related("books").count(distinct=False)
|
||||
assert count == 3
|
||||
|
||||
@ -4,6 +4,7 @@ import databases
|
||||
import pydantic
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from pydantic import Json
|
||||
|
||||
import ormar
|
||||
from ormar import QuerySet
|
||||
@ -68,7 +69,7 @@ class Note(ormar.Model):
|
||||
|
||||
|
||||
class ItemConfig(ormar.Model):
|
||||
class Meta:
|
||||
class Meta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
tablename = "item_config"
|
||||
@ -98,6 +99,16 @@ class Customer(ormar.Model):
|
||||
name: str = ormar.String(max_length=32)
|
||||
|
||||
|
||||
class JsonTestModel(ormar.Model):
|
||||
class Meta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
tablename = "test_model"
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
json_field: Json = ormar.JSON()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
@ -268,6 +279,36 @@ async def test_bulk_create():
|
||||
assert len(completed) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_create_json_field():
|
||||
async with database:
|
||||
json_value = {"a": 1}
|
||||
test_model_1 = JsonTestModel(id=1, json_field=json_value)
|
||||
test_model_2 = JsonTestModel(id=2, json_field=json_value)
|
||||
|
||||
# store one with .save() and the other with .bulk_create()
|
||||
await test_model_1.save()
|
||||
await JsonTestModel.objects.bulk_create([test_model_2])
|
||||
|
||||
# refresh from the database
|
||||
await test_model_1.load()
|
||||
await test_model_2.load()
|
||||
|
||||
assert test_model_1.json_field == test_model_2.json_field # True
|
||||
|
||||
# try to query the json field
|
||||
table = JsonTestModel.Meta.table
|
||||
query = table.select().where(table.c.json_field["a"].as_integer() == 1)
|
||||
res = [
|
||||
JsonTestModel.from_row(record, source_model=JsonTestModel)
|
||||
for record in await database.fetch_all(query)
|
||||
]
|
||||
|
||||
assert test_model_1 in res
|
||||
assert test_model_2 in res
|
||||
assert len(res) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_create_with_relation():
|
||||
async with database:
|
||||
@ -408,6 +449,21 @@ async def test_bulk_operations_with_json():
|
||||
items = await ItemConfig.objects.all()
|
||||
assert all(x.pairs == ["1"] for x in items)
|
||||
|
||||
items = await ItemConfig.objects.filter(ItemConfig.id > 1).all()
|
||||
for item in items:
|
||||
item.pairs = {"b": 2}
|
||||
await ItemConfig.objects.bulk_update(items)
|
||||
items = await ItemConfig.objects.filter(ItemConfig.id > 1).all()
|
||||
assert all(x.pairs == {"b": 2} for x in items)
|
||||
|
||||
table = ItemConfig.Meta.table
|
||||
query = table.select().where(table.c.pairs["b"].as_integer() == 2)
|
||||
res = [
|
||||
ItemConfig.from_row(record, source_model=ItemConfig)
|
||||
for record in await database.fetch_all(query)
|
||||
]
|
||||
assert len(res) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_custom_queryset_cls():
|
||||
|
||||
Reference in New Issue
Block a user