__ne__() unit tests & have special methods use (self, other)

Commit 41c0413adb added the __ne__()
method. This adds unit tests for the method.

Add some additional unit tests for the special methods.

Use the standard way[1] of defining the special methods __eq__() and
__gt__() with (self, other) as the arguments

https://docs.python.org/2/reference/datamodel.html#special-method-names

Change-Id: I947e7a453e1cb621f27a0bb3576b0544868734fb
This commit is contained in:
John L. Villalovos 2016-09-08 14:44:26 -07:00
parent 41c0413adb
commit aeaf273c07
2 changed files with 24 additions and 4 deletions

View File

@ -105,11 +105,11 @@ class Version(object):
"Invalid value for %s header") % Version.string)
return version
def __gt__(a, b):
return (a.major, a.minor) > (b.major, b.minor)
def __gt__(self, other):
return (self.major, self.minor) > (other.major, other.minor)
def __eq__(a, b):
return (a.major, a.minor) == (b.major, b.minor)
def __eq__(self, other):
return (self.major, self.minor) == (other.major, other.minor)
def __ne__(self, other):
return not self.__eq__(other)

View File

@ -97,8 +97,24 @@ class TestVersion(base.BaseApiTest):
{cbase.Version.string: '123.456'}, mock.ANY, mock.ANY)
ver_2 = cbase.Version(
{cbase.Version.string: '123.456'}, mock.ANY, mock.ANY)
ver_3 = cbase.Version(
{cbase.Version.string: '654.321'}, mock.ANY, mock.ANY)
self.assertTrue(hasattr(ver_1, '__eq__'))
self.assertEqual(ver_1, ver_2)
# Force __eq__ to be called and return False
self.assertFalse(ver_1 == ver_3)
def test_not_equals(self):
ver_1 = cbase.Version(
{cbase.Version.string: '123.456'}, mock.ANY, mock.ANY)
ver_2 = cbase.Version(
{cbase.Version.string: '123.456'}, mock.ANY, mock.ANY)
ver_3 = cbase.Version(
{cbase.Version.string: '654.321'}, mock.ANY, mock.ANY)
self.assertTrue(hasattr(ver_1, '__ne__'))
self.assertNotEqual(ver_1, ver_3)
# Force __ne__ to be called and return False
self.assertFalse(ver_1 != ver_2)
def test_greaterthan(self):
ver_1 = cbase.Version(
@ -107,6 +123,8 @@ class TestVersion(base.BaseApiTest):
{cbase.Version.string: '123.456'}, mock.ANY, mock.ANY)
self.assertTrue(hasattr(ver_1, '__gt__'))
self.assertGreater(ver_1, ver_2)
# Force __gt__ to be called and return False
self.assertFalse(ver_2 > ver_1)
def test_lessthan(self):
# __lt__ is created by @functools.total_ordering, make sure it exists
@ -117,3 +135,5 @@ class TestVersion(base.BaseApiTest):
{cbase.Version.string: '123.457'}, mock.ANY, mock.ANY)
self.assertTrue(hasattr(ver_1, '__lt__'))
self.assertLess(ver_1, ver_2)
# Force __lt__ to be called and return False
self.assertFalse(ver_2 < ver_1)