kenken999's picture
tes
164aab4
raw
history blame
1.89 kB
# Import necessary modules
import dataclasses
import pytest
# Define a dataclass for a simple BankAccount
@dataclasses.dataclass
class BankAccount:
"""Represents a simple bank account"""
account_number: int
account_holder: str
balance: float = 0.0
def deposit(self, amount: float) -> None:
"""Deposit money into the account"""
self.balance += amount
def withdraw(self, amount: float) -> None:
"""Withdraw money from the account"""
if amount > self.balance:
raise ValueError("Insufficient balance")
self.balance -= amount
def get_balance(self) -> float:
"""Get the current balance of the account"""
return self.balance
# Define a function to create a new BankAccount
def create_account(account_number: int, account_holder: str) -> BankAccount:
"""Create a new BankAccount instance"""
return BankAccount(account_number, account_holder)
# Define a function to perform a transaction
def perform_transaction(account: BankAccount, amount: float, is_deposit: bool) -> None:
"""Perform a transaction on the account"""
if is_deposit:
account.deposit(amount)
else:
account.withdraw(amount)
# Define a test function using pytest
def test_bank_account():
"""Test the BankAccount class"""
account = create_account(12345, "John Doe")
assert account.get_balance() == 0.0
perform_transaction(account, 100.0, True)
assert account.get_balance() == 100.0
perform_transaction(account, 50.0, False)
assert account.get_balance() == 50.0
# Run the test
pytest.main([__file__])
# Create a new BankAccount instance
account = create_account(12345, "John Doe")
# Perform some transactions
perform_transaction(account, 100.0, True)
perform_transaction(account, 50.0, False)
# Print the final balance
print("Final balance:", account.get_balance())