-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* The bulk_update is an adaptation of encode/orm#148
- Loading branch information
Showing
7 changed files
with
308 additions
and
22 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import datetime | ||
from enum import Enum | ||
|
||
import pytest | ||
from tests.settings import DATABASE_URL | ||
|
||
import saffier | ||
from saffier import fields | ||
from saffier.db.connection import Database | ||
|
||
pytestmark = pytest.mark.anyio | ||
|
||
database = Database(DATABASE_URL) | ||
models = saffier.Registry(database=database) | ||
|
||
|
||
def time(): | ||
return datetime.datetime.now().time() | ||
|
||
|
||
class StatusEnum(Enum): | ||
DRAFT = "Draft" | ||
RELEASED = "Released" | ||
|
||
|
||
class Product(saffier.Model): | ||
id = fields.IntegerField(primary_key=True) | ||
uuid = fields.UUIDField(null=True) | ||
created = fields.DateTimeField(default=datetime.datetime.now) | ||
created_day = fields.DateField(default=datetime.date.today) | ||
created_time = fields.TimeField(default=time) | ||
created_date = fields.DateField(auto_now_add=True) | ||
created_datetime = fields.DateTimeField(auto_now_add=True) | ||
updated_datetime = fields.DateTimeField(auto_now=True) | ||
updated_date = fields.DateField(auto_now=True) | ||
data = fields.JSONField(default={}) | ||
description = fields.CharField(blank=True, max_length=255) | ||
huge_number = fields.BigIntegerField(default=0) | ||
price = fields.DecimalField(max_digits=5, decimal_places=2, null=True) | ||
status = fields.ChoiceField(StatusEnum, default=StatusEnum.DRAFT) | ||
value = fields.FloatField(null=True) | ||
|
||
class Meta: | ||
registry = models | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="module") | ||
async def create_test_database(): | ||
await models.create_all() | ||
yield | ||
await models.drop_all() | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
async def rollback_transactions(): | ||
with database.force_rollback(): | ||
async with database: | ||
yield | ||
|
||
|
||
async def test_bulk_create(): | ||
await Product.query.bulk_create( | ||
[ | ||
{"data": {"foo": 123}, "value": 123.456, "status": StatusEnum.RELEASED}, | ||
{"data": {"foo": 456}, "value": 456.789, "status": StatusEnum.DRAFT}, | ||
] | ||
) | ||
products = await Product.query.all() | ||
assert len(products) == 2 | ||
assert products[0].data == {"foo": 123} | ||
assert products[0].value == 123.456 | ||
assert products[0].status == StatusEnum.RELEASED | ||
assert products[1].data == {"foo": 456} | ||
assert products[1].value == 456.789 | ||
assert products[1].status == StatusEnum.DRAFT |
Oops, something went wrong.