diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2022-06-15 01:46:39 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2022-06-15 01:46:39 +0000 |
commit | 1156c6f998ed2db0a34c31e34751a062bd0e6cdc (patch) | |
tree | e9b1a13df0e10b654d2ee341573d2ddc9df91b9d /test/ext/test_mutable.py | |
parent | aa8ddf7d356e7ea8c100cca18bb49e3bd820ca31 (diff) | |
parent | 042bccd2e4d6fbfcdf70ede760b29f78771f4b22 (diff) | |
download | sqlalchemy-1156c6f998ed2db0a34c31e34751a062bd0e6cdc.tar.gz |
Merge "pickle mutable parents according to key" into main
Diffstat (limited to 'test/ext/test_mutable.py')
-rw-r--r-- | test/ext/test_mutable.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/test/ext/test_mutable.py b/test/ext/test_mutable.py index 81cecb08b..1ef9afbd8 100644 --- a/test/ext/test_mutable.py +++ b/test/ext/test_mutable.py @@ -4,7 +4,9 @@ import pickle from sqlalchemy import event from sqlalchemy import ForeignKey from sqlalchemy import func +from sqlalchemy import inspect from sqlalchemy import Integer +from sqlalchemy import select from sqlalchemy import String from sqlalchemy import testing from sqlalchemy.ext.mutable import MutableComposite @@ -15,6 +17,7 @@ from sqlalchemy.orm import attributes from sqlalchemy.orm import column_property from sqlalchemy.orm import composite from sqlalchemy.orm import declarative_base +from sqlalchemy.orm import Session from sqlalchemy.orm.instrumentation import ClassManager from sqlalchemy.orm.mapper import Mapper from sqlalchemy.testing import assert_raises @@ -40,6 +43,10 @@ class SubFoo(Foo): pass +class Foo2(fixtures.BasicEntity): + pass + + class FooWithEq: def __init__(self, **kw): for k in kw: @@ -101,6 +108,58 @@ class _MutableDictTestFixture: ClassManager.dispatch._clear() +class MiscTest(fixtures.TestBase): + @testing.combinations(True, False, argnames="pickleit") + def test_pickle_parent_multi_attrs(self, registry, connection, pickleit): + """test #8133""" + + local_foo = Table( + "lf", + registry.metadata, + Column("id", Integer, primary_key=True), + Column("j1", MutableDict.as_mutable(PickleType)), + Column("j2", MutableDict.as_mutable(PickleType)), + Column("j3", MutableDict.as_mutable(PickleType)), + Column("j4", MutableDict.as_mutable(PickleType)), + ) + + registry.map_imperatively(Foo2, local_foo) + registry.metadata.create_all(connection) + + with Session(connection) as sess: + + data = dict( + j1={"a": 1}, + j2={"b": 2}, + j3={"c": 3}, + j4={"d": 4}, + ) + lf = Foo2(**data) + sess.add(lf) + sess.commit() + + all_attrs = {"j1", "j2", "j3", "j4"} + for attr in all_attrs: + for loads, dumps in picklers(): + with Session(connection) as sess: + f1 = sess.scalars(select(Foo2)).first() + if pickleit: + f2 = loads(dumps(f1)) + else: + f2 = f1 + + existing_dict = getattr(f2, attr) + existing_dict["q"] = "c" + eq_( + inspect(f2).attrs[attr].history, + ([existing_dict], (), ()), + ) + for other_attr in all_attrs.difference([attr]): + a = inspect(f2).attrs[other_attr].history + b = ((), [data[other_attr]], ()) + eq_(a, b) + + class _MutableDictTestBase(_MutableDictTestFixture): run_define_tables = "each" |