]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Adding setter to should_evaluate_none property
authorsanjana <sanjana0796@gmail.com>
Wed, 13 Feb 2019 15:17:46 +0000 (10:17 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 13 Feb 2019 22:29:23 +0000 (17:29 -0500)
Fixed issue where the :class:`.JSON` type had a read-only
:attr:`.JSON.should_evaluate_none` attribute, which would cause failures
when making use of the :meth:`.TypeEngine.evaluates_none` method in
conjunction with this type.  Pull request courtesy Sanjana S.

Fixes: #4485
Closes: #4496
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4496
Pull-request-sha: 044beb23982d411be6fe640716b1b693df0f7189

Change-Id: I1f3e1d7dec9d2ceb6ccaaa8cac158a062cf02710
(cherry picked from commit 12dad561a77506fe262d791d3135babc0be50e66)

doc/build/changelog/unreleased_12/4485.rst [new file with mode: 0644]
lib/sqlalchemy/sql/sqltypes.py
test/orm/test_unitofworkv2.py
test/sql/test_types.py

diff --git a/doc/build/changelog/unreleased_12/4485.rst b/doc/build/changelog/unreleased_12/4485.rst
new file mode 100644 (file)
index 0000000..dc58604
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+   :tags: bug, sql
+   :tickets: 4485
+
+   Fixed issue where the :class:`.JSON` type had a read-only
+   :attr:`.JSON.should_evaluate_none` attribute, which would cause failures
+   when making use of the :meth:`.TypeEngine.evaluates_none` method in
+   conjunction with this type.  Pull request courtesy Sanjana S.
index 2a3e091f41bac68937243b16d90aeab5ef415182..9e893fd497a07c6c2e2c3124a4c32489b64ff08a 100644 (file)
@@ -2196,8 +2196,13 @@ class JSON(Indexable, TypeEngine):
 
     @property
     def should_evaluate_none(self):
+        """Alias of :attr:`.JSON.none_as_null`"""
         return not self.none_as_null
 
+    @should_evaluate_none.setter
+    def should_evaluate_none(self, value):
+        self.none_as_null = not value
+
     @util.memoized_property
     def _str_impl(self):
         return String(convert_unicode=True)
index 3577802756b2cdd9ded23d4a218cc08d7dbadb1f..84f3640a8c3d9283759e49c0b0e6a473c0790be4 100644 (file)
@@ -1,9 +1,11 @@
+from sqlalchemy import cast
 from sqlalchemy import event
 from sqlalchemy import exc
 from sqlalchemy import FetchedValue
 from sqlalchemy import ForeignKey
 from sqlalchemy import func
 from sqlalchemy import Integer
+from sqlalchemy import JSON
 from sqlalchemy import literal
 from sqlalchemy import select
 from sqlalchemy import String
@@ -2601,6 +2603,20 @@ class NullEvaluatingTest(fixtures.MappedTest, testing.AssertsExecutionResults):
             ),
         )
 
+        if testing.requires.json_type.enabled:
+            Table(
+                "test_has_json",
+                metadata,
+                Column(
+                    "id",
+                    Integer,
+                    primary_key=True,
+                    test_needs_autoincrement=True,
+                ),
+                Column("data", JSON(none_as_null=True).evaluates_none()),
+                Column("data_null", JSON(none_as_null=True)),
+            )
+
     @classmethod
     def setup_classes(cls):
         class Thing(cls.Basic):
@@ -2609,6 +2625,9 @@ class NullEvaluatingTest(fixtures.MappedTest, testing.AssertsExecutionResults):
         class AltNameThing(cls.Basic):
             pass
 
+        class JSONThing(cls.Basic):
+            pass
+
     @classmethod
     def setup_mappers(cls):
         Thing = cls.classes.Thing
@@ -2618,6 +2637,9 @@ class NullEvaluatingTest(fixtures.MappedTest, testing.AssertsExecutionResults):
 
         mapper(AltNameThing, cls.tables.test_w_renames, column_prefix="_foo_")
 
+        if testing.requires.json_type.enabled:
+            mapper(cls.classes.JSONThing, cls.tables.test_has_json)
+
     def _assert_col(self, name, value):
         Thing, AltNameThing = self.classes.Thing, self.classes.AltNameThing
         s = Session()
@@ -2751,3 +2773,14 @@ class NullEvaluatingTest(fixtures.MappedTest, testing.AssertsExecutionResults):
         self._test_bulk_insert_novalue(
             "builtin_evals_null_default", "default_val"
         )
+
+    @testing.requires.json_type
+    def test_json_none_as_null(self):
+        JSONThing = self.classes.JSONThing
+
+        s = Session()
+        f1 = JSONThing(data=None, data_null=None)
+        s.add(f1)
+        s.commit()
+        eq_(s.query(cast(JSONThing.data, String)).scalar(), "null")
+        eq_(s.query(cast(JSONThing.data_null, String)).scalar(), None)
index f113282da0fc50749bd6e85e9dd851d7103b0c9b..b53e9e64263604e91342fd354cc1f3797fe70490 100644 (file)
@@ -247,6 +247,8 @@ class AdaptTest(fixtures.TestBase):
                         or t1.__dict__[k] is None
                     )
 
+            eq_(t1.evaluates_none().should_evaluate_none, True)
+
     def test_python_type(self):
         eq_(types.Integer().python_type, int)
         eq_(types.Numeric().python_type, decimal.Decimal)