import portfolio
import unittest
from unittest import mock
class AmountTest(unittest.TestCase):
def setUp(self):
super(AmountTest, self).setUp()
def test_values(self):
amount = portfolio.Amount("BTC", 0.65)
self.assertEqual(0.65, amount.value)
self.assertEqual("BTC", amount.currency)
amount = portfolio.Amount("BTC", 10, int_val=2000000000000000)
self.assertEqual(0.002, amount.value)
def test_in_currency(self):
amount = portfolio.Amount("ETC", 10)
self.assertEqual(amount, amount.in_currency("ETC", None))
ticker_mock = unittest.mock.Mock()
with mock.patch.object(portfolio.Amount, 'get_ticker', new=ticker_mock):
ticker_mock.return_value = None
portfolio.Amount.get_ticker = ticker_mock
self.assertRaises(Exception, amount.in_currency, "ETH", None)
with mock.patch.object(portfolio.Amount, 'get_ticker', new=ticker_mock):
ticker_mock.return_value = {
"average": 0.3,
"foo": "bar",
}
converted_amount = amount.in_currency("ETH", None)
self.assertEqual(3.0, converted_amount.value)
self.assertEqual("ETH", converted_amount.currency)
self.assertEqual(amount, converted_amount.linked_to)
self.assertEqual("bar", converted_amount.ticker["foo"])
@unittest.skip("TODO")
def test_get_ticker(self):
pass
def test__abs(self):
amount = portfolio.Amount("SC", -120)
self.assertEqual(120, abs(amount).value)
self.assertEqual("SC", abs(amount).currency)
amount = portfolio.Amount("SC", 10)
self.assertEqual(10, abs(amount).value)
self.assertEqual("SC", abs(amount).currency)
def test__add(self):
amount1 = portfolio.Amount("XVG", 12.9)
amount2 = portfolio.Amount("XVG", 13.1)
self.assertEqual(26, (amount1 + amount2).value)
self.assertEqual("XVG", (amount1 + amount2).currency)
amount3 = portfolio.Amount("ETH", 1.6)
with self.assertRaises(Exception):
amount1 + amount3
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 + amount4)
def test__radd(self):
amount = portfolio.Amount("XVG", 12.9)
self.assertEqual(amount, 0 + amount)
with self.assertRaises(Exception):
4 + amount
def test__sub(self):
amount1 = portfolio.Amount("XVG", 13.3)
amount2 = portfolio.Amount("XVG", 13.1)
self.assertEqual(0.2, (amount1 - amount2).value)
self.assertEqual("XVG", (amount1 - amount2).currency)
amount3 = portfolio.Amount("ETH", 1.6)
with self.assertRaises(Exception):
amount1 - amount3
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 - amount4)
def test__int(self):
amount = portfolio.Amount("XMR", 0.1)
self.assertEqual(100000000000000000, int(amount))
def test__mul(self):
amount = portfolio.Amount("XEM", 11)
self.assertEqual(38.5, (amount * 3.5).value)
self.assertEqual(33, (amount * 3).value)
with self.assertRaises(Exception):
amount * amount
def test__rmul(self):
amount = portfolio.Amount("XEM", 11)
self.assertEqual(38.5, (3.5 * amount).value)
self.assertEqual(33, (3 * amount).value)
def test__floordiv(self):
amount = portfolio.Amount("XEM", 11)
self.assertEqual(5.5, (amount // 2).value)
with self.assertRaises(TypeError):
amount // 2.5
self.assertEqual(1571428571428571428, (amount // 7)._value)
def test__div(self):
amount = portfolio.Amount("XEM", 11)
with self.assertRaises(TypeError):
amount / 2.5
self.assertEqual(5.5, (amount / 2).value)
self.assertEqual(1571428571428571428, (amount / 7)._value)
def test__lt(self):
amount1 = portfolio.Amount("BTD", 11.3)
amount2 = portfolio.Amount("BTD", 13.1)
self.assertTrue(amount1 < amount2)
self.assertFalse(amount2 < amount1)
self.assertFalse(amount1 < amount1)
amount3 = portfolio.Amount("BTC", 1.6)
with self.assertRaises(Exception):
amount1 < amount3
def test__eq(self):
amount1 = portfolio.Amount("BTD", 11.3)
amount2 = portfolio.Amount("BTD", 13.1)
amount3 = portfolio.Amount("BTD", 11.3)
self.assertFalse(amount1 == amount2)
self.assertFalse(amount2 == amount1)
self.assertTrue(amount1 == amount3)
self.assertFalse(amount2 == 0)
amount4 = portfolio.Amount("BTC", 1.6)
with self.assertRaises(Exception):
amount1 == amount4
amount5 = portfolio.Amount("BTD", 0)
self.assertTrue(amount5 == 0)
def test__str(self):
amount1 = portfolio.Amount("BTX", 32)
self.assertEqual("32.00000000 BTX", str(amount1))
amount2 = portfolio.Amount("USDT", 12000)
amount1.linked_to = amount2
self.assertEqual("32.00000000 BTX [12000.00000000 USDT]", str(amount1))
def test__repr(self):
amount1 = portfolio.Amount("BTX", 32)
self.assertEqual("Amount(32.00000000 BTX)", repr(amount1))
amount2 = portfolio.Amount("USDT", 12000)
amount1.linked_to = amount2
self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT))", repr(amount1))
amount3 = portfolio.Amount("BTC", 0.1)
amount2.linked_to = amount3
self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1))
if __name__ == '__main__':
unittest.main()