コード例 #1
0
ファイル: banklite.py プロジェクト: mharrisb1/banklite
    def __init__(self, config: Optional[str] = None):
        create_db()
        fill_db()

        # parse config if exists
        try:
            _config_path = Path(config)
            with open(_config_path) as file:
                _config = yaml.safe_load(file)["config"]
        except TypeError:
            LOGGER.INFO("No config file specified. Falling back to default")
            _config = {}
        except FileNotFoundError:
            LOGGER.INFO("Config file not found. Falling back to default")
            _config = {}

        self.name = _config.get("name", "BankLite")  # defaults to BankLite
        self.currency = _config.get("currency", "USD")  # defaults to USD
        self.initial_reserve = _config.get("initial_reserve", 1e9)  # defaults to 1 billion

        self.conn = sqlite3.connect("banklite.db", check_same_thread=False)

        # main services
        self.ledger_service = LedgerService(conn=self.conn)
        self.account_service = AccountService(conn=self.conn)
        self.customer_service = CustomerService(conn=self.conn)

        # create reserve account
        self.reserve_account = self.account_service.create_reserve_account()

        # fill reserve with initial deposit
        self.ledger_service.record_entry(
            account=self.reserve_account, amount=self.initial_reserve
        )
コード例 #2
0
def test_open_checking_account(database_setup):
    conn = database_setup
    customer_service = CustomerService(conn)

    customer = customer_service.create_retail_customer(11111, "555-555-5555",
                                                       "*****@*****.**",
                                                       "111-11-1111", "John",
                                                       "Doe")

    acct = customer_service.open_checking_account(
        customer_id=customer.customer_id, deposit_amount=2000.0)

    accounts = customer_service.list_accounts(customer_id=customer.customer_id)

    assert len(accounts) == 1
    assert accounts[0] == acct
コード例 #3
0
def test_commercial_customer_creation(database_setup):
    conn = database_setup
    customer_service = CustomerService(conn)

    customer_service.create_commercial_customer(11111, "555-555-5555",
                                                "*****@*****.**",
                                                "11-1111111", "Acme, Inc")

    _, _, ein, _, first_name, _, czip, phone, email = conn.execute(
        "SELECT * FROM customers").fetchall()[0]

    assert ein == "11-1111111"
    assert first_name == "Acme, Inc"
    assert czip == 11111
    assert phone == "555-555-5555"
    assert email == "*****@*****.**"
コード例 #4
0
def test_retail_customer_creation(database_setup):
    conn = database_setup
    customer_service = CustomerService(conn)

    customer_service.create_retail_customer(11111, "555-555-5555",
                                            "*****@*****.**", "111-11-1111",
                                            "John", "Doe")

    _, _, _, ssn, first_name, last_name, czip, phone, email = conn.execute(
        "SELECT * FROM customers").fetchall()[0]

    assert ssn == "111-11-1111"
    assert first_name == "John"
    assert last_name == "Doe"
    assert czip == 11111
    assert phone == "555-555-5555"
    assert email == "*****@*****.**"
コード例 #5
0
def test_get_customer_by_id(database_setup):
    conn = database_setup
    customer_service = CustomerService(conn)

    conn.execute("""
        INSERT INTO customers
        VALUES (1111111111, DATETIME('now'), NULL, NULL,
        'Tester', NULL, 11111, '555-555-5555', '*****@*****.**')
        """)
    conn.commit()

    customer = customer_service.get_customer_by_id("1111111111")

    assert customer.customer_id == "1111111111"
    assert customer.first_name == "Tester"
    assert customer.zip == 11111
    assert customer.phone == "555-555-5555"
    assert customer.email == "*****@*****.**"
コード例 #6
0
def test_list_accounts(database_setup):
    conn = database_setup
    customer_service = CustomerService(conn)
    account_service = AccountService(conn)

    customer = customer_service.create_retail_customer(11111, "555-555-5555",
                                                       "*****@*****.**",
                                                       "111-11-1111", "John",
                                                       "Doe")

    customer_id = customer.customer_id

    account_service.create_checking_account(customer)
    account_service.create_checking_account(customer)
    account_service.create_checking_account(customer)

    accounts = customer_service.list_accounts(customer_id)

    assert len(accounts) == 3
コード例 #7
0
ファイル: banklite.py プロジェクト: mharrisb1/banklite
class BankLite:
    """
    BankLite controller object
    """

    def __init__(self, config: Optional[str] = None):
        create_db()
        fill_db()

        # parse config if exists
        try:
            _config_path = Path(config)
            with open(_config_path) as file:
                _config = yaml.safe_load(file)["config"]
        except TypeError:
            LOGGER.INFO("No config file specified. Falling back to default")
            _config = {}
        except FileNotFoundError:
            LOGGER.INFO("Config file not found. Falling back to default")
            _config = {}

        self.name = _config.get("name", "BankLite")  # defaults to BankLite
        self.currency = _config.get("currency", "USD")  # defaults to USD
        self.initial_reserve = _config.get("initial_reserve", 1e9)  # defaults to 1 billion

        self.conn = sqlite3.connect("banklite.db", check_same_thread=False)

        # main services
        self.ledger_service = LedgerService(conn=self.conn)
        self.account_service = AccountService(conn=self.conn)
        self.customer_service = CustomerService(conn=self.conn)

        # create reserve account
        self.reserve_account = self.account_service.create_reserve_account()

        # fill reserve with initial deposit
        self.ledger_service.record_entry(
            account=self.reserve_account, amount=self.initial_reserve
        )

    def __repr__(self):
        return f"BankLite(name={self.name!r})"

    def create_new_customer(
        self, customer_type: str, **kwargs
    ) -> Union[BaseCustomer, RetailCustomer, CommercialCustomer, None]:
        """
        Factory object creator for customer objects

        :param customer_type: Either `retail` or `commercial`
        :return: Customer object
        """
        if customer_type == "retail":
            customer = self.customer_service.create_retail_customer(**kwargs)
        elif customer_type == "commercial":
            customer = self.customer_service.create_commercial_customer(**kwargs)
        else:
            LOGGER.DEBUG("Invalid customer type: %s", customer_type)
            return None

        return customer

    def open_account(
        self,
        acct_type: str,
        customer_id: str,
        deposit_amount: float,
        savings_rate: Optional[float] = 0.0,
    ) -> Union[BaseAccount, None]:
        """
        Factory object creator for account objects

        :param acct_type: Either `checking` or `savings`
        :param customer_id: ID of customer opening account
        :param deposit_amount: Amount to deposit (must be positive value)
        :param savings_rate: If the account is a savings account, a savings
                             rate must be passed in
        :return: Account object
        """
        if acct_type == "checking":
            account = self.customer_service.open_checking_account(customer_id, deposit_amount)
        elif acct_type == "savings":
            account = self.customer_service.open_savings_account(
                customer_id, deposit_amount, savings_rate
            )
        else:
            LOGGER.DEBUG("Invalid account type: %s", acct_type)
            return None

        return account

    def deposit(self, account_id: str, amount: float) -> Union[float, None]:
        """
        Deposit amount into an account

        :param account_id: ID of account
        :param amount: Amount to deposit
        :return: Resulting account balance
        """
        account = self.account_service.get_account_by_id(account_id)

        if account and amount > 0:
            self.ledger_service.record_entry(account, amount)
            return self.account_service.get_account_balance(account.account_id)
        elif amount <= 0:
            return self.account_service.get_account_balance(account.account_id)
        else:
            return None

    def withdraw(self, account_id: str, amount: float) -> Union[float, None]:
        """
        Withdraw amount from account

        :param account_id: ID of account
        :param amount: Amount to withdraw
        :return: Resulting account balance
        """
        account = self.account_service.get_account_by_id(account_id)

        if amount > self.account_service.get_account_balance(account_id):
            return None

        if account and amount < 0:
            self.ledger_service.record_entry(account, amount)
            return self.account_service.get_account_balance(account.account_id)
        elif amount >= 0:
            return self.account_service.get_account_balance(account.account_id)
        else:
            return None