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_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 setUp(self): self.asset = AccountBuilder().set_name("name") \ .set_owner("owner") \ .set_investment("investment") \ .set_institution("institution") \ .set_update_frequency(3) \ .build() self.liability = AccountBuilder().set_name("name") \ .set_owner("owner") \ .set_investment("investment") \ .set_institution("institution") \ .set_liability() \ .build() self.portfolio = Portfolio()
def test_it_does_not_return_duplicate_institutions(self): account_one = AccountBuilder().set_name("name one") \ .set_institution("inst") \ .set_owner("owner") \ .set_investment("investment") \ .build() account_two = AccountBuilder().set_name("name two") \ .set_institution("inst") \ .set_owner("owner") \ .set_investment("investment") \ .build() self.portfolio.import_account(account_one) self.portfolio.import_account(account_two) self.assertEqual(self.portfolio.institutions(), ["inst"])
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 test_it_returns_two_institutions(self): account_one = AccountBuilder().set_name("name one") \ .set_institution("institution 1") \ .set_owner("owner") \ .set_investment("investment") \ .build() account_two = AccountBuilder().set_name("name two") \ .set_institution("institution 2") \ .set_owner("owner") \ .set_investment("investment") \ .build() self.portfolio.import_account(account_one) self.portfolio.import_account(account_two) self.assertTrue("institution 1" in self.portfolio.institutions()) self.assertTrue("institution 2" in self.portfolio.institutions())
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 test_it_returns_one_institution(self): account = AccountBuilder().set_name("name one") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .build() self.portfolio.import_account(account) self.assertEqual(self.portfolio.institutions(), ["institution"])
def test_it_imports_a_second_account_in_the_portfolio(self): account_one = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.ASSET) \ .build() account_two = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("another owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.ASSET) \ .build() self.portfolio.import_account(account_one) self.portfolio.import_account(account_two) self.assertEqual(self.portfolio.accounts, [account_one, account_two])
def test_it_imports_an_account(self): account = AccountBuilder().set_name("name") \ .set_institution("institution") \ .set_owner("owner") \ .set_investment("investment") \ .set_asset_class(AssetClass.NONE) \ .set_account_type(AccountType.ASSET) \ .build() self.portfolio.import_account(account) self.assertEqual(self.portfolio.accounts, [account])
def import_data(self, data): account = AccountBuilder()\ .set_name(data.get("name"))\ .set_institution(data.get("institution"))\ .set_owner(data.get("owner"))\ .set_investment(data.get("investment"))\ .set_asset_class(AssetClass(data.get("asset_class")))\ .set_account_type(AccountType(data.get("account_type")))\ .set_update_frequency(data.get("update_frequency"))\ .set_open_date(data.get("open_date"))\ .set_term(Term(data.get("term")))\ .build() self.__create_or_update(data.get("timestamp"), data.get("value"), account)
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_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_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)
class AssetTestCase(unittest.TestCase): def setUp(self): self.builder = AccountBuilder().set_name("name").set_owner("owner").set_investment("investment").set_institution("institution") def test_it_builds_an_account_with_defaults(self): account = self.builder.build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.CASH_EQUIVALENTS, "institution": "institution", "account_type": AccountType.ASSET} self.assertTrue(account.is_identical_to(Account(params))) def test_it_sets_an_account_as_a_liability_and_updates_the_asset_class(self): account = self.builder.set_liability().build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.NONE, "institution": "institution", "account_type": AccountType.LIABILITY} self.assertTrue(account.is_identical_to(Account(params))) def test_it_sets_an_account_as_a_liability_by_passing_in_the_account_type(self): account = self.builder.set_account_type(AccountType.LIABILITY).build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.NONE, "institution": "institution", "account_type": AccountType.LIABILITY} self.assertTrue(account.is_identical_to(Account(params))) def test_it_sets_an_account_as_an_asset(self): account = self.builder.set_liability().set_asset().build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.NONE, "institution": "institution", "account_type": AccountType.ASSET} self.assertTrue(account.is_identical_to(Account(params))) def test_it_sets_the_asset_class_of_an_account(self): account = self.builder.set_asset_class(AssetClass.ANNUITIES).build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.ANNUITIES, "institution": "institution", "account_type": AccountType.ASSET} self.assertTrue(account.is_identical_to(Account(params))) def test_it_sets_the_update_frequency_of_an_account(self): account = self.builder.set_update_frequency(12).build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.CASH_EQUIVALENTS, "institution": "institution", "account_type": AccountType.ASSET} self.assertTrue(account.is_identical_to(Account(params))) def test_it_sets_the_open_date(self): account = self.builder.set_open_date("2005-1-1").build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.CASH_EQUIVALENTS, "institution": "institution", "account_type": AccountType.ASSET, "open_date": "2005-1-1"} self.assertTrue(account.is_identical_to(Account(params))) def test_it_sets_the_term(self): account = self.builder.set_term(Term.MEDIUM).build() params = {"name": "name", "owner": "owner", "investment": "investment", "asset_class": AssetClass.CASH_EQUIVALENTS, "institution": "institution", "account_type": AccountType.ASSET, "term": Term.MEDIUM} self.assertTrue(account.is_identical_to(Account(params))) def test_the_account_name_is_required(self): builder = AccountBuilder().set_owner("owner").set_investment("investment").set_institution("institution") self.assertRaises(InvalidAccountException, builder.build) def test_the_account_owner_is_required(self): builder = AccountBuilder().set_name("name").set_investment("investment").set_institution("institution") self.assertRaises(InvalidAccountException, builder.build) def test_the_account_investment_is_required(self): builder = AccountBuilder().set_name("name").set_owner("owner").set_institution("institution") self.assertRaises(InvalidAccountException, builder.build) def test_the_account_institution_is_required(self): builder = AccountBuilder().set_name("name").set_owner("owner").set_investment("investment") self.assertRaises(InvalidAccountException, builder.build)
def test_the_account_institution_is_required(self): builder = AccountBuilder().set_name("name").set_owner("owner").set_investment("investment") self.assertRaises(InvalidAccountException, builder.build)
separator = "=>" default_start_date = "2018-01-01" institution = "Charles Schwab" name = "Brokerage" owner = "Cyrus" investment = "Johnson and Johnson" account_type = AccountType.ASSET asset_class = AssetClass.EQUITIES open_date = None test_account = AccountBuilder().set_name(name) \ .set_institution(institution) \ .set_owner(owner) \ .set_investment(investment) \ .set_asset_class(asset_class) \ .set_account_type(account_type) \ .set_update_frequency(90) \ .set_open_date(open_date)\ .build() account = None for a in portfolio.accounts: if a.is_identical_to(test_account): account = a if account is None: print("No account found") exit(1)
class BalanceSheetTestCase(unittest.TestCase): def setUp(self): self.asset = AccountBuilder().set_name("name") \ .set_owner("owner") \ .set_investment("investment") \ .set_institution("institution") \ .set_update_frequency(3) \ .build() self.liability = AccountBuilder().set_name("name") \ .set_owner("owner") \ .set_investment("investment") \ .set_institution("institution") \ .set_liability() \ .build() self.portfolio = Portfolio() 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_an_empty_balance_sheet_if_there_are_no_accounts_in_the_portfolio(self): balance_sheet = BalanceSheet(self.portfolio) expected_output = [["---", "---", "---", "---", "---", "---"], ["", "", "", "", "Total", "0.00"]] 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 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_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 test_it_returns_no_assets_or_liabilities(self): balance_sheet = BalanceSheet(self.portfolio) expected_output = {"assets": [], "liabilities": []} self.assertEqual(balance_sheet.json(), expected_output) def test_it_returns_an_asset(self): self.asset.import_snapshot(EpochDateConverter().date_to_epoch('2017-11-12'), 1020) self.portfolio.import_account(self.asset) balance_sheet = BalanceSheet(self.portfolio) expected_output = {"assets": [{"lastUpdated": "2017-11-12T12:00:00-05:00", "institution": "institution", "owner": "owner", "account": "name", "value": 1020, "investment": "investment"}], "liabilities": []} self.assertEqual(balance_sheet.json(), expected_output) 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_it_returns_a_liability(self): self.liability.import_snapshot(EpochDateConverter().date_to_epoch('2010-01-10'), 129) self.portfolio.import_account(self.liability) balance_sheet = BalanceSheet(self.portfolio) expected_output = {"assets": [], "liabilities": [{"lastUpdated": "2010-01-10T12:00:00-05:00", "institution": "institution", "owner": "owner", "account": "name", "value": 129, "investment": "investment"}]} self.assertEqual(balance_sheet.json(), expected_output) def test_it_returns_two_liabilities(self): liability_one = AccountBuilder().set_name("name one") \ .set_owner("owner one") \ .set_investment("investment one") \ .set_institution("institution one") \ .set_update_frequency(5) \ .set_liability() \ .build() liability_two = AccountBuilder().set_name("name two") \ .set_owner("owner two") \ .set_investment("investment two") \ .set_institution("institution two") \ .set_update_frequency(3) \ .set_liability() \ .build() liability_one.import_snapshot(EpochDateConverter().date_to_epoch('2005-11-12'), 100.50) liability_two.import_snapshot(EpochDateConverter().date_to_epoch('2000-12-12'), 1289) self.portfolio.import_account(liability_one) self.portfolio.import_account(liability_two) balance_sheet = BalanceSheet(self.portfolio) expected_output = {"assets": [], "liabilities": [{"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"}]} self.assertEqual(balance_sheet.json(), expected_output)
def setUp(self): self.builder = AccountBuilder().set_name("name").set_owner("owner").set_investment("investment").set_institution("institution")