Skip to content

Commit

Permalink
Rename TransactionLog class to TrHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
tacgomes committed Apr 11, 2024
1 parent 5bf3d9b commit 48c1247
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
20 changes: 10 additions & 10 deletions src/investir/investir.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .config import Config
from .parser.factory import ParserFactory
from .transaction import OrderType, TransferType
from .transactionlog import TransactionLog
from .trhistory import TrHistory

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -138,15 +138,15 @@ def main() -> None:

config = Config(strict=True)

trlog = TransactionLog()
tr_hist = TrHistory()

for csv_file in args.input_files:
if csv_parser := ParserFactory.create_parser(csv_file, config):
result = csv_parser.parse()
trlog.insert_orders(result.orders)
trlog.insert_dividends(result.dividends)
trlog.insert_transfers(result.transfers)
trlog.insert_interest(result.interest)
tr_hist.insert_orders(result.orders)
tr_hist.insert_dividends(result.dividends)
tr_hist.insert_transfers(result.transfers)
tr_hist.insert_interest(result.interest)
else:
logger.warning('Failed to find a parser for %s', csv_file)

Expand All @@ -162,10 +162,10 @@ def main() -> None:
filters.append(lambda tr: tr.tax_year() == args.tax_year)

if args.command == 'orders':
trlog.show_orders(filters)
tr_hist.show_orders(filters)
elif args.command == 'dividends':
trlog.show_dividends(filters)
tr_hist.show_dividends(filters)
elif args.command == 'transfers':
trlog.show_transfers(filters)
tr_hist.show_transfers(filters)
elif args.command == 'interest':
trlog.show_interest(filters)
tr_hist.show_interest(filters)
10 changes: 5 additions & 5 deletions src/investir/transactionlog.py → src/investir/trhistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
from .utils import multiple_filter


class TransactionLog:
class TrHistory:
def __init__(self) -> None:
self._orders: list[Order] = []
self._dividends: list[Dividend] = []
self._transfers: list[Transfer] = []
self._interest: list[Interest] = []

def insert_orders(self, orders: list[Order]) -> None:
self._orders = TransactionLog._insert(self._orders, orders)
self._orders = TrHistory._insert(self._orders, orders)

def insert_dividends(self, dividends: list[Dividend]) -> None:
self._dividends = TransactionLog._insert(self._dividends, dividends)
self._dividends = TrHistory._insert(self._dividends, dividends)

def insert_transfers(self, transfers: list[Transfer]) -> None:
self._transfers = TransactionLog._insert(self._transfers, transfers)
self._transfers = TrHistory._insert(self._transfers, transfers)

def insert_interest(self, interest: list[Interest]) -> None:
self._interest = TransactionLog._insert(self._interest, interest)
self._interest = TrHistory._insert(self._interest, interest)

def orders(self) -> list[Order]:
return self._orders[:]
Expand Down
76 changes: 38 additions & 38 deletions tests/test_transactionlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Dividend,
Transfer, TransferType,
Interest)
from investir.transactionlog import TransactionLog
from investir.trhistory import TrHistory


ORDER1 = Order(
Expand Down Expand Up @@ -59,96 +59,96 @@


def test_transactionlog_duplicates_are_removed():
trlog = TransactionLog()
tr_hist = TrHistory()

trlog.insert_orders([ORDER1, ORDER1])
trlog.insert_orders([ORDER1])
assert len(trlog.orders()) == 1
tr_hist.insert_orders([ORDER1, ORDER1])
tr_hist.insert_orders([ORDER1])
assert len(tr_hist.orders()) == 1

trlog.insert_dividends([DIVIDEND1, DIVIDEND1])
trlog.insert_dividends([DIVIDEND1])
assert len(trlog.dividends()) == 1
tr_hist.insert_dividends([DIVIDEND1, DIVIDEND1])
tr_hist.insert_dividends([DIVIDEND1])
assert len(tr_hist.dividends()) == 1

trlog.insert_transfers([TRANSFER1, TRANSFER1])
trlog.insert_transfers([TRANSFER1])
assert len(trlog.transfers()) == 1
tr_hist.insert_transfers([TRANSFER1, TRANSFER1])
tr_hist.insert_transfers([TRANSFER1])
assert len(tr_hist.transfers()) == 1

trlog.insert_interest([INTEREST1, INTEREST1])
trlog.insert_interest([INTEREST1])
assert len(trlog.interest()) == 1
tr_hist.insert_interest([INTEREST1, INTEREST1])
tr_hist.insert_interest([INTEREST1])
assert len(tr_hist.interest()) == 1


def test_transactionlog_transactions_are_sorted_by_timestamp():
trlog = TransactionLog()
trlog.insert_orders([ORDER2, ORDER1])
tr_hist = TrHistory()
tr_hist.insert_orders([ORDER2, ORDER1])

orders = trlog.orders()
orders = tr_hist.orders()
assert len(orders) == 2
assert orders[0].ticker == ORDER1.ticker
assert orders[1].ticker == ORDER2.ticker


def test_transactionlog_dividends_are_sorted_by_timestamp():
trlog = TransactionLog()
trlog.insert_dividends([DIVIDEND2, DIVIDEND1])
tr_hist = TrHistory()
tr_hist.insert_dividends([DIVIDEND2, DIVIDEND1])

dividends = trlog.dividends()
dividends = tr_hist.dividends()
assert len(dividends) == 2
assert dividends[0].ticker == DIVIDEND1.ticker
assert dividends[1].ticker == DIVIDEND2.ticker


def test_transactionlog_transfers_are_sorted_by_timestamp():
trlog = TransactionLog()
trlog.insert_transfers([TRANSFER2, TRANSFER1])
tr_hist = TrHistory()
tr_hist.insert_transfers([TRANSFER2, TRANSFER1])

transfers = trlog.transfers()
transfers = tr_hist.transfers()
assert len(transfers) == 2
assert transfers[0].type == TransferType.DEPOSIT
assert transfers[1].type == TransferType.WITHDRAW


def test_transactionlog_interest_is_sorted_by_timestamp():
trlog = TransactionLog()
trlog.insert_interest([INTEREST2, INTEREST1])
tr_hist = TrHistory()
tr_hist.insert_interest([INTEREST2, INTEREST1])

interest = trlog.interest()
interest = tr_hist.interest()
assert len(interest) == 2
assert interest[0].amount == INTEREST1.amount
assert interest[1].amount == INTEREST2.amount


def test_transactionlog_show_orders(capsys):
trlog = TransactionLog()
trlog.insert_orders([ORDER1, ORDER2])
trlog.show_orders()
tr_hist = TrHistory()
tr_hist.insert_orders([ORDER1, ORDER2])
tr_hist.show_orders()
captured = capsys.readouterr()
assert ORDER1.ticker in captured.out
assert ORDER2.ticker in captured.out


def test_transactionlog_show_dividends(capsys):
trlog = TransactionLog()
trlog.insert_dividends([DIVIDEND1, DIVIDEND2])
trlog.show_dividends()
tr_hist = TrHistory()
tr_hist.insert_dividends([DIVIDEND1, DIVIDEND2])
tr_hist.show_dividends()
captured = capsys.readouterr()
assert DIVIDEND1.ticker in captured.out
assert DIVIDEND2.ticker in captured.out


def test_transactionlog_show_transfers(capsys):
trlog = TransactionLog()
trlog.insert_transfers([TRANSFER1, TRANSFER2])
trlog.show_transfers()
tr_hist = TrHistory()
tr_hist.insert_transfers([TRANSFER1, TRANSFER2])
tr_hist.show_transfers()
captured = capsys.readouterr()
assert str(TRANSFER1.amount) in captured.out
assert str(TRANSFER2.amount) in captured.out


def test_transactionlog_show_interest(capsys):
trlog = TransactionLog()
trlog.insert_interest([INTEREST1, INTEREST2])
trlog.show_interest()
tr_hist = TrHistory()
tr_hist.insert_interest([INTEREST1, INTEREST2])
tr_hist.show_interest()
captured = capsys.readouterr()
assert str(INTEREST1.amount) in captured.out
assert str(INTEREST2.amount) in captured.out

0 comments on commit 48c1247

Please sign in to comment.