-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
304 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import uuid | ||
|
||
from tests.conftest import postgres_only, all_dbs | ||
from tests.models import TestModel | ||
from tests.utils import model_has_fields | ||
|
||
|
||
@all_dbs | ||
async def test_delete__count(manager): | ||
query = TestModel.insert_many([ | ||
{'text': "Test %s" % uuid.uuid4()}, | ||
{'text': "Test %s" % uuid.uuid4()}, | ||
]) | ||
await query.aio_execute() | ||
|
||
count = await TestModel.delete().aio_execute() | ||
|
||
assert count == 2 | ||
|
||
|
||
@all_dbs | ||
async def test_delete__by_condition(manager): | ||
expected_text = "text1" | ||
deleted_text = "text2" | ||
query = TestModel.insert_many([ | ||
{'text': expected_text}, | ||
{'text': deleted_text}, | ||
]) | ||
await query.aio_execute() | ||
|
||
await TestModel.delete().where(TestModel.text == deleted_text).aio_execute() | ||
|
||
res = await TestModel.select().aio_execute() | ||
assert len(res) == 1 | ||
assert res[0].text == expected_text | ||
|
||
|
||
@postgres_only | ||
async def test_delete__return_model(manager): | ||
m = await TestModel.aio_create(text="text", data="data") | ||
|
||
res = await TestModel.delete().returning(TestModel).aio_execute() | ||
assert model_has_fields(res[0], { | ||
"id": m.id, | ||
"text": m.text, | ||
"data": m.data | ||
}) is True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import uuid | ||
|
||
from tests.conftest import postgres_only, all_dbs | ||
from tests.models import TestModel, UUIDTestModel | ||
from tests.utils import model_has_fields | ||
|
||
|
||
@all_dbs | ||
async def test_insert_many(manager): | ||
last_id = await TestModel.insert_many([ | ||
{'text': "Test %s" % uuid.uuid4()}, | ||
{'text': "Test %s" % uuid.uuid4()}, | ||
]).aio_execute() | ||
|
||
res = await TestModel.select().aio_execute() | ||
|
||
assert len(res) == 2 | ||
assert last_id in [m.id for m in res] | ||
|
||
|
||
@all_dbs | ||
async def test_insert__return_id(manager): | ||
last_id = await TestModel.insert(text="Test %s" % uuid.uuid4()).aio_execute() | ||
|
||
res = await TestModel.select().aio_execute() | ||
obj = res[0] | ||
assert last_id == obj.id | ||
|
||
|
||
@postgres_only | ||
async def test_insert_on_conflict_ignore__last_id_is_none(manager): | ||
query = TestModel.insert(text="text").on_conflict_ignore() | ||
await query.aio_execute() | ||
|
||
last_id = await query.aio_execute() | ||
|
||
assert last_id is None | ||
|
||
|
||
@postgres_only | ||
async def test_insert_on_conflict_ignore__return_model(manager): | ||
query = TestModel.insert(text="text", data="data").on_conflict_ignore().returning(TestModel) | ||
|
||
res = await query.aio_execute() | ||
|
||
inserted = res[0] | ||
res = await TestModel.select().aio_execute() | ||
expected = res[0] | ||
|
||
assert model_has_fields(inserted, { | ||
"id": expected.id, | ||
"text": expected.text, | ||
"data": expected.data | ||
}) is True | ||
|
||
|
||
@postgres_only | ||
async def test_insert_on_conflict_ignore__inserted_once(manager): | ||
query = TestModel.insert(text="text").on_conflict_ignore() | ||
last_id = await query.aio_execute() | ||
|
||
await query.aio_execute() | ||
|
||
res = await TestModel.select().aio_execute() | ||
assert len(res) == 1 | ||
assert res[0].id == last_id | ||
|
||
|
||
@postgres_only | ||
async def test_insert__uuid_pk(manager): | ||
query = UUIDTestModel.insert(text="Test %s" % uuid.uuid4()) | ||
last_id = await query.aio_execute() | ||
assert len(str(last_id)) == 36 | ||
|
||
|
||
@postgres_only | ||
async def test_insert__return_model(manager): | ||
text = "Test %s" % uuid.uuid4() | ||
data = "data" | ||
query = TestModel.insert(text=text, data=data).returning(TestModel) | ||
|
||
res = await query.aio_execute() | ||
|
||
inserted = res[0] | ||
assert model_has_fields( | ||
inserted, {"id": inserted.id, "text": text, "data": data} | ||
) is True | ||
|
||
|
||
@postgres_only | ||
async def test_insert_many__return_model(manager): | ||
texts = [f"text{n}" for n in range(2)] | ||
query = TestModel.insert_many([ | ||
{"text": text} for text in texts | ||
]).returning(TestModel) | ||
|
||
res = await query.aio_execute() | ||
|
||
texts = [m.text for m in res] | ||
assert sorted(texts) == ["text0", "text1"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import uuid | ||
|
||
import pytest | ||
|
||
from tests.conftest import all_dbs, postgres_only | ||
from tests.models import TestModel, TestModelAlpha, TestModelBeta | ||
|
||
|
||
@all_dbs | ||
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") | ||
|
||
result = (await TestModelBeta.select(TestModelBeta, TestModelAlpha).join( | ||
TestModelAlpha, | ||
attr="joined_alpha", | ||
).aio_execute())[0] | ||
|
||
assert result.id == beta.id | ||
assert result.joined_alpha.id == alpha.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import uuid | ||
|
||
import pytest | ||
|
||
from tests.conftest import all_dbs, postgres_only | ||
from tests.models import TestModel, TestModelAlpha, TestModelBeta | ||
|
||
|
||
|
||
@all_dbs | ||
async def test_aio_get(manager): | ||
obj1 = await TestModel.aio_create(text="Test 1") | ||
obj2 = await TestModel.aio_create(text="Test 2") | ||
|
||
result = await TestModel.aio_get(TestModel.id == obj1.id) | ||
assert result.id == obj1.id | ||
|
||
result = await TestModel.aio_get(TestModel.text == "Test 2") | ||
assert result.id == obj2.id | ||
|
||
with pytest.raises(TestModel.DoesNotExist): | ||
await TestModel.aio_get(TestModel.text == "unknown") | ||
|
||
|
||
@all_dbs | ||
async def test_aio_get_or_none(manager): | ||
obj1 = await TestModel.aio_create(text="Test 1") | ||
|
||
result = await TestModel.aio_get_or_none(TestModel.id == obj1.id) | ||
assert result.id == obj1.id | ||
|
||
result = await TestModel.aio_get_or_none(TestModel.text == "unknown") | ||
assert result is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters