From b140c16ca8f9ed0227f5295878c3f6b346a8472c Mon Sep 17 00:00:00 2001
From: David Szotten <davidszotten@gmail.com>
Date: Wed, 19 Jan 2022 17:53:07 +0000
Subject: [PATCH 1/5] failing test to expose issue
---
djmoney/models/fields.py | 7 ++++++-
docs/changes.rst | 13 +++++++++++--
tests/migrations/helpers.py | 2 +-
tests/test_models.py | 11 ++++++++++-
4 files changed, 28 insertions(+), 5 deletions(-)
--- a/djmoney/models/fields.py
+++ b/djmoney/models/fields.py
@@ -104,7 +104,12 @@ class MoneyFieldProxy:
return data[self.field.name]
def __set__(self, obj, value): # noqa
- if value is not None and self.field._currency_field.null and not isinstance(value, MONEY_CLASSES + (Decimal,)):
+ if (
+ value is not None
+ and self.field._currency_field.null
+ and not isinstance(value, MONEY_CLASSES)
+ and not obj.__dict__[self.currency_field_name]
+ ):
# For nullable fields we need either both NULL amount and currency or both NOT NULL
raise ValueError("Missing currency value")
if isinstance(value, BaseExpression):
--- a/docs/changes.rst
+++ b/docs/changes.rst
@@ -1,6 +1,14 @@
Changelog
=========
+`Unreleased`_ - TBA
+-------------------
+
+**Fixed**
+
+- Improve the internal check for whether a currency is provided `#657`_ (`davidszotten`_)
+- Fix test suite for django main branch `#657`_ (`davidszotten`_)
+
`2.1.1`_ - 2022-01-02
---------------------
@@ -709,8 +717,7 @@ wrapping with ``money_manager``.
- Initial public release
-# .. _Unreleased: https:///github.com/django-money/django-money/compare/2.1.1...HEAD
-
+.. _Unreleased: https:///github.com/django-money/django-money/compare/2.1.1...HEAD
.. _2.1.1: https:///github.com/django-money/django-money/compare/2.1...2.1.1
.. _2.1: https:///github.com/django-money/django-money/compare/2.0.3...2.1
.. _2.0.3: https://github.com/django-money/django-money/compare/2.0.2...2.0.3
@@ -773,6 +780,7 @@ wrapping with ``money_manager``.
.. _0.3: https://github.com/django-money/django-money/compare/0.2...0.3
.. _0.2: https://github.com/django-money/django-money/compare/0.2...a6d90348085332a393abb40b86b5dd9505489b04
+.. _#657: https://github.com/django-money/django-money/issues/657
.. _#648: https://github.com/django-money/django-money/issues/648
.. _#646: https://github.com/django-money/django-money/issues/646
.. _#637: https://github.com/django-money/django-money/issues/637
@@ -966,3 +974,4 @@ wrapping with ``money_manager``.
.. _washeck: https://github.com/washeck
.. _fara: https://github.com/fara
.. _wearebasti: https://github.com/wearebasti
+.. _davidszotten: https://github.com/davidszotten
--- a/tests/migrations/helpers.py
+++ b/tests/migrations/helpers.py
@@ -12,7 +12,7 @@ def makemigrations():
from django.db.migrations import questioner
# We should answer yes for all migrations questioner questions
- questioner.input = lambda x: "y"
+ questioner.input = lambda prompt=None: "y"
os.system("find . -name \\*.pyc -delete")
call_command("makemigrations", "money_app", name=MIGRATION_NAME)
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -5,6 +5,7 @@ Created on May 7, 2011
"""
import datetime
from copy import copy
+from decimal import Decimal
from django import VERSION
from django.core.exceptions import ValidationError
@@ -373,6 +374,12 @@ class TestNullableCurrency:
assert str(exc.value) == "Missing currency value"
assert not ModelWithNullableCurrency.objects.exists()
+ def test_fails_with_null_currency_decimal(self):
+ with pytest.raises(ValueError) as exc:
+ ModelWithNullableCurrency.objects.create(money=Decimal(10))
+ assert str(exc.value) == "Missing currency value"
+ assert not ModelWithNullableCurrency.objects.exists()
+
def test_fails_with_nullable_but_no_default(self):
with pytest.raises(IntegrityError) as exc:
ModelWithTwoMoneyFields.objects.create()
@@ -712,7 +719,9 @@ def test_override_decorator():
def test_properties_access():
with pytest.raises(TypeError) as exc:
ModelWithVanillaMoneyField(money=Money(1, "USD"), bla=1)
- if VERSION[:2] > (2, 1):
+ if VERSION[:2] > (4, 0):
+ assert str(exc.value) == "ModelWithVanillaMoneyField() got unexpected keyword arguments: 'bla'"
+ elif VERSION[:2] > (2, 1):
assert str(exc.value) == "ModelWithVanillaMoneyField() got an unexpected keyword argument 'bla'"
else:
assert str(exc.value) == "'bla' is an invalid keyword argument for this function"