Skip to content

Commit

Permalink
add Decimal field (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee committed Sep 3, 2021
1 parent ea9153a commit 2649ede
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 11 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ jobs:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10.0-rc.1"]

services:
postgres:
image: postgres:10.8
env:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
POSTGRES_DB: testsuite
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- uses: "actions/checkout@v2"
- uses: "actions/setup-python@v2"
Expand All @@ -28,6 +39,8 @@ jobs:
- name: "Build package & docs"
run: "scripts/build"
- name: "Run tests"
env:
TEST_DATABASE_URL: "postgresql://username:password@localhost:5432/testsuite"
run: "scripts/test"
- name: "Enforce coverage"
run: "scripts/coverage"
1 change: 1 addition & 0 deletions docs/declaring_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ See `TypeSystem` for [type-specific validation keyword arguments][typesystem-fie
* `orm.Boolean()`
* `orm.Date()`
* `orm.DateTime()`
* `orm.Decimal()`
* `orm.Enum()`
* `orm.Float()`
* `orm.Integer()`
Expand Down
4 changes: 3 additions & 1 deletion orm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Boolean,
Date,
DateTime,
Decimal,
Enum,
Float,
ForeignKey,
Expand All @@ -15,14 +16,15 @@
)
from orm.models import Model

__version__ = "0.1.7"
__version__ = "0.1.8"
__all__ = [
"NoMatch",
"MultipleMatches",
"BigInteger",
"Boolean",
"Date",
"DateTime",
"Decimal",
"Enum",
"Float",
"Integer",
Expand Down
12 changes: 12 additions & 0 deletions orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ def __init__(self, enum, **kwargs):

def get_column_type(self):
return sqlalchemy.Enum(self.enum)


class Decimal(ModelField, typesystem.Decimal):
def __init__(self, max_digits: int, decimal_places: int, **kwargs):
assert max_digits, "max_digits is required"
assert decimal_places, "decimal_places is required"
self.max_digits = max_digits
self.decimal_places = decimal_places
super().__init__(**kwargs)

def get_column_type(self):
return sqlalchemy.Numeric(precision=self.max_digits, scale=self.decimal_places)
2 changes: 1 addition & 1 deletion orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ async def load(self):
row = await self.__database__.fetch_one(expr)

# Update the instance.
for key, value in dict(row).items():
for key, value in dict(row._mapping).items():
setattr(self, key, value)

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
databases[sqlite]
databases[postgresql]
psycopg2
typesystem

# Packaging
Expand Down
4 changes: 3 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///test.db")
assert "TEST_DATABASE_URL" in os.environ, "TEST_DATABASE_URL is not set."

DATABASE_URL = os.environ["TEST_DATABASE_URL"]
9 changes: 8 additions & 1 deletion tests/test_columns.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import decimal
from enum import Enum

import databases
Expand Down Expand Up @@ -35,6 +36,7 @@ class Example(orm.Model):
created_time = orm.Time(default=time)
description = orm.Text(allow_blank=True)
value = orm.Float(allow_null=True)
price = orm.Decimal(max_digits=5, decimal_places=2, allow_null=True)
data = orm.JSON(default={})
status = orm.Enum(StatusEnum, default=StatusEnum.DRAFT)

Expand All @@ -57,13 +59,18 @@ async def test_model_crud():
assert example.created_day == datetime.date.today()
assert example.description == ""
assert example.value is None
assert example.price is None
assert example.data == {}
assert example.status == StatusEnum.DRAFT

await example.update(
data={"foo": 123}, value=123.456, status=StatusEnum.RELEASED
data={"foo": 123},
value=123.456,
status=StatusEnum.RELEASED,
price=decimal.Decimal("999.99"),
)
example = await Example.objects.get()
assert example.value == 123.456
assert example.data == {"foo": 123}
assert example.status == StatusEnum.RELEASED
assert example.price == decimal.Decimal("999.99")
8 changes: 2 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,19 @@ async def test_model_order_by():

users = await User.objects.order_by("name", "-id").all()
assert users[0].name == "Allen"
assert users[0].id == 2
assert users[1].name == "Bob"
assert users[1].id == 3
assert users[0].id < users[1].id

users = await User.objects.filter(name="Bob").order_by("-id").all()
assert users[0].name == "Bob"
assert users[0].id == 3
assert users[1].name == "Bob"
assert users[1].id == 1
assert users[0].id > users[1].id

users = await User.objects.order_by("id").limit(1).all()
assert users[0].name == "Bob"
assert users[0].id == 1

users = await User.objects.order_by("id").limit(1).offset(1).all()
assert users[0].name == "Allen"
assert users[0].id == 2


async def test_model_exists():
Expand Down

0 comments on commit 2649ede

Please sign in to comment.