]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Adds tests for attributes.is_attr_set
authorZhongyue Luo <zhongyue.nah@intel.com>
Mon, 12 Nov 2012 03:49:42 +0000 (11:49 +0800)
committerZhongyue Luo <zhongyue.nah@intel.com>
Mon, 12 Nov 2012 05:09:41 +0000 (13:09 +0800)
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
quantum/tests/unit/test_attributes.py

index 77e8e4692ea2871ab665cba2ac44066c2bcc5773..9da37f4f8ed795939a08a61d5411eaf670f0b5ea 100644 (file)
@@ -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):
index d73aeaa2e39d748148dd57b8095916bcbba5688e..b6f20c35a2c2881e62c6c4ac843acad211cf029a 100644 (file)
@@ -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)