Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracted compatibility module to work without AioModel subclassing #232

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
474 changes: 28 additions & 446 deletions peewee_async.py

Large diffs are not rendered by default.

438 changes: 438 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
24 changes: 21 additions & 3 deletions tests/aio_model/test_selecting.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import peewee
from tests.conftest import all_dbs
from tests.models import TestModelAlpha, TestModelBeta
from tests.models import TestModel, TestModelAlpha, TestModelBeta


@all_dbs
async def test__select__w_join(manager):
async def test_select_w_join(manager):
alpha = await TestModelAlpha.aio_create(text="Test 1")
beta = await TestModelBeta.aio_create(alpha_id=alpha.id, text="text")

Expand All @@ -13,4 +14,21 @@ async def test__select__w_join(manager):
).aio_execute())[0]

assert result.id == beta.id
assert result.joined_alpha.id == alpha.id
assert result.joined_alpha.id == alpha.id


@all_dbs
async def test_select_compound(manager):
obj1 = await manager.create(TestModel, text="Test 1")
obj2 = await manager.create(TestModel, text="Test 2")
query = (
TestModel.select().where(TestModel.id == obj1.id) |
TestModel.select().where(TestModel.id == obj2.id)
)
assert isinstance(query, peewee.ModelCompoundSelectQuery)
# NOTE: Two `AioModelSelect` when joining via `|` produce `ModelCompoundSelectQuery`
# without `aio_execute()` method, so using `database.aio_execute()` here.
result = await manager.database.aio_execute(query)
assert len(list(result)) == 2
assert obj1 in list(result)
assert obj2 in list(result)
10 changes: 6 additions & 4 deletions tests/db_config.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import os
import peewee_async
import peewee_asyncext


PG_DEFAULTS = {
'database': 'postgres',
'host': '127.0.0.1',
'port': 5432,
'port': int(os.environ.get('POSTGRES_PORT', 5432)),
'password': 'postgres',
'user': 'postgres',
"connect_timeout": 30
'connect_timeout': 30
}

MYSQL_DEFAULTS = {
'database': 'mysql',
'host': '127.0.0.1',
'port': 3306,
'port': int(os.environ.get('MYSQL_PORT', 3306)),
'user': 'root',
'password': 'mysql',
"connect_timeout": 30
'connect_timeout': 30
}

DB_DEFAULTS = {
Expand All @@ -28,6 +29,7 @@
'mysql': MYSQL_DEFAULTS,
'mysql-pool': MYSQL_DEFAULTS
}

DB_CLASSES = {
'postgres': peewee_async.PostgresqlDatabase,
'postgres-ext': peewee_asyncext.PostgresqlExtDatabase,
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
70 changes: 70 additions & 0 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import uuid

import peewee
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)
assert isinstance(query, peewee.ModelSelect)
result = await manager.execute(query)
assert list(result) == [obj1, obj2]


@all_dbs
async def test_compound_select_compat_mode(manager):
obj1 = await manager.create(CompatTestModel, text="Test 1")
obj2 = await manager.create(CompatTestModel, text="Test 2")
query = (
CompatTestModel.select().where(CompatTestModel.id == obj1.id) |
CompatTestModel.select().where(CompatTestModel.id == obj2.id)
)
assert isinstance(query, peewee.ModelCompoundSelectQuery)
result = await manager.execute(query)
assert len(list(result)) == 2
assert obj1 in list(result)
assert obj2 in list(result)


@all_dbs
async def test_raw_select_compat_mode(manager):
obj1 = await manager.create(CompatTestModel, text="Test 1")
obj2 = await manager.create(CompatTestModel, text="Test 2")
query = CompatTestModel.raw(
'SELECT id, text, data FROM compattestmodel m ORDER BY m.text'
)
assert isinstance(query, peewee.ModelRaw)
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
Loading