-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c556401
commit 9e0e3be
Showing
6 changed files
with
114 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from decimal import Decimal | ||
|
||
from cdp.client.models.fiat_amount import FiatAmount as FiatAmountModel | ||
|
||
|
||
class FiatAmount: | ||
"""A representation of a FiatAmount that includes the amount and currency.""" | ||
|
||
def __init__(self, amount: Decimal, currency: str) -> None: | ||
"""Initialize a new FiatAmount. Do not use this directly, use the from_model method instead. | ||
Args: | ||
amount (Decimal): The amount in the fiat currency | ||
currency (str): The currency code (e.g. 'USD') | ||
""" | ||
self._amount = amount | ||
self._currency = currency | ||
|
||
@classmethod | ||
def from_model(cls, fiat_amount_model: FiatAmountModel) -> "FiatAmount": | ||
"""Convert a FiatAmount model to a FiatAmount. | ||
Args: | ||
fiat_amount_model (FiatAmountModel): The fiat amount from the API. | ||
Returns: | ||
FiatAmount: The converted FiatAmount object. | ||
""" | ||
return cls(amount=Decimal(fiat_amount_model.amount), currency=fiat_amount_model.currency) | ||
|
||
@property | ||
def amount(self) -> Decimal: | ||
"""Get the amount in the fiat currency. | ||
Returns: | ||
Decimal: The amount in the fiat currency. | ||
""" | ||
return self._amount | ||
|
||
@property | ||
def currency(self) -> str: | ||
"""Get the currency code. | ||
Returns: | ||
str: The currency code. | ||
""" | ||
return self._currency | ||
|
||
def __str__(self) -> str: | ||
"""Get a string representation of the FiatAmount. | ||
Returns: | ||
str: A string representation of the FiatAmount. | ||
""" | ||
return f"FiatAmount(amount: '{int(self.amount)}', currency: '{self.currency}')" | ||
|
||
def __repr__(self) -> str: | ||
"""Get a string representation of the FiatAmount. | ||
Returns: | ||
str: A string representation of the FiatAmount. | ||
""" | ||
return self.__str__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from decimal import Decimal | ||
|
||
from cdp.client.models.fiat_amount import FiatAmount as FiatAmountModel | ||
from cdp.fiat_amount import FiatAmount | ||
|
||
|
||
def test_fiat_amount_from_model(): | ||
"""Test converting a FiatAmount model to a FiatAmount.""" | ||
model = FiatAmountModel(amount="100.50", currency="USD") | ||
fiat_amount = FiatAmount.from_model(model) | ||
|
||
assert fiat_amount.amount == Decimal("100.50") | ||
assert fiat_amount.currency == "USD" | ||
|
||
|
||
def test_fiat_amount_properties(): | ||
"""Test FiatAmount properties.""" | ||
fiat_amount = FiatAmount(amount=Decimal("50.25"), currency="USD") | ||
|
||
assert fiat_amount.amount == Decimal("50.25") | ||
assert fiat_amount.currency == "USD" | ||
|
||
|
||
def test_fiat_amount_str_representation(): | ||
"""Test string representation of FiatAmount.""" | ||
fiat_amount = FiatAmount(amount=Decimal("75.00"), currency="USD") | ||
expected_str = "FiatAmount(amount: '75', currency: 'USD')" | ||
|
||
assert str(fiat_amount) == expected_str | ||
assert repr(fiat_amount) == expected_str | ||
|
||
|
||
def test_fiat_amount_repr(): | ||
"""Test repr of FiatAmount.""" | ||
fiat_amount = FiatAmount(amount=Decimal("75.00"), currency="USD") | ||
assert repr(fiat_amount) == "FiatAmount(amount: '75', currency: 'USD')" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters