def test_add_transaction_multiple(self): dao = self.account_repo.get_account(1) acc: Account = PersistentAccount.from_dao(dao, self.account_repo, self.trans_repo) t_dao1 = TransactionDAO(None, None, None, '255.255', '2021-07-04 10:20:25.123', 'PLN', '1') t_dao2 = TransactionDAO(None, None, None, '255.255', '2021-07-04 10:20:25.123', 'USD', '3.73') acc.add_transaction(t_dao1) acc.add_transaction(t_dao2) t_dao1 = self.trans_repo.get_transaction(1) t_dao2 = self.trans_repo.get_transaction(2) self.assertEqual(t_dao1.account_id, 1) self.assertEqual(t_dao2.account_id, 1) self.assertEqual(acc.balance, (Decimal('255.26') * Decimal('3.73') + Decimal('255.26')).quantize(Decimal(10)**-2)) self.assertEqual(len(acc.transactions), 2) trans1_from_acc = acc.transactions[0] trans2_from_acc = acc.transactions[1] trans1_from_db = Transaction.from_dao(t_dao1) trans2_from_db = Transaction.from_dao(t_dao2) self.assertEqual(trans1_from_acc.id, trans1_from_db.id) self.assertEqual(trans2_from_acc.id, trans2_from_db.id)
def change_balance(self, amount: Decimal): trans = Transaction(-1, amount, datetime.today(), self.currency, Decimal(1), note="Balance change") self.add_transaction(trans.to_dao())
def add_transaction( self, dao: TransactionDAO, trans_builder: Callable = Transaction.from_dao) -> Transaction: Transaction.validate_dao(dao) trans = trans_builder(dao) trans.account = self self._transactions.append(trans) self.balance = self.balance + trans.converted_value return trans
def transfer(self, to: 'Account', amount: Decimal, rate: Decimal = Decimal(1)): date = datetime.today() t1 = Transaction(-1, -amount, date, self.currency, Decimal('1'), note="Funds transfer", acc=self) t2 = Transaction(-1, amount, date, self.currency, rate, note="Funds transfer", acc=to) t1 = self.add_transaction(t1.to_dao()) t2 = to.add_transaction(t2.to_dao()) t2.inverse_transaction = t1 t1.inverse_transaction = t2
def test_change_balance(self): dao = self.account_repo.get_account(1) acc: Account = PersistentAccount.from_dao(dao, self.account_repo, self.trans_repo) acc.change_balance(Decimal('500')) t_dao = self.trans_repo.get_transaction(1) self.assertEqual(t_dao.account_id, 1) self.assertEqual(acc.balance, Decimal('500')) self.assertEqual(len(acc.transactions), 1) trans_from_acc = acc.transactions[0] trans_from_db = Transaction.from_dao(t_dao) self.assertEqual(trans_from_acc.id, trans_from_db.id)
def test_add_transaction_same_curr(self): dao = self.account_repo.get_account(1) acc: Account = PersistentAccount.from_dao(dao, self.account_repo, self.trans_repo) t_dao = TransactionDAO(None, None, None, '255.255', '2021-07-04 10:20:25.123', 'PLN', '1') acc.add_transaction(t_dao) t_dao = self.trans_repo.get_transaction(1) self.assertEqual(t_dao.account_id, 1) self.assertEqual(acc.balance, Decimal('255.26')) self.assertEqual(len(acc.transactions), 1) trans_from_acc = acc.transactions[0] trans_from_db = Transaction.from_dao(t_dao) self.assertEqual(trans_from_acc.id, trans_from_db.id)
def test_add_transaction_different_curr(self): dao = self.account_repo.get_account(1) acc: Account = PersistentAccount.from_dao(dao, self.account_repo, self.trans_repo) t_dao = TransactionDAO(None, None, None, '255.255', '2021-07-04 10:20:25.123', 'USD', '3.73') acc.add_transaction(t_dao) t_dao = self.trans_repo.get_transaction(1) self.assertEqual(t_dao.account_id, 1) self.assertEqual(acc.balance, (Decimal('255.26') * Decimal('3.73')).quantize( Decimal(10)**-2)) self.assertEqual(len(acc.transactions), 1) trans_from_acc = acc.transactions[0] trans_from_db = Transaction.from_dao(t_dao) self.assertEqual(trans_from_acc.id, trans_from_db.id)
def test_remove_transaction(self): dao = self.account_repo.get_account(1) acc: Account = PersistentAccount.from_dao(dao, self.account_repo, self.trans_repo) t_dao1 = TransactionDAO(None, None, None, '120', '2021-07-04 10:20:25.123', 'PLN', '1') t_dao2 = TransactionDAO(None, None, None, '350', '2021-07-04 10:20:25.123', 'USD', '3.73') acc.add_transaction(t_dao1) acc.add_transaction(t_dao2) acc.remove_transaction(1) self.assertIsNone(self.trans_repo.get_transaction(1)) t_dao = self.trans_repo.get_transaction(2) self.assertEqual(t_dao.account_id, 1) self.assertEqual(acc.balance, Decimal('350') * Decimal('3.73')) self.assertEqual(len(acc.transactions), 1) trans_from_acc = acc.transactions[0] trans_from_db = Transaction.from_dao(t_dao) self.assertEqual(trans_from_acc.id, trans_from_db.id)
def init_sandbox() -> SandboxData: return SandboxData(virtual_start, virtual_start, 1000, []) def add_to_virtual_start(value: int) -> str: return str(int(virtual_start) + value) def test_step(): sandbox_data = init_sandbox() sandbox_data.virtual_current = add_to_virtual_start(100) assert(sandbox_data.virtual_current == '345427300') transactions = [ {"ticker": "AAPL", "transaction": Transaction(3, 200, 1.67, virtual_start, OperationType.SELL)}, {"ticker": "AAPL", "transaction": Transaction(2, 249, 2, add_to_virtual_start(100), OperationType.BUY)}, {"ticker": "AAPL", "transaction": Transaction(1000, 315, 3, add_to_virtual_start(200), OperationType.BUY)}, {"ticker": "AAPL", "transaction": Transaction(2, 315, 3, add_to_virtual_start(200), OperationType.SELL)} ] def test_transactions(): sandbox_data = init_sandbox() # sell something that you don't have res = sandbox_data.apply_transaction(**transactions[0]) assert res == False # apply transaction that will happen in the future res = sandbox_data.apply_transaction(**transactions[1])