コード例 #1
0
ファイル: test.py プロジェクト: neskoc/oopython
class TestBank(unittest.TestCase):
    """ Submodule for unittests, derives from unittest.TestCase. """
    src = "static/data/test_data_orig.json"
    dst = "static/data/test_data.json"

    def setUp(self):
        """ Create object for all tests """
        # Arrange
        copyfile(TestBank.src, TestBank.dst)
        self.bank = AccountManager(TestBank.dst)

    def tearDown(self):
        """ Remove dependencies after test """
        copyfile(TestBank.src, TestBank.dst)
        self.bank = None

    def test_bank_transfer_to_account(self):
        """ Test transactions (Account). """
        imd = ImmutableMultiDict([('from_account', '1'), ('to_account', '2'),
                                  ('amount', '100')])
        self.bank.transfer(imd)
        src_account = self.bank.accounts[0]
        dst_account = self.bank.accounts[1]
        self.assertEqual(src_account.balance, 900.0)
        self.assertEqual(dst_account.balance, 1100 - 1)

    def test_bank_transfer_to_savingsaccount(self):
        """ Test transactions (SavingsAccount). """
        imd = ImmutableMultiDict([('from_account', '2'), ('to_account', '1'),
                                  ('amount', '100')])
        self.bank.transfer(imd)
        dst_account = self.bank.accounts[0]
        self.assertEqual(dst_account.balance, 1100 - 1.3)

    def test_calculate_interest_rate_account(self):
        """ Test calculation of interest rate. """
        test_account = SavingsAccount(1000, [])

        today = date.today()
        test_date = "22-09-23"
        diff = (date(22, 9, 23) - today).days
        interest_rate1 = diff * test_account.calculate_daily_interest_rate()
        interset_rate2 = AccountManager.calculate_interest_rate(
            test_account, test_date)
        self.assertEqual(interest_rate1, interset_rate2)

    def test_add_customer_true_and_attributes(self):
        """ Test add customer (true) and whether added attributes are correct """
        new_person = Person("Nenad", "nen")
        res = self.bank.add_customer(new_person)
        self.assertTrue(res)
        self.assertEqual(self.bank.customers[-1].name, "Nenad")
        self.assertEqual(self.bank.customers[-1].id_, "nen")

    def test_add_customer_false(self):
        """ Test add customer with existing id (false). """
        new_person = Person("Martin", "and")
        res = self.bank.add_customer(new_person)
        self.assertFalse(res)

    def test_add_account(self):
        """ Test add account. """
        new_account = Account(5000, [])
        self.bank.add_account(new_account)
        self.assertEqual(new_account, self.bank.accounts[-1])

    def test_connect_true_and_customer_id(self):
        """ Test connect customer to account (true) and if correct customer id. """
        imd = ImmutableMultiDict([('account', '2'), ('person', 'and')])
        self.assertTrue(self.bank.connect(imd))
        self.assertTrue("and" in self.bank.accounts[1].holders)
        self.assertIn("and", self.bank.accounts[1].holders)

    def test_connect_false(self):
        """ Test connect existing customer to account as holder (false). """
        imd = ImmutableMultiDict([('account', '2'), ('person', 'mar')])
        self.assertFalse(self.bank.connect(imd))
        self.assertTrue("mar" in self.bank.accounts[1].holders)
        self.assertIn("mar", self.bank.accounts[1].holders)
コード例 #2
0
class TestBank(unittest.TestCase):
    "Initiation of Test class"

    def setUp(self):
        """ creates object for all tests"""
        Account.account_number = 1
        self.bank = AccountManager()

    def tearDown(self):
        "Removes dependicies after every test"
        self.bank = None

    def test_init_accounts(self):
        " test init init methods from both account types"
        account_obj = self.bank.accounts[0]
        saving_obj = self.bank.accounts[3]
        self.assertEqual(account_obj.balance, 9499.0)
        self.assertEqual(account_obj.type_, 'Account')
        self.assertEqual(account_obj.id_, 1)
        self.assertEqual(account_obj.holders, [])

        self.assertEqual(saving_obj.balance, 10990.0)
        self.assertEqual(saving_obj.type_, 'SavingsAccount')
        self.assertEqual(saving_obj.id_, 4)
        self.assertEqual(saving_obj.holders, [])

    def test_get_account_id_notfound(self):
        "Test that bank returns correct string when accountID not exist"
        self.assertEqual(self.bank.get_account_by_id(10),
                         "konto med detta id finns inte")

    def test_get_account_id_found(self):
        "Test bank returns correct account object when accountID exist"
        self.assertEqual(self.bank.get_account_by_id(1), self.bank.accounts[0])

    def test_transaction_account(self):
        "Test transaction Account"
        input_dic = {'from_account': '1', 'to_account': '2', 'amount': '1000'}
        self.assertEqual(self.bank.accounts[0].balance, 9499.0)
        self.assertEqual(self.bank.accounts[1].balance, 6698.0)

        self.bank.transfer(input_dic)
        self.assertEqual(self.bank.accounts[0].balance, 8499.0)
        self.assertEqual(self.bank.accounts[1].balance, 7688.0)

    def test_transaction_savingccount(self):
        "test transaction SavingsAccount"
        input_dic = {'from_account': '4', 'to_account': '5', 'amount': '1000'}
        self.assertEqual(self.bank.accounts[3].balance, 10990.0)
        self.assertEqual(self.bank.accounts[4].balance, 11800.0)

        self.bank.transfer(input_dic)
        self.assertEqual(self.bank.accounts[3].balance, 9990.0)
        self.assertEqual(self.bank.accounts[4].balance, 12787.0)

    def test_interest_cal(self):
        "test interest calculation between two days."
        string = '2021-02-17'
        acc = self.bank.get_account_by_id(4)

        self.assertEqual(self.bank.calculate_interest_rate(acc, string),
                         0.09032876712328768)

    def test_init_persons(self):
        "test init method of person class"
        person_obj = self.bank.persons[0]
        self.assertEqual(person_obj.name, 'theodor')
        self.assertEqual(person_obj.id_, 'ted')

    def test_get_person_id_notfound(self):
        "Test bank returns correct string when personId not found"
        self.assertEqual(self.bank.get_person_by_id('lena'),
                         "Detta id har ingen ägare")

    def test_get__id_found(self):
        "Test bank returns correct person object when personId found."
        self.assertEqual(self.bank.get_person_by_id('tedy'),
                         self.bank.persons[1])

    def test_person_id_false(self):
        "Test bank returns False when personId already exist"
        input_dic = {'name': 'oskar', 'id': 'ted'}
        self.assertFalse(self.bank.add_persons(input_dic))

    def test_person_id_true(self):
        "Test bank returns correct personId after adding the object."
        input_dic = {'name': 'oskar', 'id': 'osk'}
        self.bank.add_persons(input_dic)
        self.assertEqual(self.bank.get_person_by_id('osk'),
                         self.bank.persons[-1])

    def test_connect_false(self):
        "Test bank returns False when connection to id already exist to account"
        input_dic = {'person': 'bols', 'account': '3'}
        self.assertFalse(self.bank.connect_person_account(input_dic))

    def test_connect(self):
        "Test bank returns correct id after connection with account."
        input_dic = {'person': 'hum', 'account': '1'}
        self.bank.connect_person_account(input_dic)
        self.assertTrue(self.bank.get_person_by_id('hum'))