def test_list_pf(self): pf = Portfolio.objects.create(name='Test') sec = Security() price = Price() mysec1 = sec.add('Test', ['SomeAliasString'], '', 'APC.DE', 'Stock') mysec2 = sec.add('Test2', ['SomeAliasString'], '', 'APC.DE', 'Stock') time_price_1 = timezone.now() - datetime.timedelta(days=20) time_price_2 = timezone.now() - datetime.timedelta(days=5) time_price_3 = timezone.now() - datetime.timedelta(days=2) time_price_4 = timezone.now() - datetime.timedelta(days=1) price.add(mysec1, time_price_1, 100) price.add(mysec1, time_price_2, 15) price.add(mysec1, time_price_3, 15) price.add(mysec1, time_price_4, 10) price.add(mysec2, time_price_1, 40) price.add(mysec2, time_price_2, 50) price.add(mysec2, time_price_3, 60) price.add(mysec2, time_price_4, 70) t = Transaction() # def add(self, transaction_type, portfolio, stock_id, date, nominal, price, cost): t.add('b', pf, mysec1, time_price_1, 10, 100, 0) t.add('b', pf, mysec1, time_price_2, 10, 50, 0) t.add('s', pf, mysec1, time_price_3, 10, 15, 0) t.add('b', pf, mysec2, time_price_1, 10, 40, 0) result = t.list_pf(pf.name, time_price_1, time_price_4) # print() # for item in result: # print(item['name'], item['profit']) # print() self.assertEqual(result[0]['profit'], Decimal('-1250')) self.assertEqual(result[1]['profit'], Decimal('300')) self.assertEqual(result[2]['profit'], Decimal('-950'))
def a_test_portfolio_overview(self): sec = Security.objects.create(name='TestSec', aliases='Test Alias', isin_id='DETest', yahoo_id='ABC', type='Stock') pf = Portfolio.objects.create(name='Test') t = Transaction() time = timezone.now().date() p = Price() p.add(sec, time, Decimal(111)) t.add('b', pf, sec, time, 100, 100, 100) p = t.list_pf(pf.name, time) self.assertEqual(p, [{ 'nominal': Decimal('100'), 'invest': Decimal('-10100'), 'profit': Decimal('1000'), 'name': 'TestSec', 'value': Decimal('11100'), 'price': Decimal('111'), 'cost': Decimal('100')}, { 'name': 'Total', 'value': Decimal('11100')}])