-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#24): Allow tables in get_versioning_manager()
Add versioning related information to Tables and Models. Now, Tables will also have: - __versioning_manager__ - So we can keep track of the manager used for a table. - __version_parent__ - So we can keep track of the parent Using this, we can now allow table in get_versioning_manager() Also add unittests for the function
- Loading branch information
1 parent
1e7e561
commit 37e5ae3
Showing
3 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from copy import copy | ||
from pytest import raises | ||
import sqlalchemy as sa | ||
from sqlalchemy_history import versioning_manager | ||
from sqlalchemy_history.exc import ClassNotVersioned | ||
from sqlalchemy_history.utils import get_versioning_manager | ||
|
||
from tests import TestCase | ||
|
||
|
||
class TestGetVersioningManager(TestCase): | ||
def create_models(self): | ||
""" | ||
Creates many-to-many relationship between Article and Tag | ||
Article is versioned. But Tag is not versioned | ||
""" | ||
|
||
class Article(self.Model): | ||
__tablename__ = "article" | ||
__versioned__ = copy(self.options) | ||
|
||
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True) | ||
name = sa.Column(sa.Unicode(255)) | ||
|
||
article_tag = sa.Table( | ||
"article_tag", | ||
self.Model.metadata, | ||
sa.Column( | ||
"article_id", | ||
sa.Integer, | ||
sa.ForeignKey("article.id", ondelete="CASCADE"), | ||
primary_key=True, | ||
), | ||
sa.Column("tag_id", sa.Integer, sa.ForeignKey("tag.id", ondelete="CASCADE"), primary_key=True), | ||
) | ||
|
||
class Tag(self.Model): | ||
__tablename__ = "tag" | ||
|
||
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True) | ||
name = sa.Column(sa.Unicode(255)) | ||
articles = sa.orm.relationship(Article, secondary=article_tag, backref="tags") | ||
|
||
self.Article = Article | ||
self.article_tag = article_tag | ||
self.Tag = Tag | ||
|
||
def test_parent_class(self): | ||
assert get_versioning_manager(self.Article) == versioning_manager | ||
|
||
def test_parent_table(self): | ||
assert get_versioning_manager(self.Article.__table__) == versioning_manager | ||
|
||
def test_version_class(self): | ||
assert get_versioning_manager(self.ArticleVersion) == versioning_manager | ||
|
||
def test_version_table(self): | ||
assert get_versioning_manager(self.ArticleVersion.__table__) == versioning_manager | ||
|
||
def test_association_table(self): | ||
assert get_versioning_manager(self.article_tag) == versioning_manager | ||
|
||
def test_aliased_class(self): | ||
assert get_versioning_manager(sa.orm.aliased(self.Article)) == versioning_manager | ||
assert get_versioning_manager(sa.orm.aliased(self.ArticleVersion)) == versioning_manager | ||
|
||
def test_unknown_class(self): | ||
with raises(ClassNotVersioned): | ||
get_versioning_manager(self.Tag) | ||
|
||
def test_unknown_table(self): | ||
with raises(ClassNotVersioned): | ||
get_versioning_manager(self.Tag.__table__) |