Implemented __eq__ and __hash__ for the database primitives

This commit is contained in:
s3lph 2018-11-03 19:20:30 +01:00
parent ea57485cfb
commit b8cbb265b8
4 changed files with 77 additions and 0 deletions

View file

@ -17,3 +17,15 @@ class Product:
self.price_member: int = price_member
self.price_non_member: int = price_non_member
self.stock: int = stock
def __eq__(self, other) -> bool:
if not isinstance(other, Product):
return False
return self.id == other.id and \
self.name == other.name and \
self.price_member == other.price_member and \
self.price_non_member == other.price_non_member and \
self.stock == other.stock
def __hash__(self) -> int:
return hash((self.id, self.name, self.price_member, self.price_non_member, self.stock))

View file

@ -28,3 +28,15 @@ class Receipt:
self.user: User = user
self.from_date: datetime = from_date
self.to_date: datetime = to_date
def __eq__(self, other) -> bool:
if not isinstance(other, Receipt):
return False
return self.id == other.id and \
self.transactions == other.transactions and \
self.user == other.user and \
self.from_date == other.from_date and \
self.to_date == other.to_date
def __hash__(self) -> int:
return hash((self.id, self.transactions, self.user, self.from_date, self.to_date))

View file

@ -45,6 +45,18 @@ class Transaction:
def receipt_message(self) -> Optional[str]:
return None
def __eq__(self, other) -> bool:
if not isinstance(other, Transaction):
return False
return self.id == other.id and \
self.user == other.user and \
self.value == other.value and \
self.old_balance == other.old_balance and \
self.date == other.date
def __hash__(self) -> int:
return hash((self.id, self.user, self.value, self.old_balance, self.date))
class Consumption(Transaction):
"""
@ -66,6 +78,15 @@ class Consumption(Transaction):
def receipt_description(self) -> str:
return self.product
def __eq__(self, other) -> bool:
if not isinstance(other, Consumption):
return False
return super().__eq__(other) and \
self.product == other.product
def __hash__(self) -> int:
return hash((super().__hash__(), self.product))
class Deposit(Transaction):
"""
@ -85,6 +106,14 @@ class Deposit(Transaction):
def receipt_description(self) -> str:
return 'Deposit'
def __eq__(self, other) -> bool:
if not isinstance(other, Deposit):
return False
return super().__eq__(other)
def __hash__(self) -> int:
return super().__hash__()
class Modification(Transaction):
"""
@ -122,3 +151,13 @@ class Modification(Transaction):
return None
else:
return f'Reason: «{self.reason}»'
def __eq__(self, other) -> bool:
if not isinstance(other, Modification):
return False
return super().__eq__(other) and \
self.agent == other.agent and \
self.reason == other.reason
def __hash__(self) -> int:
return hash((super().__hash__(), self.agent, self.reason))

View file

@ -34,3 +34,17 @@ class User:
self.is_admin: bool = is_admin
self.is_member: bool = is_member
self.receipt_pref: ReceiptPreference = receipt_pref
def __eq__(self, other) -> bool:
if not isinstance(other, User):
return False
return self.id == other.id and \
self.name == other.name and \
self.balance == other.balance and \
self.email == other.email and \
self.is_admin == other.is_admin and \
self.is_member == other.is_member and \
self.receipt_pref == other.receipt_pref
def __hash__(self) -> int:
return hash((self.id, self.name, self.balance, self.email, self.is_admin, self.is_member, self.receipt_pref))