from decimal import Decimal, InvalidOperation from django.test import TestCase from django.utils import timezone from core.models import Balance def _create_and_reload_balance(amount): bal = Balance.objects.create(amount=amount, date=timezone.now().date()) bal.refresh_from_db() return bal class PriceFieldTestCase(TestCase): def test_normal_decimal(self): bal = _create_and_reload_balance(Decimal("13.37")) self.assertEqual(bal.amount, Decimal("13.37")) def test_too_big_decimal(self): with self.assertRaises(InvalidOperation): _create_and_reload_balance(Decimal("13371337.00")) def test_rounding_decimal(self): bal = _create_and_reload_balance(Decimal("1.337")) self.assertEqual(bal.amount, Decimal("1.34")) def test_normal_int(self): bal = _create_and_reload_balance(1337) self.assertEqual(bal.amount, Decimal("1337.00")) def test_too_big_int(self): with self.assertRaises(InvalidOperation): _create_and_reload_balance(13371337) def test_normal_float(self): bal = _create_and_reload_balance(13.37) self.assertEqual(bal.amount, Decimal("13.37")) def test_too_big_float(self): with self.assertRaises(InvalidOperation): _create_and_reload_balance(13371337.00) def test_rounding_float(self): bal = _create_and_reload_balance(1.337) self.assertEqual(bal.amount, Decimal("1.34"))