rate: Add Rate.convert method.
This commit is contained in:
parent
0120e4ce5a
commit
9d47238245
3 changed files with 19 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import decimal
|
||||||
import functools
|
import functools
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
@ -34,14 +35,21 @@ class Rate:
|
||||||
def deserialize_timestamp(cls, value):
|
def deserialize_timestamp(cls, value):
|
||||||
return datetime.datetime.fromtimestamp(value)
|
return datetime.datetime.fromtimestamp(value)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def serialize_rates(cls, rates):
|
||||||
|
return {code: float(rates[code]) for code in rates}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def serialize_timestamp(cls, value):
|
def serialize_timestamp(cls, value):
|
||||||
return int(value.timestamp())
|
return int(value.timestamp())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json_file(cls, json_file):
|
def from_json_file(cls, json_file):
|
||||||
response = json.load(json_file)
|
response = json.load(json_file, parse_float=decimal.Decimal)
|
||||||
return cls(**response)
|
return cls(**response)
|
||||||
|
|
||||||
|
def convert(self, amount, from_currency, to_currency):
|
||||||
|
return amount * self.rates[to_currency] / self.rates[from_currency]
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
return dict(self.walk_fields(functools.partial(getattr, self), 'serialize'))
|
return dict(self.walk_fields(functools.partial(getattr, self), 'serialize'))
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
"rates": {
|
"rates": {
|
||||||
"AED": 3.67246,
|
"AED": 3.67246,
|
||||||
"ALL": 144.529793,
|
"ALL": 144.529793,
|
||||||
"ANG": 1.79
|
"ANG": 1.79,
|
||||||
|
"USD": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import decimal
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
@ -7,6 +8,9 @@ from . import relpath
|
||||||
import oxrlib.rate
|
import oxrlib.rate
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
HIST1_AED_RATE = decimal.Decimal('3.67246')
|
||||||
|
HIST1_ALL_RATE = decimal.Decimal('144.529793')
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def historical1_rate():
|
def historical1_rate():
|
||||||
with open(relpath('historical1.json').as_posix()) as rate_file:
|
with open(relpath('historical1.json').as_posix()) as rate_file:
|
||||||
|
@ -21,3 +25,7 @@ def test_serialize(historical1_rate):
|
||||||
expected = json.load(rate_file)
|
expected = json.load(rate_file)
|
||||||
assert expected == historical1_rate.serialize()
|
assert expected == historical1_rate.serialize()
|
||||||
|
|
||||||
|
def test_convert(historical1_rate):
|
||||||
|
assert historical1_rate.convert(2, 'USD', 'ALL') == (2 * HIST1_ALL_RATE)
|
||||||
|
assert historical1_rate.convert(10, 'AED', 'USD') == (10 / HIST1_AED_RATE)
|
||||||
|
assert historical1_rate.convert(1, 'AED', 'ALL') == (HIST1_ALL_RATE / HIST1_AED_RATE)
|
||||||
|
|
Loading…
Reference in a new issue