Replace float with Decimal for price calculations
Fix Decimal precision issue Fix Decimal precision issue Fix Decimal precision issue Fix old prices for tickets Fix Decimal precision issue
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock, AsyncMock
|
||||
from decimal import Decimal
|
||||
|
||||
from src.ez_lan_manager.services.AccountingService import AccountingService, InsufficientFundsError
|
||||
from src.ez_lan_manager.types.Transaction import Transaction
|
||||
@@ -12,7 +13,6 @@ class AccountingServiceTests(unittest.IsolatedAsyncioTestCase):
|
||||
self.mock_database_service.add_transaction = AsyncMock()
|
||||
self.accounting_service = AccountingService(self.mock_database_service)
|
||||
|
||||
|
||||
def test_importing_unit_under_test_works(self) -> None:
|
||||
"""
|
||||
This test asserts that the object produced in setUp is AccountingService object,
|
||||
@@ -21,59 +21,59 @@ class AccountingServiceTests(unittest.IsolatedAsyncioTestCase):
|
||||
self.assertIsInstance(self.accounting_service, AccountingService)
|
||||
|
||||
def test_making_string_from_euro_value_works_correctly(self) -> None:
|
||||
test_value = 13466
|
||||
test_value = Decimal("134.66")
|
||||
expected_result = "134.66 €"
|
||||
self.assertEqual(expected_result, AccountingService.make_euro_string_from_int(test_value))
|
||||
self.assertEqual(expected_result, AccountingService.make_euro_string_from_decimal(test_value))
|
||||
|
||||
def test_making_euro_string_from_negative_value_works_correctly(self) -> None:
|
||||
test_value = -99741
|
||||
test_value = Decimal("-997.41")
|
||||
expected_result = "-997.41 €"
|
||||
self.assertEqual(expected_result, AccountingService.make_euro_string_from_int(test_value))
|
||||
self.assertEqual(expected_result, AccountingService.make_euro_string_from_decimal(test_value))
|
||||
|
||||
def test_making_euro_string_from_less_than_ten_cents_works_correctly(self) -> None:
|
||||
test_value = 4
|
||||
test_value = Decimal("0.04")
|
||||
expected_result = "0.04 €"
|
||||
self.assertEqual(expected_result, AccountingService.make_euro_string_from_int(test_value))
|
||||
self.assertEqual(expected_result, AccountingService.make_euro_string_from_decimal(test_value))
|
||||
|
||||
async def test_get_balance_correctly_adds_up_transactions(self) -> None:
|
||||
self.mock_database_service.get_all_transactions_for_user = AsyncMock(return_value=[
|
||||
Transaction(
|
||||
user_id=0,
|
||||
value=5,
|
||||
value=Decimal("0.05"),
|
||||
is_debit=True,
|
||||
reference="",
|
||||
transaction_date=datetime.now()
|
||||
),
|
||||
Transaction(
|
||||
user_id=0,
|
||||
value=99,
|
||||
value=Decimal("0.99"),
|
||||
is_debit=False,
|
||||
reference="",
|
||||
transaction_date=datetime.now()
|
||||
),
|
||||
Transaction(
|
||||
user_id=0,
|
||||
value=101,
|
||||
value=Decimal("1.01"),
|
||||
is_debit=False,
|
||||
reference="",
|
||||
transaction_date=datetime.now()
|
||||
),
|
||||
Transaction(
|
||||
user_id=0,
|
||||
value=77,
|
||||
value=Decimal("0.77"),
|
||||
is_debit=True,
|
||||
reference="",
|
||||
transaction_date=datetime.now()
|
||||
),
|
||||
])
|
||||
expected_result = 118
|
||||
expected_result = Decimal("1.18")
|
||||
actual_result = await self.accounting_service.get_balance(0)
|
||||
|
||||
self.assertEqual(expected_result, actual_result)
|
||||
|
||||
async def test_trying_to_remove_more_than_is_on_account_balance_raises_exception(self) -> None:
|
||||
user_balance = 100
|
||||
balance_to_remove = 101
|
||||
user_balance = Decimal("1.00")
|
||||
balance_to_remove = Decimal("1.01")
|
||||
self.mock_database_service.get_all_transactions_for_user = AsyncMock(return_value=[
|
||||
Transaction(
|
||||
user_id=0,
|
||||
@@ -88,8 +88,8 @@ class AccountingServiceTests(unittest.IsolatedAsyncioTestCase):
|
||||
await self.accounting_service.remove_balance(0, balance_to_remove, "TestRef")
|
||||
|
||||
async def test_trying_to_remove_less_than_is_on_account_balance_spawns_correct_transaction(self) -> None:
|
||||
user_balance = 101
|
||||
balance_to_remove = 100
|
||||
user_balance = Decimal("1.01")
|
||||
balance_to_remove = Decimal("1.00")
|
||||
reference = "Yey, a reference"
|
||||
|
||||
self.mock_database_service.get_all_transactions_for_user = AsyncMock(return_value=[
|
||||
|
||||
Reference in New Issue
Block a user