-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add support for create_many_and_return
- Loading branch information
1 parent
78f0395
commit 0ce26e0
Showing
9 changed files
with
2,446 additions
and
536 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
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,70 @@ | ||
import pytest | ||
|
||
from prisma import Prisma | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_create_many_and_return(client: Prisma) -> None: | ||
"""Standard usage""" | ||
records = await client.user.create_many_and_return([{'name': 'Robert'}, {'name': 'Tegan'}]) | ||
assert len(records) == 2 | ||
assert records[0].name == 'Robert' | ||
assert records[1].name == 'Tegan' | ||
|
||
user = await client.user.find_first(where={'name': 'Robert'}) | ||
assert user is not None | ||
assert user.name == 'Robert' | ||
|
||
assert await client.user.count() == 2 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_required_relation_key_field(client: Prisma) -> None: | ||
"""Explicitly passing a field used as a foreign key connects the relations""" | ||
user = await client.user.create( | ||
data={ | ||
'name': 'Robert', | ||
}, | ||
) | ||
user2 = await client.user.create( | ||
data={ | ||
'name': 'Robert', | ||
}, | ||
) | ||
records = await client.profile.create_many_and_return( | ||
data=[ | ||
{'user_id': user.id, 'description': 'Foo', 'country': 'Scotland'}, | ||
{ | ||
'user_id': user2.id, | ||
'description': 'Foo 2', | ||
'country': 'Scotland', | ||
}, | ||
], | ||
) | ||
assert len(records) == 2 | ||
assert records[0].description == 'Foo' | ||
assert records[1].description == 'Foo 2' | ||
|
||
found = await client.user.find_unique( | ||
where={ | ||
'id': user.id, | ||
}, | ||
include={ | ||
'profile': True, | ||
}, | ||
) | ||
assert found is not None | ||
assert found.profile is not None | ||
assert found.profile.description == 'Foo' | ||
|
||
found = await client.user.find_unique( | ||
where={ | ||
'id': user2.id, | ||
}, | ||
include={ | ||
'profile': True, | ||
}, | ||
) | ||
assert found is not None | ||
assert found.profile is not None | ||
assert found.profile.description == 'Foo 2' |
22 changes: 22 additions & 0 deletions
22
databases/tests/test_create_many_and_return_skip_duplicates.py
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,22 @@ | ||
import pytest | ||
|
||
import prisma | ||
from prisma import Prisma | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_create_many_and_return_skip_duplicates(client: Prisma) -> None: | ||
"""Skipping duplcates ignores unique constraint errors""" | ||
user = await client.user.create({'name': 'Robert'}) | ||
|
||
with pytest.raises(prisma.errors.UniqueViolationError) as exc: | ||
await client.user.create_many_and_return([{'id': user.id, 'name': 'Robert 2'}]) | ||
|
||
assert exc.match(r'Unique constraint failed') | ||
|
||
records = await client.user.create_many_and_return( | ||
[{'id': user.id, 'name': 'Robert 2'}, {'name': 'Tegan'}], | ||
skip_duplicates=True, | ||
) | ||
assert len(records) == 1 | ||
assert records[0].name == 'Tegan' |
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
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
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
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
Oops, something went wrong.