Skip to content

Commit

Permalink
Extracted compatibility module to work without AioModel subclassing +…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
rudyryk committed May 5, 2024
1 parent 430a3cf commit 06386fa
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 455 deletions.
462 changes: 27 additions & 435 deletions peewee_async.py

Large diffs are not rendered by default.

442 changes: 442 additions & 0 deletions peewee_async_compat.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ authors = ["Alexey Kinev <[email protected]>", "Gorshkov Nikolay(contributor) <noga
readme = "README.md"
packages = [
{ include = "peewee_async.py" },
{ include = "peewee_async_compat.py" },
{ include = "peewee_asyncext.py" },
]

Expand Down
28 changes: 18 additions & 10 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import uuid

import peewee
import peewee_async

from peewee_async import AioModel


class TestModel(AioModel):
class TestModel(peewee_async.AioModel):
__test__ = False # disable pytest warnings
text = peewee.CharField(max_length=100, unique=True)
data = peewee.TextField(default='')
Expand All @@ -14,15 +13,15 @@ def __str__(self):
return '<%s id=%s> %s' % (self.__class__.__name__, self.id, self.text)


class TestModelAlpha(AioModel):
class TestModelAlpha(peewee_async.AioModel):
__test__ = False
text = peewee.CharField()

def __str__(self):
return '<%s id=%s> %s' % (self.__class__.__name__, self.id, self.text)


class TestModelBeta(AioModel):
class TestModelBeta(peewee_async.AioModel):
__test__ = False
alpha = peewee.ForeignKeyField(TestModelAlpha, backref='betas')
text = peewee.CharField()
Expand All @@ -31,7 +30,7 @@ def __str__(self):
return '<%s id=%s> %s' % (self.__class__.__name__, self.id, self.text)


class TestModelGamma(AioModel):
class TestModelGamma(peewee_async.AioModel):
__test__ = False
text = peewee.CharField()
beta = peewee.ForeignKeyField(TestModelBeta, backref='gammas')
Expand All @@ -40,15 +39,24 @@ def __str__(self):
return '<%s id=%s> %s' % (self.__class__.__name__, self.id, self.text)


class UUIDTestModel(AioModel):
class UUIDTestModel(peewee_async.AioModel):
id = peewee.UUIDField(primary_key=True, default=uuid.uuid4)
text = peewee.CharField()

def __str__(self):
return '<%s id=%s> %s' % (self.__class__.__name__, self.id, self.text)


class CompositeTestModel(AioModel):
class CompatTestModel(peewee.Model):
id = peewee.UUIDField(primary_key=True, default=uuid.uuid4)
text = peewee.CharField(max_length=100, unique=True)
data = peewee.TextField(default='')

def __str__(self):
return '<%s id=%s> %s' % (self.__class__.__name__, self.id, self.text)


class CompositeTestModel(peewee_async.AioModel):
"""A simple "through" table for many-to-many relationship."""
uuid = peewee.ForeignKeyField(UUIDTestModel)
alpha = peewee.ForeignKeyField(TestModelAlpha)
Expand All @@ -58,6 +66,6 @@ class Meta:


ALL_MODELS = (
TestModel, UUIDTestModel, TestModelAlpha,
TestModelBeta, TestModelGamma, CompositeTestModel
TestModel, UUIDTestModel, TestModelAlpha, TestModelBeta, TestModelGamma,
CompatTestModel, CompositeTestModel
)
9 changes: 3 additions & 6 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import uuid

import peewee as pw
import peewee
import pytest

import peewee_async
Expand All @@ -28,7 +28,6 @@ async def test_multiple_iterate_over_result(manager):
TestModel.select().order_by(TestModel.text))

assert list(result) == [obj1, obj2]
assert list(result) == [obj1, obj2]


@all_dbs
Expand Down Expand Up @@ -122,7 +121,7 @@ async def test_allow_sync_is_reverted_for_exc(manager):
ununique_text = "ununique_text"
await manager.create(TestModel, text=ununique_text)
await manager.create(TestModel, text=ununique_text)
except pw.IntegrityError:
except peewee.IntegrityError:
pass
assert manager.database._allow_sync is False

Expand Down Expand Up @@ -196,11 +195,9 @@ async def test_deferred_init(params, db_cls):
[
(DB_DEFAULTS[name], db_cls) for name, db_cls in DB_CLASSES.items()
]
)
async def test_proxy_database(params, db_cls):

database = pw.Proxy()
database = peewee.Proxy()
TestModel._meta.database = database
manager = peewee_async.Manager(database)

Expand Down
41 changes: 41 additions & 0 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import uuid

from tests.conftest import all_dbs
from tests.models import CompatTestModel


@all_dbs
async def test_create_select_compat_mode(manager):
obj1 = await manager.create(CompatTestModel, text="Test 1")
obj2 = await manager.create(CompatTestModel, text="Test 2")
query = CompatTestModel.select().order_by(CompatTestModel.text)
result = await manager.execute(query)
assert list(result) == [obj1, obj2]


@all_dbs
async def test_update_compat_mode(manager):
obj_draft = await manager.create(CompatTestModel, text="Draft 1")
obj_draft.text = "Final result"
await manager.update(obj_draft)
obj = await manager.get(CompatTestModel, id=obj_draft.id)
assert obj.text == "Final result"


@all_dbs
async def test_count_compat_mode(manager):
obj = await manager.create(CompatTestModel, text="Unique title %s" % uuid.uuid4())
search = CompatTestModel.select().where(CompatTestModel.text == obj.text)
count = await manager.count(search)
assert count == 1


@all_dbs
async def test_delete_compat_mode(manager):
obj = await manager.create(CompatTestModel, text="Expired item %s" % uuid.uuid4())
search = CompatTestModel.select().where(CompatTestModel.id == obj.id)
count_before = await manager.count(search)
assert count_before == 1
await manager.delete(obj)
count_after = await manager.count(search)
assert count_after == 0
3 changes: 1 addition & 2 deletions tests/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import uuid

import peewee
import peewee as pw
import pytest

from tests.conftest import all_dbs
Expand Down Expand Up @@ -94,7 +93,7 @@ async def test_scalar_query(manager):
text = "Test %s" % uuid.uuid4()
await manager.create(TestModel, text=text)

fn = pw.fn.Count(TestModel.id)
fn = peewee.fn.Count(TestModel.id)
count = await manager.scalar(TestModel.select(fn))

assert count == 2
Expand Down
2 changes: 0 additions & 2 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import asyncio

import pytest

from tests.conftest import all_dbs
from tests.models import TestModel

Expand Down

0 comments on commit 06386fa

Please sign in to comment.