vpp_papi: MACAddress equals fails in unittest. 26/16626/4
authorPaul Vinciguerra <pvinci@vinciconsulting.com>
Wed, 26 Dec 2018 23:27:10 +0000 (15:27 -0800)
committerOle Trøan <otroan@employees.org>
Fri, 28 Dec 2018 20:40:14 +0000 (20:40 +0000)
Before:
    EqualsAssertionError:  :: MACAddress(11:22:33:44:55:66) != MACAddress(11:22:33:44:55:66)
    ----------------------------------------------------------------------
    Ran 1 test in 0.002s
    FAILED (failures=1)
    MACAddress(11:22:33:44:55:66) != MACAddress(11:22:33:44:55:66)

After:

    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    OK

Change-Id: I89896a823b8f8a861813dabf23e7c9207e4fabb6
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
src/vpp-api/python/vpp_papi/macaddress.py
src/vpp-api/python/vpp_papi/tests/test_macaddress.py [new file with mode: 0644]

index a100381..5005fa8 100644 (file)
@@ -52,3 +52,14 @@ class MACAddress():
 
     def __repr__(self):
         return '%s(%s)' % (self.__class__.__name__, self.mac_string)
+
+    def __eq__(self, other):
+        if not isinstance(other, MACAddress):
+            return NotImplemented
+        return self.mac_binary == other.mac_binary
+
+    def __ne__(self, other):
+        return not self == other
+
+    def __hash__(self):
+        return hash(self.mac_binary)
diff --git a/src/vpp-api/python/vpp_papi/tests/test_macaddress.py b/src/vpp-api/python/vpp_papi/tests/test_macaddress.py
new file mode 100644 (file)
index 0000000..08e365a
--- /dev/null
@@ -0,0 +1,10 @@
+import unittest
+from vpp_papi import MACAddress
+
+
+class TestMacAddress(unittest.TestCase):
+
+    def test_eq(self):
+        mac = '11:22:33:44:55:66'
+        self.assertEqual(MACAddress(mac),
+                         MACAddress(mac))