Skip to content

Commit

Permalink
Merge branch 'master' into orm
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Feb 9, 2021
2 parents 8842889 + d255726 commit aba137c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pony/orm/dbapiprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,10 @@ def validate(converter, val, obj=None):
'Value type for attribute %s must be int. Got string %r' % (converter.attr, val))
else: throw(TypeError, 'Value type for attribute %s must be int. Got: %r' % (converter.attr, type(val)))

if converter.min_val and val < converter.min_val:
if converter.min_val is not None and val < converter.min_val:
throw(ValueError, 'Value %r of attr %s is less than the minimum allowed value %r'
% (val, converter.attr, converter.min_val))
if converter.max_val and val > converter.max_val:
if converter.max_val is not None and val > converter.max_val:
throw(ValueError, 'Value %r of attr %s is greater than the maximum allowed value %r'
% (val, converter.attr, converter.max_val))
return val
Expand Down
52 changes: 52 additions & 0 deletions pony/orm/tests/test_int_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from __future__ import absolute_import, print_function, division

import unittest

from pony import orm
from pony.orm.tests.testutils import *
from pony.orm.tests import setup_database, teardown_database

class TestIntConverter1(unittest.TestCase):
def setUp(self):
self.db = db = orm.Database()

class Foo(db.Entity):
id = orm.PrimaryKey(int)
x = orm.Required(int, size=8, unsigned=True)

setup_database(db)

with orm.db_session:
foo = Foo(id=123, x=1)

def tearDown(self):
teardown_database(self.db)

def test_1(self):
with orm.db_session:
foo = self.db.Foo[123]
foo.x -= 1
with orm.db_session:
foo = self.db.Foo[123]
self.assertEqual(foo.x, 0)

@raises_exception(ValueError, "Value -1 of attr Foo.x is less than the minimum allowed value 0")
@orm.db_session
def test_2(self):
foo = self.db.Foo[123]
foo.x -= 2

@orm.db_session
def test_3(self):
with orm.db_session:
foo = self.db.Foo[123]
foo.x += 254
with orm.db_session:
foo = self.db.Foo[123]
self.assertEqual(foo.x, 255)

@raises_exception(ValueError, "Value 256 of attr Foo.x is greater than the maximum allowed value 255")
@orm.db_session
def test_4(self):
foo = self.db.Foo[123]
foo.x += 255

0 comments on commit aba137c

Please sign in to comment.