Fix asserts expecting failing update

`sqlalchemy_utils.asserts.raises` was broken. I removed it and replaced
it's usage with traditional `try..except` and with `pytest.raises` in tests.
This commit is contained in:
Janne Vanhala
2015-05-21 13:31:59 +03:00
parent 48064bac1d
commit fbcd108e92
3 changed files with 34 additions and 47 deletions

View File

@@ -4,6 +4,12 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release.
0.30.2 (not yet released)
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed ``assert_max_length``, ``assert_non_nullable``, ``assert_min_value`` and ``assert_max_value`` not properly raising an ``AssertionError`` when the assertion failed.
0.30.1 (2015-05-06)
^^^^^^^^^^^^^^^^^^^

View File

@@ -39,17 +39,6 @@ from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.exc import DataError, IntegrityError
class raises(object):
def __init__(self, expected_exc):
self.expected_exc = expected_exc
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return exc_type == self.expected_exc
def _update_field(obj, field, value):
session = sa.orm.object_session(obj)
table = sa.inspect(obj.__class__).columns[field].table
@@ -68,8 +57,13 @@ def _expect_successful_update(obj, field, value, reraise_exc):
def _expect_failing_update(obj, field, value, expected_exc):
with raises(expected_exc):
try:
_update_field(obj, field, value)
except expected_exc:
pass
else:
raise AssertionError('Expected update to raise %s' % expected_exc)
finally:
session = sa.orm.object_session(obj)
session.rollback()

View File

@@ -9,22 +9,9 @@ from sqlalchemy_utils import (
assert_non_nullable,
assert_nullable
)
from sqlalchemy_utils.asserts import raises
from tests import TestCase
class TestRaises(object):
def test_matching_exception(self):
with raises(Exception):
raise Exception()
assert True
def test_non_matchin_exception(self):
with pytest.raises(Exception):
with raises(ValueError):
raise Exception()
class AssertionTestCase(TestCase):
dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'
@@ -67,15 +54,15 @@ class TestAssertMaxLengthWithArray(AssertionTestCase):
assert_max_length(self.user, 'fav_numbers', 8)
def test_smaller_than_max_length(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'fav_numbers', 7)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'fav_numbers', 7)
def test_bigger_than_max_length(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'fav_numbers', 9)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'fav_numbers', 9)
@@ -86,9 +73,9 @@ class TestAssertNonNullable(AssertionTestCase):
assert_non_nullable(self.user, 'age')
def test_nullable_column(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_non_nullable(self.user, 'name')
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_non_nullable(self.user, 'name')
@@ -98,9 +85,9 @@ class TestAssertNullable(AssertionTestCase):
assert_nullable(self.user, 'name')
def test_non_nullable_column(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_nullable(self.user, 'age')
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_nullable(self.user, 'age')
@@ -114,15 +101,15 @@ class TestAssertMaxLength(AssertionTestCase):
assert_max_length(self.user, 'email', 200)
def test_smaller_than_max_length(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'name', 19)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'name', 19)
def test_bigger_than_max_length(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'name', 21)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_length(self.user, 'name', 21)
@@ -132,15 +119,15 @@ class TestAssertMinValue(AssertionTestCase):
assert_min_value(self.user, 'age', 0)
def test_smaller_than_min_value(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_min_value(self.user, 'age', -1)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_min_value(self.user, 'age', -1)
def test_bigger_than_min_value(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_min_value(self.user, 'age', 1)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_min_value(self.user, 'age', 1)
@@ -150,13 +137,13 @@ class TestAssertMaxValue(AssertionTestCase):
assert_max_value(self.user, 'age', 150)
def test_smaller_than_max_value(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_value(self.user, 'age', 149)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_value(self.user, 'age', 149)
def test_bigger_than_max_value(self):
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_value(self.user, 'age', 151)
with raises(AssertionError):
with pytest.raises(AssertionError):
assert_max_value(self.user, 'age', 151)