From 24b3429028dd9c6907b9709d7431f4cd303dae9a Mon Sep 17 00:00:00 2001 From: Rohit Date: Mon, 28 Oct 2024 12:47:11 -0400 Subject: [PATCH 1/2] Add bank account class --- billing/accounts.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 billing/accounts.py diff --git a/billing/accounts.py b/billing/accounts.py new file mode 100644 index 0000000000..e539f2f126 --- /dev/null +++ b/billing/accounts.py @@ -0,0 +1,30 @@ +class BankAccount: + def __init__(self, owner, balance=0.0): + self.owner = owner + if balance < 0: + raise ValueError("Initial balance cannot be negative") + self.balance = balance + + def deposit(self, amount): + if amount <= 0: + raise ValueError("Deposit amount must be positive") + self.balance += amount + return self.balance + + def withdraw(self, amount): + if amount <= 0: + raise ValueError("Withdrawal amount must be positive") + if amount > self.balance: + raise ValueError("Insufficient funds") + self.balance -= amount + return self.balance + + def transfer(self, other_account, amount): + if not isinstance(other_account, BankAccount): + raise ValueError("Recipient must be a BankAccount instance") + self.withdraw(amount) + other_account.deposit(amount) + return self.balance, other_account.balance + + def get_balance(self): + return self.balance From 95a22ad3d4255b59ca63b28bd23e12412a9db65a Mon Sep 17 00:00:00 2001 From: "sentry-autofix[bot]" <157164994+sentry-autofix[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 11:36:29 -0400 Subject: [PATCH 2/2] Add Tests for PR#943 (#944) Co-authored-by: sentry-autofix[bot] <157164994+sentry-autofix[bot]@users.noreply.github.com> Co-authored-by: Rohit --- billing/tests/test_accounts.py | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 billing/tests/test_accounts.py diff --git a/billing/tests/test_accounts.py b/billing/tests/test_accounts.py new file mode 100644 index 0000000000..6960c5af93 --- /dev/null +++ b/billing/tests/test_accounts.py @@ -0,0 +1,61 @@ +import unittest + +from billing.accounts import BankAccount + + +class TestBankAccount(unittest.TestCase): + def setUp(self): + self.account = BankAccount("John Doe", 100.0) + + def test_init(self): + self.assertEqual(self.account.owner, "John Doe") + self.assertEqual(self.account.balance, 100.0) + with self.assertRaises(ValueError): + BankAccount("Jane Doe", -50.0) + + def test_deposit(self): + new_balance = self.account.deposit(50.0) + self.assertEqual(new_balance, 150.0) + self.assertEqual(self.account.balance, 150.0) + with self.assertRaises(ValueError): + self.account.deposit(-10.0) + with self.assertRaises(ValueError): + self.account.deposit(0) + + def test_withdraw(self): + new_balance = self.account.withdraw(50.0) + self.assertEqual(new_balance, 50.0) + self.assertEqual(self.account.balance, 50.0) + with self.assertRaises(ValueError): + self.account.withdraw(-10.0) + with self.assertRaises(ValueError): + self.account.withdraw(0) + with self.assertRaises(ValueError): + self.account.withdraw(1000.0) + + def test_transfer(self): + other_account = BankAccount("Jane Doe", 50.0) + balances = self.account.transfer(other_account, 30.0) + self.assertEqual(balances, (70.0, 80.0)) + self.assertEqual(self.account.balance, 70.0) + self.assertEqual(other_account.balance, 80.0) + + with self.assertRaises(ValueError): + self.account.transfer(other_account, -10.0) + with self.assertRaises(ValueError): + self.account.transfer(other_account, 0) + with self.assertRaises(ValueError): + self.account.transfer(other_account, 1000.0) + with self.assertRaises(ValueError): + self.account.transfer("not an account", 10.0) + + def test_get_balance(self): + self.assertEqual(self.account.get_balance(), 100.0) + self.account.deposit(50.0) + self.assertEqual(self.account.get_balance(), 150.0) + self.account.withdraw(30.0) + self.assertEqual(self.account.get_balance(), 120.0) + + +if __name__ == "__main__": + unittest.main()