forked from s3lph/matemat
Implemented __eq__ and __hash__ for the database primitives
This commit is contained in:
parent
ea57485cfb
commit
b8cbb265b8
4 changed files with 77 additions and 0 deletions
|
@ -17,3 +17,15 @@ class Product:
|
||||||
self.price_member: int = price_member
|
self.price_member: int = price_member
|
||||||
self.price_non_member: int = price_non_member
|
self.price_non_member: int = price_non_member
|
||||||
self.stock: int = stock
|
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))
|
||||||
|
|
|
@ -28,3 +28,15 @@ class Receipt:
|
||||||
self.user: User = user
|
self.user: User = user
|
||||||
self.from_date: datetime = from_date
|
self.from_date: datetime = from_date
|
||||||
self.to_date: datetime = to_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))
|
||||||
|
|
|
@ -45,6 +45,18 @@ class Transaction:
|
||||||
def receipt_message(self) -> Optional[str]:
|
def receipt_message(self) -> Optional[str]:
|
||||||
return None
|
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):
|
class Consumption(Transaction):
|
||||||
"""
|
"""
|
||||||
|
@ -66,6 +78,15 @@ class Consumption(Transaction):
|
||||||
def receipt_description(self) -> str:
|
def receipt_description(self) -> str:
|
||||||
return self.product
|
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):
|
class Deposit(Transaction):
|
||||||
"""
|
"""
|
||||||
|
@ -85,6 +106,14 @@ class Deposit(Transaction):
|
||||||
def receipt_description(self) -> str:
|
def receipt_description(self) -> str:
|
||||||
return 'Deposit'
|
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):
|
class Modification(Transaction):
|
||||||
"""
|
"""
|
||||||
|
@ -122,3 +151,13 @@ class Modification(Transaction):
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return f'Reason: «{self.reason}»'
|
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))
|
||||||
|
|
|
@ -34,3 +34,17 @@ class User:
|
||||||
self.is_admin: bool = is_admin
|
self.is_admin: bool = is_admin
|
||||||
self.is_member: bool = is_member
|
self.is_member: bool = is_member
|
||||||
self.receipt_pref: ReceiptPreference = receipt_pref
|
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))
|
||||||
|
|
Loading…
Reference in a new issue