Commit 006e31b4 authored by Adrien Oliva's avatar Adrien Oliva

feat(usecase): Implement account Update request

parent da6d74cd
Pipeline #820 passed with stages
in 3 minutes and 45 seconds
......@@ -115,12 +115,20 @@ class Account:
return '{} <{}>'.format(self.label, self.ident)
def __repr__(self):
float_precision = 'initialbalance={}:.0{}f{})'.format(
'{', self.currency.exponent, '}'
)
return (
'Account(' +
', '.join([
'ident={!r}'.format(self.ident),
'label={!r}'.format(self.label),
'currency={!r}'.format(self.currency),
'initialbalance={!r})'.format(self.initialbalance)
float_precision.format(
round(
self.initialbalance / (10 ** self.currency.exponent),
self.currency.exponent
)
)
])
)
......@@ -258,3 +258,72 @@ class Delete(base.UseCaseBase):
self._repo.delete_account(request.ident)
return base.ResponseSuccess()
class UpdateRequest(base.ValidRequest):
"""
Request use as input in Update use cases
"""
def __init__(self, ident, **kwargs):
self.ident = ident
self.label = kwargs.get('label', None)
self.currency = kwargs.get('currency', None)
self.initialbalance = kwargs.get('initialbalance', None)
@classmethod
def from_dict(cls, adict):
"""
:adict Keys:
* *ident* (``str``) --
_mandatory_, String that identify accoutn to modify.
* *label* (``str``) --
_optional_, New Human readable account description.
* *currency* (``str``) --
_optional_, New account currency, ISO-4217 code
* *initialbalance* (``float``) --
_optional_, Initial balance of account.
"""
errors = base.InvalidRequest()
if 'ident' not in adict:
errors.add_error('ident', 'Required argument')
if 'currency' in adict:
try:
currency.from_code(adict['currency'])
except exceptions.UnknownCurrency:
errors.add_error(
'currency',
'Must be a valid ISO-4217 currency code'
)
if 'initialbalance' in adict:
try:
adict['initialbalance'] = float(adict['initialbalance'])
except ValueError:
errors.add_error(
'initialbalance',
'Must be a float'
)
if errors.has_errors():
return errors
return cls(**adict)
def to_dict(self):
"""
Export request information to a dictionnary with account ident and all
modified information.
:return: Account modification dict
:rtype: ``dict``
"""
account = {
'ident': self.ident
}
if self.label is not None:
account['label'] = self.label
if self.currency is not None:
account['currency'] = self.currency
if self.initialbalance is not None:
account['initialbalance'] = self.initialbalance
return account
......@@ -174,5 +174,5 @@ def test_account_representations():
assert representation.startswith(
"Account(ident='MyAccount', label='MyLabel', currency="
)
assert representation.endswith(', initialbalance=1200)')
assert representation.endswith(', initialbalance=12.00)')
assert 'EUR' in representation
......@@ -342,3 +342,77 @@ def test_delete_request_factory_invalid():
'message': 'Required argument'
}
]
def test_update_request_ident():
"""
Create an update request
"""
req = ucaccount.UpdateRequest('foobar')
assert bool(req)
assert req.ident == 'foobar'
assert req.label is None
assert req.currency is None
assert req.initialbalance is None
assert req.to_dict() == {
'ident': 'foobar'
}
def test_update_request_factory_valid():
"""
Create an update request from factory
"""
req = ucaccount.UpdateRequest.from_dict({
'ident': 'baz',
'label': 'account label',
'currency': 'USD',
'initialbalance': '12.34'
})
assert bool(req)
assert req.ident == 'baz'
assert req.label == 'account label'
assert req.currency == 'USD'
assert req.initialbalance == 12.34
assert req.to_dict() == {
'ident': 'baz',
'label': 'account label',
'currency': 'USD',
'initialbalance': 12.34
}
def test_update_request_no_ident():
"""
Create an update request without ident
"""
req = ucaccount.UpdateRequest.from_dict({})
assert not bool(req)
assert isinstance(req, base.InvalidRequest)
assert req.errors == [{
'parameter': 'ident',
'message': 'Required argument'
}]
def test_update_request_invalid_currency_balance():
"""
Create an update request with invalid currency and balance
"""
req = ucaccount.UpdateRequest.from_dict({
'ident': 'bar',
'currency': 'FOO',
'initialbalance': '12AZ'
})
assert not bool(req)
assert isinstance(req, base.InvalidRequest)
assert req.errors == [{
'parameter': 'currency',
'message': 'Must be a valid ISO-4217 currency code'
}, {
'parameter': 'initialbalance',
'message': 'Must be a float'
}]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment