Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tracking dict merging (Python 3.9) #33

Merged
merged 2 commits into from
Mar 13, 2022
Merged
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
4 changes: 4 additions & 0 deletions sqlalchemy_json/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def __init__(self, source=(), **kwds):
self.convert_mapping(source),
self.convert_mapping(kwds)))

def __ior__(self, other):
self.changed('__ior__: %r', other)
return super(TrackedDict, self).__ior__(self.convert(other, self))

def __setitem__(self, key, value):
self.changed('__setitem__: %r=%r', key, value)
super(TrackedDict, self).__setitem__(key, self.convert(value, self))
Expand Down
14 changes: 14 additions & 0 deletions test/test_sqlalchemy_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pickle
import sys

import pytest

Expand Down Expand Up @@ -102,3 +103,16 @@ def test_nested_pickling():
assert one_reloaded["numbers"].parent is one_reloaded
assert two_reloaded["numbers"].parent is two_reloaded
assert one_reloaded is not two_reloaded


@pytest.mark.skipif(sys.version_info < (3, 9), reason="Python 3.9+ required")
def test_dict_merging(session, article):
article.references['github.com'] |= {'someone/somerepo': 10}
session.commit()
assert article.references == {
'github.com': {
'edelooff/sqlalchemy-json': 4,
'zzzeek/sqlalchemy': [1, 2, 3],
'someone/somerepo': 10,
},
}