def convert_amount(self, amount, target_currency, date): """Convert an amount to a specific currency.""" # TODO(blais): Save the amount here too. return convert.convert_amount(amount, target_currency, self.price_map, date=date)
def _convert_cost(self, node, date) -> amount.Amount: account_cost_node = node.balance.reduce(convert.get_cost) cur, num = list(account_cost_node.items())[0] amt = amount.Amount(num, cur) account_cost_amt = convert.convert_amount( amt, self.operating_currency, g.ledger.price_map, date ) return account_cost_amt
def test_convert_amount_with_date(self, entries, _, __): """ 2013-01-01 price USD 1.20 CAD 2014-01-01 price USD 1.25 CAD 2015-01-01 price USD 1.30 CAD """ price_map = prices.build_price_map(entries) for date, exp_amount in [ (None, A('130 CAD')), (datetime.date(2015, 1, 1), A('130 CAD')), (datetime.date(2014, 12, 31), A('125 CAD')), (datetime.date(2014, 1, 1), A('125 CAD')), (datetime.date(2013, 12, 31), A('120 CAD')), (datetime.date(2013, 1, 1), A('120 CAD')), (datetime.date(2012, 12, 31), A('100 USD')), ]: self.assertEqual( exp_amount, convert.convert_amount(A('100 USD'), 'CAD', price_map, date))
def convert_amount(context, amount_, currency, date=None): """Coerce an amount to a particular currency.""" return convert.convert_amount(amount_, currency, context.price_map, date)
def convert_amount(self, amount, target_currency, date): # TODO(blais): Save the amount here too. return convert.convert_amount(amount, target_currency, self.price_map, date=date)
def test_convert_amount__noop(self): amt = A("127.00 USD") self.assertEqual( amt, convert.convert_amount(amt, "USD", self.PRICE_MAP_EMPTY)) self.assertEqual( amt, convert.convert_amount(amt, "USD", self.PRICE_MAP_HIT))
def test_convert_amount__success(self): amt = A("127.00 USD") self.assertEqual( A("152.40 CAD"), convert.convert_amount(amt, "CAD", self.PRICE_MAP_HIT))
def test_convert_amount__fail(self): amt = A("127.00 USD") self.assertEqual( amt, convert.convert_amount(amt, "CAD", self.PRICE_MAP_EMPTY))
def __call__(self, context): args = self.eval_args(context) amount_, currency, date = args return convert.convert_amount(amount_, currency, context.price_map, date)