From fbcd108e92acbfe25280442093e1a554d2993992 Mon Sep 17 00:00:00 2001 From: Janne Vanhala Date: Thu, 21 May 2015 13:31:59 +0300 Subject: [PATCH] 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. --- CHANGES.rst | 6 +++++ sqlalchemy_utils/asserts.py | 22 ++++++--------- tests/test_asserts.py | 53 ++++++++++++++----------------------- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f229f34..f1523ad 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) ^^^^^^^^^^^^^^^^^^^ diff --git a/sqlalchemy_utils/asserts.py b/sqlalchemy_utils/asserts.py index a7f5ee9..4800c01 100644 --- a/sqlalchemy_utils/asserts.py +++ b/sqlalchemy_utils/asserts.py @@ -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,10 +57,15 @@ 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) - session = sa.orm.object_session(obj) - session.rollback() + except expected_exc: + pass + else: + raise AssertionError('Expected update to raise %s' % expected_exc) + finally: + session = sa.orm.object_session(obj) + session.rollback() def _repeated_value(type_): diff --git a/tests/test_asserts.py b/tests/test_asserts.py index ba1f820..623ff06 100644 --- a/tests/test_asserts.py +++ b/tests/test_asserts.py @@ -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)