def test_reduce_relative(self): # Test with a few different cost currencies. test_holdings = list(itertools.starmap(holdings.Holding, [ (None, D('1'), 'BLA', D('200'), 'USD', D('10'), D('1000'), D('1100'), None), (None, D('1'), 'BLA', D('200'), 'USD', D('10'), D('3000'), D('300'), None), (None, D('1'), 'BLA', D('200'), 'CAD', D('10'), D('500'), D('600'), None), ])) converted_holdings = holdings.reduce_relative(test_holdings) expected_holdings = list(itertools.starmap(holdings.Holding, [ (None, D('1'), 'BLA', D('200'), 'USD', D('0.5'), D('0.75'), D('300'), None), (None, D('1'), 'BLA', D('200'), 'USD', D('0.5'), D('0.25'), D('1100'), None), (None, D('1'), 'BLA', D('200'), 'CAD', D('1'), D('1'), D('600'), None), ])) self.assertEqual(expected_holdings, converted_holdings) # Test with a single cost currency (and some Nones), ensure the total is 100%. test_holdings = list(itertools.starmap(holdings.Holding, [ (None, D('1'), 'BLA', D('200'), 'USD', D('10'), D('1000'), D('1100'), None), (None, D('1'), 'BLA', D('200'), 'USD', D('10'), D('3000'), D('300'), None), (None, D('1'), 'BLA', D('200'), None, None, None, D('600'), None), ])) converted_holdings = holdings.reduce_relative(test_holdings) expected_holdings = list(itertools.starmap(holdings.Holding, [ (None, D('1'), 'BLA', D('200'), 'USD', D('0.5'), D('0.75'), D('300'), None), (None, D('1'), 'BLA', D('200'), 'USD', D('0.5'), D('0.25'), D('1100'), None), (None, D('1'), 'BLA', D('200'), None, None, None, D('600'), None), ])) self.assertEqual(expected_holdings, converted_holdings) self.assertEqual(D('1'), sum(holding.market_value or ZERO for holding in converted_holdings))
def report_holdings(currency, relative, entries, options_map, aggregation_key=None, sort_key=None): """Generate a detailed list of all holdings. Args: currency: A string, a currency to convert to. If left to None, no conversion is carried out. relative: A boolean, true if we should reduce this to a relative value. entries: A list of directives. options_map: A dict of parsed options. aggregation_key: A callable use to generate aggregations. sort_key: A function to use to sort the holdings, if specified. Returns: A Table instance. """ holdings_list, _ = holdings.get_assets_holdings(entries, options_map, currency) if aggregation_key: holdings_list = holdings.aggregate_holdings_by(holdings_list, aggregation_key) if relative: holdings_list = holdings.reduce_relative(holdings_list) field_spec = RELATIVE_FIELD_SPEC else: field_spec = FIELD_SPEC if sort_key: holdings_list.sort(key=sort_key, reverse=True) return table.create_table(holdings_list, field_spec)