Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Harley committed Sep 12, 2018
2 parents 2b35dbc + 7468067 commit 75cc24b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Minimal OpenAPI asynchronous server application
"""

__version__ = '0.8.3'
__version__ = '0.8.4'
23 changes: 15 additions & 8 deletions openapi/data/fields.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import decimal
from datetime import date, datetime
from numbers import Number
from uuid import UUID
from datetime import datetime, date
from dataclasses import dataclass, field, Field

from email_validator import validate_email, EmailNotValidError

from dataclasses import Field, dataclass, field
from dateutil.parser import parse as parse_date
from email_validator import EmailNotValidError, validate_email

from .. import json

from ..utils import compact_dict


DEFAULT = 'default'
REQUIRED = 'required'
VALIDATOR = 'OPENAPI_VALIDATOR'
Expand Down Expand Up @@ -326,13 +324,20 @@ def __init__(self, min_value=None, max_value=None, precision=None):

def __call__(self, field, value, data=None):
try:
value = round(value, self.precision)
if not isinstance(value, Number):
raise TypeError

if self.precision is not None:
value = round(value, self.precision)

except (ValueError, TypeError):
raise ValidationError(field.name, '%s not valid number' % value)
return super().__call__(field, value, data=data)

def dump(self, value):
return round(value, self.precision)
if self.precision is not None:
return round(value, self.precision)
return value


class IntegerValidator(BoundedNumberValidator):
Expand All @@ -349,6 +354,8 @@ def __call__(self, field, value, data=None):
class DecimalValidator(NumberValidator):
def __call__(self, field, value, data=None):
try:
if isinstance(value, float):
value = str(value)
value = decimal.Decimal(value)
except (TypeError, decimal.InvalidOperation):
raise ValidationError(field.name, '%s not valid Decimal' % value)
Expand Down
22 changes: 15 additions & 7 deletions tests/data/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,17 @@ def test_DecimalValidator_valid():
assert validator(field, 10) == 10
assert validator(field, -10) == -10
assert validator(field, 5.55) == Decimal('5.55')
# assert validator(field, 5.555) == Decimal(5.55)
# assert validator(field, '5.555') == Decimal(5.55)
# assert validator(field, (5, 555)) == Decimal(5.55)
assert validator(field, 5.555) == Decimal('5.56')
assert validator(field, '5.555') == Decimal('5.56')


def test_DecimalValidator_precision_None():
field = decimal_field()
validator = DecimalValidator(min_value=-10, max_value=10)
assert validator(field, 10) == 10
assert validator(field, -10) == -10
assert validator(field, 5.555) == Decimal('5.555')
assert validator(field, 5.555555555555555) == Decimal('5.555555555555555')


def test_DecimalValidator_invalid():
Expand All @@ -208,10 +216,10 @@ def test_DecimalValidator_invalid():

def test_DecimalValidator_dump():
validator = DecimalValidator(min_value=-10, max_value=10, precision=2)
assert validator.dump(10) == 10
assert validator.dump(-10) == -10
assert validator.dump(5.55) == 5.55
assert validator.dump(5.556) == 5.56
assert validator.dump(Decimal(10)) == Decimal('10')
assert validator.dump(Decimal(-10)) == Decimal('-10')
assert validator.dump(Decimal('5.55')) == Decimal('5.55')
assert validator.dump(Decimal('5.556')) == Decimal('5.56')


def test_email_validator_valid():
Expand Down
4 changes: 2 additions & 2 deletions tests/example/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def get(self):
description: Authenticated tasks
"""
data = await self.get_list()
return web.json_response(data)
return self.json_response(data)

@op(response_schema=Task, body_schema=TaskAdd)
async def post(self):
Expand All @@ -59,7 +59,7 @@ async def post(self):
description: Failed validation
"""
data = await self.create_one()
return web.json_response(data, status=201)
return self.json_response(data, status=201)

@op(query_schema=TaskQuery)
async def delete(self):
Expand Down

0 comments on commit 75cc24b

Please sign in to comment.