From b8cbb265b8ee4153ac2af9322618bdece2502d3f Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 3 Nov 2018 19:20:30 +0100 Subject: [PATCH] Implemented __eq__ and __hash__ for the database primitives --- matemat/db/primitives/Product.py | 12 +++++++++ matemat/db/primitives/Receipt.py | 12 +++++++++ matemat/db/primitives/Transaction.py | 39 ++++++++++++++++++++++++++++ matemat/db/primitives/User.py | 14 ++++++++++ 4 files changed, 77 insertions(+) diff --git a/matemat/db/primitives/Product.py b/matemat/db/primitives/Product.py index c543630..ed196d4 100644 --- a/matemat/db/primitives/Product.py +++ b/matemat/db/primitives/Product.py @@ -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)) diff --git a/matemat/db/primitives/Receipt.py b/matemat/db/primitives/Receipt.py index 29f5d4b..f11320a 100644 --- a/matemat/db/primitives/Receipt.py +++ b/matemat/db/primitives/Receipt.py @@ -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)) diff --git a/matemat/db/primitives/Transaction.py b/matemat/db/primitives/Transaction.py index c2777ea..061f65f 100644 --- a/matemat/db/primitives/Transaction.py +++ b/matemat/db/primitives/Transaction.py @@ -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)) diff --git a/matemat/db/primitives/User.py b/matemat/db/primitives/User.py index 01f8032..ce203d7 100644 --- a/matemat/db/primitives/User.py +++ b/matemat/db/primitives/User.py @@ -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))