Skip to content

Commit

Permalink
Fix money formatting when numeric locale could not be inferred
Browse files Browse the repository at this point in the history
On windows numeric locale could not be inffered. In case this happens we
overwrite environment locale with default pl_PL locale
  • Loading branch information
WojtekMs committed Feb 9, 2024
1 parent 610de0e commit e5bf1ca
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ dependencies = [

[project.optional-dependencies] # Optional
dev = [
"pytest"
"pytest",
"pytest-mock"
]

[project.scripts]
Expand Down
15 changes: 15 additions & 0 deletions src/banker/common/locale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging

from babel import default_locale


def get_numeric_locale() -> str:
if not get_numeric_locale.__numeric_locale:
default_numeric_locale = "pl_PL"
logging.getLogger().warning(
f"Numeric locale could not be deduced from the environment, setting {default_numeric_locale} by default")
get_numeric_locale.__numeric_locale = default_numeric_locale
return get_numeric_locale.__numeric_locale


get_numeric_locale.__numeric_locale = default_locale('LC_NUMERIC')
4 changes: 3 additions & 1 deletion src/banker/formatter/html_transactions_formatter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pandas
from moneyed import format_money

from banker.common.locale import get_numeric_locale
from banker.data.transaction import Transaction
from banker.formatter.interfaces.transactions_formatter import ITransactionsFormatter
from banker.common.naming import TRANSACTION_COL_NAME_DATE, TRANSACTION_COL_NAME_VALUE, \
Expand All @@ -14,6 +16,6 @@ def format_transactions(self, transactions: list[Transaction]) -> str:
data[TRANSACTION_COL_NAME_DATE].append(transaction.date)
data[TRANSACTION_COL_NAME_TYPE].append(transaction.type)
data[TRANSACTION_COL_NAME_DESCRIPTION].append(transaction.description)
data[TRANSACTION_COL_NAME_VALUE].append(str(transaction.value))
data[TRANSACTION_COL_NAME_VALUE].append(format_money(transaction.value, locale=get_numeric_locale()))
df = pandas.DataFrame(data=data)
return df.to_html()
Empty file added tests/banker/common/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions tests/banker/common/test_locale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Optional

import pytest

from banker.common.locale import get_numeric_locale


@pytest.fixture
def numeric_locale_patch(mocker):
def _numeric_locale_patch(locale: Optional[str]) -> str:
return mocker.patch('banker.common.locale.get_numeric_locale.__numeric_locale', locale)

return _numeric_locale_patch


def test_given_locale_read_from_environment_when_get_locale_then_return_environment_locale(numeric_locale_patch):
numeric_locale_patch("en_US")
assert get_numeric_locale() == "en_US"
assert get_numeric_locale() == "en_US"
assert get_numeric_locale() == "en_US"


def test_given_no_environment_locale_when_get_locale_then_return_default_locale(numeric_locale_patch):
numeric_locale_patch(None)
assert get_numeric_locale() == "pl_PL"
assert get_numeric_locale() == "pl_PL"
assert get_numeric_locale() == "pl_PL"
7 changes: 5 additions & 2 deletions tests/banker/formatter/test_html_transactions_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def test_given_empty_transactions_when_format_transactions_then_return_empty_tab

def test_given_transactions_when_format_transactions_then_return_html_table_with_headers_and_rows(
html_transactions_formatter_sut, transaction1, transaction2):
html_hard_space = "\xa0"
expected_transaction_1_value = f"-37,35{html_hard_space}zł"
expected_transaction_2_value = f"-200,00{html_hard_space}zł"
expected_result = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: right;">\n'
Expand All @@ -47,14 +50,14 @@ def test_given_transactions_when_format_transactions_then_return_html_table_with
f' <td>{transaction1.date}</td>\n'
f' <td>{transaction1.type}</td>\n'
f' <td>{transaction1.description}</td>\n'
f' <td>{transaction1.value}</td>\n'
f' <td>{expected_transaction_1_value}</td>\n'
' </tr>\n'
' <tr>\n'
' <th>1</th>\n'
f' <td>{transaction2.date}</td>\n'
f' <td>{transaction2.type}</td>\n'
f' <td>{transaction2.description}</td>\n'
f' <td>{transaction2.value}</td>\n'
f' <td>{expected_transaction_2_value}</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
Expand Down

0 comments on commit e5bf1ca

Please sign in to comment.