Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions sqlalchemy_json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy.ext.mutable import Mutable, MutableDict, MutableList
from sqlalchemy.sql import expression
from sqlalchemy.types import JSON

from .track import TrackedDict, TrackedList
Expand All @@ -19,8 +20,8 @@ class MutableContainer(Mutable):

@classmethod
def coerce(cls, key, value):
if value is None:
return value
if value is None or isinstance(value, expression.Null):
return None
if isinstance(value, cls):
return value
if isinstance(value, dict):
Expand Down Expand Up @@ -56,8 +57,8 @@ class NestedMutableContainer(Mutable):
@classmethod
def coerce(cls, key, value):
"""Convert plain dictionary to NestedMutableContainer."""
if value is None:
return value
if value is None or isinstance(value, expression.Null):
return None
if isinstance(value, cls):
return value
if isinstance(value, dict):
Expand Down
10 changes: 10 additions & 0 deletions test/test_sqlalchemy_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Integer,
Text,
create_engine,
null,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Expand Down Expand Up @@ -178,3 +179,12 @@ def test_mutable_json_list(session, author_with_list):
session.commit()

assert author_with_list.handles == ["@JohnDoe", "JohnDoe", "@mike_bianco"]


def test_null_none(session):
"""
Make sure library can handle both None and null() as JSON value
"""
for value in (None, null()):
author = Author(name="John Doe", handles=value)
Article(author=author.name, content="very important", references=value)
Comment on lines +184 to +190
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having the test fail when either of these fail, parametrize over the different null-like values:

Suggested change
def test_null_none(session):
"""
Make sure library can handle both None and null() as JSON value
"""
for value in (None, null()):
author = Author(name="John Doe", handles=value)
Article(author=author.name, content="very important", references=value)
@pytest.mark.parametrize("null_like", [None, null()])
def test_null_like_assignments(null_like):
"""Make sure library can handle both None and null() as JSON value."""
author = Author(name="John Doe", handles=null_like)
assert author.handles is None