2
0
mirror of https://github.com/openkmip/pykmip synced 2026-01-04 17:43:51 +00:00

Fix tests to pass with SQLAlchemy>=1.4.0

I'm not *entirely* sure what's going on here, but it seems that when we
do something like

    obj = OpaqueObject(...)
    Session = sessionmaker(...)
    session = Session()
    ...
    session.add(obj)
    session.commit()

the primary key (and maybe some foreign relations?) aren't automatically
populated on `obj` following the commit, and will attempt to lazy-load
on next reference. Since expire_on_commit defaults to True, the session
attached to `obj` (which is no longer the `session` in locals!) is closed
out when we later do

    session = Session()
    get_obj = session.query(OpaqueObject).filter(
        ManagedObject.unique_identifier == obj.unique_identifier).one()

leading to a DetachedInstanceError.

There seem to be a few different ways we can fix this:

* Set expire_on_commit=False so the old session is still useful for the
  lazy-loading.
* Re-use the same session instead of creating a new one.
* Explicitly refresh added objects post-commit.

Generally prefer the first one; there's some prior art to follow in
services/server/test_engine.py. Curiously, that same file runs into
trouble despite already setting expire_on_commit=False -- so do the
explicit refresh, on the assumption that there was a reason we went to
the trouble of creating a fresh session.

Closes #649
This commit is contained in:
Tim Burke
2021-09-21 18:16:09 -07:00
committed by Peter Hamilton
parent 6c2bc6b3d5
commit c0c9803956
8 changed files with 47 additions and 43 deletions

View File

@@ -461,7 +461,7 @@ class TestPublicKey(testtools.TestCase):
key = PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1, masks=masks, name=test_name)
Session = sessionmaker(bind=self.engine)
Session = sessionmaker(bind=self.engine, expire_on_commit=False)
session = Session()
session.add(key)
session.commit()
@@ -501,7 +501,7 @@ class TestPublicKey(testtools.TestCase):
expected_mo_names.append(sqltypes.ManagedObjectName(name, i))
self.assertEquals(expected_mo_names, key._names)
Session = sessionmaker(bind=self.engine)
Session = sessionmaker(bind=self.engine, expire_on_commit=False)
session = Session()
session.add(key)
session.commit()
@@ -538,7 +538,7 @@ class TestPublicKey(testtools.TestCase):
self.assertEquals(expected_names, key.names)
self.assertEquals(expected_mo_names, key._names)
Session = sessionmaker(bind=self.engine)
Session = sessionmaker(bind=self.engine, expire_on_commit=False)
session = Session()
session.add(key)
session.commit()
@@ -578,7 +578,7 @@ class TestPublicKey(testtools.TestCase):
self.assertEquals(expected_names, key.names)
self.assertEquals(expected_mo_names, key._names)
Session = sessionmaker(bind=self.engine)
Session = sessionmaker(bind=self.engine, expire_on_commit=False)
session = Session()
session.add(key)
session.commit()
@@ -607,7 +607,7 @@ class TestPublicKey(testtools.TestCase):
key = PublicKey(
enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048,
enums.KeyFormatType.PKCS_1, name=first_name)
Session = sessionmaker(bind=self.engine)
Session = sessionmaker(bind=self.engine, expire_on_commit=False)
session = Session()
session.add(key)
session.commit()
@@ -648,7 +648,7 @@ class TestPublicKey(testtools.TestCase):
key.names.append(names[1])
key.names.append(names[2])
Session = sessionmaker(bind=self.engine)
Session = sessionmaker(bind=self.engine, expire_on_commit=False)
session = Session()
session.add(key)
session.commit()
@@ -691,7 +691,7 @@ class TestPublicKey(testtools.TestCase):
key.names.append(names[1])
key.names.append(names[2])
Session = sessionmaker(bind=self.engine)
Session = sessionmaker(bind=self.engine, expire_on_commit=False)
session = Session()
session.add(key)
session.commit()