From c97417d8e7cc1fb847a9f02a4a6029f5cb500f3d Mon Sep 17 00:00:00 2001 From: Shawn Falkner-Horine Date: Mon, 25 Aug 2014 15:09:38 -0700 Subject: [PATCH] Error handling: Model-level validations, proper error responses. (backport assert_raises as context manager for Python 2.6) --- tests/backport_assert_raises.py | 37 +++++++++++++++++++++ tests/test_ec2/test_amis.py | 5 ++- tests/test_ec2/test_dhcp_options.py | 5 ++- tests/test_ec2/test_elastic_block_store.py | 5 ++- tests/test_ec2/test_elastic_ip_addresses.py | 6 ++-- tests/test_ec2/test_general.py | 5 ++- tests/test_ec2/test_instances.py | 5 ++- tests/test_ec2/test_internet_gateways.py | 5 ++- tests/test_ec2/test_key_pairs.py | 5 ++- tests/test_ec2/test_security_groups.py | 5 ++- tests/test_ec2/test_subnets.py | 5 ++- tests/test_ec2/test_vpc_peering.py | 5 ++- tests/test_ec2/test_vpcs.py | 5 ++- 13 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 tests/backport_assert_raises.py diff --git a/tests/backport_assert_raises.py b/tests/backport_assert_raises.py new file mode 100644 index 000000000..a6d1faa8a --- /dev/null +++ b/tests/backport_assert_raises.py @@ -0,0 +1,37 @@ +""" +Patch courtesy of: +https://marmida.com/blog/index.php/2012/08/08/monkey-patching-assert_raises/ +""" + +# code for monkey-patching +import nose.tools + +# let's fix nose.tools.assert_raises (which is really unittest.assertRaises) +# so that it always supports context management + +# in order for these changes to be available to other modules, you'll need +# to guarantee this module is imported by your fixture before either nose or +# unittest are imported + +try: + nose.tools.assert_raises(Exception) +except TypeError: + # this version of assert_raises doesn't support the 1-arg version + class AssertRaisesContext(object): + def __init__(self, expected): + self.expected = expected + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, tb): + self.exception = exc_val + nose.tools.assert_equal(exc_type, self.expected) + # if you get to this line, the last assertion must have passed + # suppress the propagation of this exception + return True + + def assert_raises_context(exc_type): + return AssertRaisesContext(exc_type) + + nose.tools.assert_raises = assert_raises_context diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py index d7bbb9b1b..27201a2d1 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -1,8 +1,11 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_dhcp_options.py b/tests/test_ec2/test_dhcp_options.py index 26b85da2b..4fd2eb946 100644 --- a/tests/test_ec2/test_dhcp_options.py +++ b/tests/test_ec2/test_dhcp_options.py @@ -1,8 +1,11 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_elastic_block_store.py b/tests/test_ec2/test_elastic_block_store.py index fe4475ea1..995dd018c 100644 --- a/tests/test_ec2/test_elastic_block_store.py +++ b/tests/test_ec2/test_elastic_block_store.py @@ -1,7 +1,10 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 from moto.ec2.models import ec2_backend diff --git a/tests/test_ec2/test_elastic_ip_addresses.py b/tests/test_ec2/test_elastic_ip_addresses.py index 35418e7bd..ebdbcbc2f 100644 --- a/tests/test_ec2/test_elastic_ip_addresses.py +++ b/tests/test_ec2/test_elastic_ip_addresses.py @@ -1,9 +1,11 @@ -"""Test mocking of Elatic IP Address""" +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_general.py b/tests/test_ec2/test_general.py index 23b773812..b64b476d5 100644 --- a/tests/test_ec2/test_general.py +++ b/tests/test_ec2/test_general.py @@ -1,7 +1,10 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 9f93121c6..91b1ddc53 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -1,10 +1,13 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import base64 import boto from boto.ec2.instance import Reservation, InstanceAttribute from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_internet_gateways.py b/tests/test_ec2/test_internet_gateways.py index 6ece0063d..4c841e157 100644 --- a/tests/test_ec2/test_internet_gateways.py +++ b/tests/test_ec2/test_internet_gateways.py @@ -1,10 +1,13 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import re import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_key_pairs.py b/tests/test_ec2/test_key_pairs.py index 5828529d1..3319b82ff 100644 --- a/tests/test_ec2/test_key_pairs.py +++ b/tests/test_ec2/test_key_pairs.py @@ -1,6 +1,9 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto import sure # noqa -from nose.tools import assert_raises from boto.exception import EC2ResponseError from moto import mock_ec2 diff --git a/tests/test_ec2/test_security_groups.py b/tests/test_ec2/test_security_groups.py index 562f9f72b..6b5f4033d 100644 --- a/tests/test_ec2/test_security_groups.py +++ b/tests/test_ec2/test_security_groups.py @@ -1,7 +1,10 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_subnets.py b/tests/test_ec2/test_subnets.py index 777b7dfae..cfa7cd65e 100644 --- a/tests/test_ec2/test_subnets.py +++ b/tests/test_ec2/test_subnets.py @@ -1,7 +1,10 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 diff --git a/tests/test_ec2/test_vpc_peering.py b/tests/test_ec2/test_vpc_peering.py index 1088e8aaf..422174e88 100644 --- a/tests/test_ec2/test_vpc_peering.py +++ b/tests/test_ec2/test_vpc_peering.py @@ -1,7 +1,10 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2 from tests.helpers import requires_boto_gte diff --git a/tests/test_ec2/test_vpcs.py b/tests/test_ec2/test_vpcs.py index 29566f499..952dae34d 100644 --- a/tests/test_ec2/test_vpcs.py +++ b/tests/test_ec2/test_vpcs.py @@ -1,7 +1,10 @@ +# Ensure 'assert_raises' context manager support for Python 2.6 +import tests.backport_assert_raises +from nose.tools import assert_raises + import boto from boto.exception import EC2ResponseError import sure # noqa -from nose.tools import assert_raises from moto import mock_ec2