def test_it_returns_a_formatted_row_for_a_balance_sheet(self): date_difference = Constants.SECONDS_PER_DAY * 2 timestamp = EpochDateConverter().date_to_epoch() expected_date = EpochDateConverter().epoch_to_date(timestamp - date_difference) self.asset.import_snapshot(timestamp - date_difference, 100) balance_sheet_row = BalanceSheet().row(self.asset) self.assertEqual(balance_sheet_row, [expected_date, "institution", "name", "investment", "owner", "100.00"])
def test_it_returns_the_debt_to_equity_ratio_for_a_historical_time(self): asset = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.ASSET) \ .build() liability = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.LIABILITY) \ .build() timestamp = EpochDateConverter().date_to_epoch() early_timestamp = timestamp - 200000 query_time = timestamp - 100000 asset.import_snapshot(timestamp, 112233) liability.import_snapshot(early_timestamp, 223344) portfolio = Portfolio() portfolio.import_account(asset) portfolio.import_account(liability) self.assertEqual( PortfolioAnalyzer(portfolio).debt_to_equity( EpochDateConverter().epoch_to_date(query_time)), 1.0)
def __create_or_update(self, date, value, account): for existing_account in self.accounts: if existing_account.is_identical_to(account): existing_account.import_snapshot( EpochDateConverter().date_to_epoch(date), value) return account.import_snapshot(EpochDateConverter().date_to_epoch(date), value) self.accounts.append(account)
def test_it_returns_the_value_of_two_accounts(self): account_one = AccountBuilder().set_name("name")\ .set_institution("institution")\ .set_owner("Craig")\ .set_investment("investment")\ .build() account_two = AccountBuilder().set_name("name")\ .set_institution("institution")\ .set_owner("Samuel")\ .set_investment("investment")\ .set_liability()\ .build() account_one.import_snapshot( EpochDateConverter().date_to_epoch("2009-10-15"), 10) account_two.import_snapshot( EpochDateConverter().date_to_epoch("2009-10-17"), 1000) self.portfolio.import_account(account_one) self.portfolio.import_account(account_two) line_graph = LineGraph(self.portfolio) net_worth_values = line_graph.net_worth_vs_time( "2009-10-13", "2009-10-19") self.assertEqual(net_worth_values[0], { "series": "net-worth", "date": "2009-10-13", "value": 0 }) self.assertEqual(net_worth_values[1], { "series": "net-worth", "date": "2009-10-14", "value": 0 }) self.assertEqual(net_worth_values[2], { "series": "net-worth", "date": "2009-10-15", "value": 10 }) self.assertEqual(net_worth_values[3], { "series": "net-worth", "date": "2009-10-16", "value": 10 }) self.assertEqual(net_worth_values[4], { "series": "net-worth", "date": "2009-10-17", "value": -990 }) self.assertEqual(net_worth_values[5], { "series": "net-worth", "date": "2009-10-18", "value": -990 }) self.assertEqual(net_worth_values[6], { "series": "net-worth", "date": "2009-10-19", "value": -990 })
def __outdated_account(self, accounts): output = [] for account in accounts: last_updated = EpochDateConverter().date_to_epoch( account.last_updated()) expected_update = EpochDateConverter().date_to_epoch( ) - account.update_frequency() * Constants.SECONDS_PER_DAY if last_updated < expected_update: output.append(account) return output
def test_it_returns_a_balance_sheet_with_an_asset_and_a_liability(self): self.asset.import_snapshot(EpochDateConverter().date_to_epoch('2017-11-12'), 1020) self.liability.import_snapshot(EpochDateConverter().date_to_epoch('2013-5-5'), 0.12) self.portfolio.import_account(self.asset) self.portfolio.import_account(self.liability) balance_sheet = BalanceSheet(self.portfolio) expected_output = [["2017-11-12", "institution", "name", "investment", "owner", "1020.00"], ["---", "---", "---", "---", "---", "---"], ["2013-05-05", "institution", "name", "investment", "owner", "0.12"], ["", "", "", "", "Total", "1019.88"]] self.assertEqual(balance_sheet.create(), expected_output)
def net_worth_vs_time(self, start_date, end_date): current_epoch = EpochDateConverter().date_to_epoch(start_date) output = [] while current_epoch <= EpochDateConverter().date_to_epoch(end_date): formatted_date = EpochDateConverter().epoch_to_date(current_epoch) output.append({ "series": "net-worth", "date": datetime.datetime.fromtimestamp(current_epoch).strftime( "%Y-%m-%d"), "value": self.__portfolio.total_value(formatted_date) }) current_epoch += Constants.SECONDS_PER_DAY return output
def test_it_returns_the_value_of_a_single_account(self): account = AccountBuilder().set_name("name")\ .set_institution("institution")\ .set_owner("Craig")\ .set_investment("investment")\ .build() account.import_snapshot( EpochDateConverter().date_to_epoch("2005-12-10"), 1000) self.portfolio.import_account(account) line_graph = LineGraph(self.portfolio) net_worth_values = line_graph.net_worth_vs_time( "2005-12-09", "2005-12-11") self.assertEqual(net_worth_values[0], { "series": "net-worth", "date": "2005-12-09", "value": 0 }) self.assertEqual(net_worth_values[1], { "series": "net-worth", "date": "2005-12-10", "value": 1000 }) self.assertEqual(net_worth_values[2], { "series": "net-worth", "date": "2005-12-11", "value": 1000 })
def test_it_returns_a_balance_sheet_with_one_liability(self): self.liability.import_snapshot(EpochDateConverter().date_to_epoch('2011-1-1'), 500.12) self.portfolio.import_account(self.liability) balance_sheet = BalanceSheet(self.portfolio) expected_output = [["---", "---", "---", "---", "---", "---"], ["2011-01-01", "institution", "name", "investment", "owner", "500.12"], ["", "", "", "", "Total", "-500.12"]] self.assertEqual(balance_sheet.create(), expected_output)
def test_it_returns_a_balance_sheet_with_one_asset(self): self.asset.import_snapshot(EpochDateConverter().date_to_epoch('2017-12-12'), 100) self.portfolio.import_account(self.asset) balance_sheet = BalanceSheet(self.portfolio) expected_output = [["2017-12-12", "institution", "name", "investment", "owner", "100.00"], ["---", "---", "---", "---", "---", "---"], ["", "", "", "", "Total", "100.00"]] self.assertEqual(balance_sheet.create(), expected_output)
def append_snapshot(): global portfolio request_body = AppendSnapshotFormatter(EpochDateConverter()).format( request.form.to_dict()) json_body = json.dumps(request_body) requests.post(Constants.DATA_URL + "/append_snapshot", data=json_body) portfolio = PortfolioCreator().create(DataSource()) return redirect("/accounts", code=302)
def test_it_returns_the_latest_timestamp_for_two_snapshots(self): current_epoch = self.converter.date_to_epoch() formatted_date = EpochDateConverter().epoch_to_date(current_epoch) snapshot = Snapshot(current_epoch, 1000) self.history.import_snapshot(snapshot) snapshot = Snapshot(current_epoch - 1000000, 2000) self.history.import_snapshot(snapshot) self.assertEqual(self.history.last_updated(), formatted_date)
def test_it_returns_the_correct_value_when_queried_in_between_two_snapshots( self): later_timestamp = EpochDateConverter().date_to_epoch() earlier_timestamp = later_timestamp - 120 query_time = (earlier_timestamp + later_timestamp) / 2 self.asset.import_snapshot(earlier_timestamp, 300) self.asset.import_snapshot(later_timestamp, 250) value = self.asset.value(query_time) self.assertEqual(value, 300)
def test_it_returns_two_assets(self): asset_one = AccountBuilder().set_name("name one") \ .set_owner("owner one") \ .set_investment("investment one") \ .set_institution("institution one") \ .set_update_frequency(5) \ .build() asset_two = AccountBuilder().set_name("name two") \ .set_owner("owner two") \ .set_investment("investment two") \ .set_institution("institution two") \ .set_update_frequency(3) \ .build() asset_one.import_snapshot(EpochDateConverter().date_to_epoch('2005-11-12'), 100.50) asset_two.import_snapshot(EpochDateConverter().date_to_epoch('2000-12-12'), 1289) self.portfolio.import_account(asset_one) self.portfolio.import_account(asset_two) balance_sheet = BalanceSheet(self.portfolio) expected_output = {"assets": [{"lastUpdated": "2005-11-12T12:00:00-05:00", "institution": "institution one", "owner": "owner one", "account": "name one", "value": 100.50, "investment": "investment one"}, {"lastUpdated": "2000-12-12T12:00:00-05:00", "institution": "institution two", "owner": "owner two", "account": "name two", "value": 1289, "investment": "investment two"}], "liabilities": []} self.assertEqual(balance_sheet.json(), expected_output)
def test_the_order_in_which_snapshots_are_imported_makes_no_difference( self): timestamp1 = EpochDateConverter().date_to_epoch() timestamp2 = timestamp1 - 1 timestamp3 = timestamp1 - 2 query_time = timestamp1 + 1 self.asset.import_snapshot(timestamp2, 20) self.asset.import_snapshot(timestamp1, 10) self.asset.import_snapshot(timestamp3, 30) value = self.asset.value(query_time) self.assertEqual(value, 10)
def test_it_returns_the_debt_to_equity_ratio_for_a_portfolio_with_only_liabilities( self): account = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.LIABILITY) \ .build() account.import_snapshot(EpochDateConverter().date_to_epoch(), 1234) portfolio = Portfolio() portfolio.import_account(account) self.assertEqual(PortfolioAnalyzer(portfolio).debt_to_equity(), 1.0)
def test_it_returns_the_debt_to_equity_ratio_for_a_portfolio_with_a_mixture_of_accounts_in_nonequal_value( self): asset = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.ASSET) \ .build() liability = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.LIABILITY) \ .build() asset.import_snapshot(EpochDateConverter().date_to_epoch(), 1234) liability.import_snapshot(EpochDateConverter().date_to_epoch(), 12345) portfolio = Portfolio() portfolio.import_account(asset) portfolio.import_account(liability) self.assertAlmostEqual( PortfolioAnalyzer(portfolio).debt_to_equity(), 1.1110611)
def test_it_returns_a_list_of_outdated_liabilities(self): account_one = AccountBuilder().set_name("name one") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.LIABILITY) \ .set_update_frequency(10) \ .build() account_two = AccountBuilder().set_name("name two") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.LIABILITY) \ .set_update_frequency(3) \ .build() timestamp = EpochDateConverter().date_to_epoch() - 7 * Constants.SECONDS_PER_DAY account_one.import_snapshot(timestamp, 100) account_two.import_snapshot(timestamp, 100) self.portfolio.import_account(account_one) self.portfolio.import_account(account_two) self.assertEqual(self.portfolio.outdated_liabilities(), [account_two])
def test_it_returns_the_latest_timestamp(self): epoch = EpochDateConverter().date_to_epoch() self.asset.import_snapshot(epoch, 100) updated = self.asset.last_updated() self.assertEqual(updated, EpochDateConverter().epoch_to_date(epoch))
def test_it_defaults_to_the_current_time_if_no_argument_is_given(self): timestamp = EpochDateConverter().date_to_epoch() self.asset.import_snapshot(timestamp - 5, 10) self.asset.import_snapshot(timestamp - 10, 20) value = self.asset.value() self.assertEqual(value, 10)
def test_the_value_of_the_account_changes_on_the_day_a_snapshot_is_recorded( self): epoch = EpochDateConverter().date_to_epoch("2015-12-12") self.asset.import_snapshot(epoch, 100) value = self.asset.value(epoch) self.assertEqual(value, 100)
def test_it_returns_the_correct_value_when_queried_after_a_snapshot(self): timestamp = EpochDateConverter().date_to_epoch() query_time = timestamp + 20 self.asset.import_snapshot(timestamp, 100) value = self.asset.value(query_time) self.assertEqual(value, 100)
def test_it_returns_an_value_of_zero_when_queried_before_a_snapshot(self): timestamp = EpochDateConverter().date_to_epoch() query_time = timestamp - 20 self.asset.import_snapshot(timestamp, 100) value = self.asset.value(query_time) self.assertEqual(value, 0)
def test_it_updates_the_value_at_the_time_the_snapshot_is_recorded(self): epoch = EpochDateConverter().date_to_epoch("2014-02-03") self.history.import_snapshot(Snapshot(epoch, 3060)) value = self.history.value(epoch) self.assertEqual(value, 3060)
class SnapshotHistoryTestCase(unittest.TestCase): def setUp(self): self.history = SnapshotHistory() self.converter = EpochDateConverter() def test_imports_a_snapshot(self): snapshot = Snapshot(self.converter.date_to_epoch(), 1000) self.history.import_snapshot(snapshot) self.assertEqual(self.history.all(), [snapshot]) def test_imports_two_snapshots(self): snapshot1 = Snapshot(self.converter.date_to_epoch(), 1000) snapshot2 = Snapshot(self.converter.date_to_epoch(), 100) self.history.import_snapshot(snapshot1) self.history.import_snapshot(snapshot2) self.assertEqual(self.history.all(), [snapshot1, snapshot2]) def test_it_has_a_value_of_zero_if_there_are_no_snapshots(self): self.assertEqual(self.history.value(), 0) def test_it_returns_an_value_of_zero_when_queried_before_a_snapshot(self): timestamp = self.converter.date_to_epoch() query_time = timestamp - 20 self.history.import_snapshot(Snapshot(timestamp, 100)) value = self.history.value(query_time) self.assertEqual(value, 0) def test_it_returns_the_correct_value_when_queried_after_a_snapshot(self): timestamp = self.converter.date_to_epoch() query_time = timestamp + 20 self.history.import_snapshot(Snapshot(timestamp, 100)) value = self.history.value(query_time) self.assertEqual(value, 100) def test_it_returns_the_correct_value_when_queried_in_between_two_snapshots(self): later_timestamp = self.converter.date_to_epoch() earlier_timestamp = later_timestamp - 120 query_time = (earlier_timestamp + later_timestamp) / 2 self.history.import_snapshot(Snapshot(earlier_timestamp, 300)) self.history.import_snapshot(Snapshot(later_timestamp, 250)) value = self.history.value(query_time) self.assertEqual(value, 300) def test_it_updates_the_value_at_the_time_the_snapshot_is_recorded(self): epoch = EpochDateConverter().date_to_epoch("2014-02-03") self.history.import_snapshot(Snapshot(epoch, 3060)) value = self.history.value(epoch) self.assertEqual(value, 3060) def test_the_order_in_which_snapshots_are_imported_makes_no_difference(self): timestamp1 = self.converter.date_to_epoch() timestamp2 = timestamp1 - 1 timestamp3 = timestamp1 - 2 query_time = timestamp1 + 1 self.history.import_snapshot(Snapshot(timestamp2, 20)) self.history.import_snapshot(Snapshot(timestamp1, 10)) self.history.import_snapshot(Snapshot(timestamp3, 30)) value = self.history.value(query_time) self.assertEqual(value, 10) def test_it_defaults_to_the_current_epoch_if_no_argument_is_given(self): timestamp = self.converter.date_to_epoch() self.history.import_snapshot(Snapshot(timestamp - 5, 10)) self.history.import_snapshot(Snapshot(timestamp - 10, 20)) value = self.history.value() self.assertEqual(value, 10) def test_it_returns_the_latest_timestamp_for_one_snapshot(self): current_epoch = self.converter.date_to_epoch() formatted_date = EpochDateConverter().epoch_to_date(current_epoch) snapshot = Snapshot(current_epoch, 1000) self.history.import_snapshot(snapshot) self.assertEqual(self.history.last_updated(), formatted_date) def test_it_returns_the_latest_timestamp_for_two_snapshots(self): current_epoch = self.converter.date_to_epoch() formatted_date = EpochDateConverter().epoch_to_date(current_epoch) snapshot = Snapshot(current_epoch, 1000) self.history.import_snapshot(snapshot) snapshot = Snapshot(current_epoch - 1000000, 2000) self.history.import_snapshot(snapshot) self.assertEqual(self.history.last_updated(), formatted_date) def test_it_returns_the_latest_timestamp_for_three_snapshots(self): current_epoch = self.converter.date_to_epoch() formatted_date = EpochDateConverter().epoch_to_date(current_epoch) snapshot = Snapshot(current_epoch, 1000) self.history.import_snapshot(snapshot) snapshot = Snapshot(current_epoch - 1000000, 2000) self.history.import_snapshot(snapshot) snapshot = Snapshot(current_epoch - 2000000, 2000) self.history.import_snapshot(snapshot) self.assertEqual(self.history.last_updated(), formatted_date)
import datetime from portfolio_analysis.portfolio_analyzer import PortfolioAnalyzer from portfolio_creator.data_source import DataSource from portfolio_creator.portfolio_creator import PortfolioCreator from pylab import plot, xlabel, ylabel, title, show from utilities.constants import Constants from utilities.epoch_date_converter import EpochDateConverter portfolio = PortfolioCreator().create(DataSource()) analyzer = PortfolioAnalyzer(portfolio) number_of_days = round(Constants.DAYS_PER_YEAR) times = [] debt = [] for day in range(0, number_of_days): historical_time = EpochDateConverter().date_to_epoch( ) - day * Constants.SECONDS_PER_DAY formatted_date = EpochDateConverter().epoch_to_date(historical_time) times.append(datetime.datetime.fromtimestamp(historical_time)) debt.append(portfolio.liabilities_without_mortgage(formatted_date)) plot(times, debt) xlabel('Date') ylabel("Debt") title("Debt vs. Time") show()
def setUp(self): self.timestamp = EpochDateConverter().date_to_epoch() self.snapshot = Snapshot(self.timestamp, 10235.63)
def setUp(self): self.history = SnapshotHistory() self.converter = EpochDateConverter()
def __value_of(self, accounts, date=None): return sum( account.value(EpochDateConverter().date_to_epoch(date)) for account in accounts)
from pylab import plot, xlabel, ylabel, title, show from portfolio_creator.data_source import DataSource from portfolio_creator.portfolio_creator import PortfolioCreator from report.line_graph import LineGraph from utilities.epoch_date_converter import EpochDateConverter portfolio = PortfolioCreator().create(DataSource()) data = LineGraph(portfolio).net_worth_vs_time( "2003-01-01", EpochDateConverter().epoch_to_date()) plot(data["times"], data["values"]) xlabel('Date') ylabel("Owner's Equity") title("Owner's Equity vs. Time") show()