Blame merged_pr_657.patch

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