Skip to content

Commit

Permalink
Merge pull request #268 from TomGoBravo/fix-issue-85
Browse files Browse the repository at this point in the history
fix issue 85 "TransactionBase.changed_entities fails without TransactionChanges plugin"
  • Loading branch information
marksteward authored Feb 11, 2022
2 parents f3353a7 + 745dc9e commit f1b0faa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
18 changes: 15 additions & 3 deletions sqlalchemy_continuum/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ def compile_big_integer(element, compiler, **kw):
return 'INTEGER'


class NoChangesAttribute(Exception):
pass


class TransactionBase(object):
issued_at = sa.Column(sa.DateTime, default=datetime.utcnow)

@property
def entity_names(self):
"""
Return a list of entity names that changed during this transaction.
Raises a NoChangesAttribute exception if the 'changes' column does
not exist, most likely because TransactionChangesPlugin is not enabled.
"""
return [changes.entity_name for changes in self.changes]
if hasattr(self, 'changes'):
return [changes.entity_name for changes in self.changes]
else:
raise NoChangesAttribute()

@property
def changed_entities(self):
Expand All @@ -48,8 +57,11 @@ def changed_entities(self):
session = sa.orm.object_session(self)

for class_, version_class in tuples:
if class_.__name__ not in self.entity_names:
continue
try:
if class_.__name__ not in self.entity_names:
continue
except NoChangesAttribute:
pass

tx_column = manager.option(class_, 'transaction_column_name')

Expand Down
15 changes: 15 additions & 0 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from sqlalchemy_continuum import versioning_manager
from tests import TestCase
from pytest import mark
from sqlalchemy_continuum.plugins import TransactionMetaPlugin



class TestTransaction(TestCase):
Expand Down Expand Up @@ -38,6 +40,19 @@ def test_repr(self):
repr(transaction)
)

def test_changed_entities(self):
article_v0 = self.article.versions[0]
transaction = article_v0.transaction
assert transaction.changed_entities == {
self.ArticleVersion: [article_v0],
self.TagVersion: [self.article.tags[0].versions[0]],
}


# Check that the tests pass without TransactionChangesPlugin
class TestTransactionWithoutChangesPlugin(TestTransaction):
plugins = [TransactionMetaPlugin()]


class TestAssigningUserClass(TestCase):
user_cls = 'User'
Expand Down

0 comments on commit f1b0faa

Please sign in to comment.