From 4df4f7eec84eff7bdaebc651640eb7e95a3b1b77 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Mon, 12 Nov 2012 11:49:42 +0800 Subject: [PATCH] Adds tests for attributes.is_attr_set ATTR_NOT_SPECIFIED and None are singleton objects When conducting "is in" against a list of objects, they are compared using unequal unless they are the same object. http://docs.python.org/2/reference/expressions.html#in According to PEP8, they should be compared by their id not value Therefore the is_attr_set logic has changed to: return not (attribute is None or attribute is ATTR_NOT_SPECIFIED) Wrote unittests for is_attr_set Change-Id: I2ebdd29564e5627b9e33b13a744948e8c9c0b9ce --- quantum/api/v2/attributes.py | 2 +- quantum/tests/unit/test_attributes.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/quantum/api/v2/attributes.py b/quantum/api/v2/attributes.py index 77e8e4692..9da37f4f8 100644 --- a/quantum/api/v2/attributes.py +++ b/quantum/api/v2/attributes.py @@ -29,7 +29,7 @@ LOG = logging.getLogger(__name__) def is_attr_set(attribute): - return attribute not in (None, ATTR_NOT_SPECIFIED) + return not (attribute is None or attribute is ATTR_NOT_SPECIFIED) def _validate_boolean(data, valid_values=None): diff --git a/quantum/tests/unit/test_attributes.py b/quantum/tests/unit/test_attributes.py index d73aeaa2e..b6f20c35a 100644 --- a/quantum/tests/unit/test_attributes.py +++ b/quantum/tests/unit/test_attributes.py @@ -23,6 +23,14 @@ from quantum.common import exceptions as q_exc class TestAttributes(unittest2.TestCase): + def test_is_attr_set(self): + mock_obj = attributes.ATTR_NOT_SPECIFIED + mock_none = None + mock_str = "I'm set" + self.assertIs(attributes.is_attr_set(mock_obj), False) + self.assertIs(attributes.is_attr_set(mock_none), False) + self.assertIs(attributes.is_attr_set(mock_str), True) + def test_booleans(self): msg = attributes._validate_boolean(True) self.assertIsNone(msg) -- 2.45.2