[api] refactoring: implementing __eq__ and __hash__ for PackageUpdate

This commit is contained in:
Vinicius Moreira
2022-03-04 11:58:47 -03:00
parent 821fd730a0
commit e737df8e8c
4 changed files with 29 additions and 1 deletions

View File

@@ -263,7 +263,17 @@ class PackageUpdate:
self.type = pkg_type
def __str__(self):
return '{} (id={}, name={}, version={}, type={})'.format(self.__class__.__name__, self.id, self.name, self.version, self.type)
attrs = ', '.join(f'{p}={v}' for p, v in sorted(self.__dict__.items()))
return f'{self.__class__.__name__} ({attrs})'
def __eq__(self, other):
if isinstance(other, PackageUpdate):
return self.__dict__ == other.__dict__
return False
def __hash__(self):
return sum(hash(v) for v in self.__dict__.values())
class PackageHistory:

0
tests/api/__init__.py Normal file
View File

View File

View File

@@ -0,0 +1,18 @@
from unittest import TestCase
from bauh.api.abstract.model import PackageUpdate
class PackageUpdateTest(TestCase):
def test__hash__return_same_hash_for_equal_packages(self):
a = PackageUpdate(pkg_id='a', name='b', version='c', pkg_type='d')
b = PackageUpdate(pkg_id='a', name='b', version='c', pkg_type='d')
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))
def test__hash__return_different_hash_for_not_equal_packages(self):
a = PackageUpdate(pkg_id='a', name='b', version='c', pkg_type='d')
b = PackageUpdate(pkg_id='a', name='b', version='c', pkg_type='e')
self.assertNotEqual(a, b)
self.assertNotEqual(hash(a), hash(b))