Skip to content

Commit 16174a6

Browse files
Fix FixedCurrencyMoneyInput.decompress to always return currency
1 parent eb5dfe5 commit 16174a6

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

django_prices/widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, currency, attrs=None):
3333
def decompress(self, value):
3434
if value and isinstance(value, Money):
3535
return [value.amount, self.currency]
36-
return [None, None]
36+
return [None, self.currency]
3737

3838
def render(self, name, value, attrs=None, renderer=None):
3939
widget = super().render(name, value, attrs=attrs, renderer=renderer)

tests/test_forms.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def test_render_fixed_currency_money_input():
3434
assert attr in result
3535

3636

37+
def test_fixed_currency_money_input_decompress_with_none():
38+
"""Test that decompress returns currency even when value is None."""
39+
widget = widgets.FixedCurrencyMoneyInput(currency="USD")
40+
# When value is None, should return [None, 'USD'] not [None, None]
41+
result = widget.decompress(None)
42+
assert result == [None, "USD"], "Currency should be set even when value is None"
43+
44+
45+
def test_fixed_currency_money_input_decompress_with_value():
46+
"""Test that decompress returns correct values when Money object is provided."""
47+
widget = widgets.FixedCurrencyMoneyInput(currency="USD")
48+
result = widget.decompress(Money(10, "USD"))
49+
assert result == [10, "USD"], "Should return amount and currency from Money object"
50+
51+
3752
@pytest.mark.parametrize(
3853
"data,initial,expected_result",
3954
[

0 commit comments

Comments
 (0)