File: test_common_base_class.py

package info (click to toggle)
sqlalchemy-i18n 1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: python: 2,164; makefile: 157
file content (107 lines) | stat: -rw-r--r-- 3,430 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-

import sqlalchemy as sa

from sqlalchemy_i18n import Translatable, translation_base
from sqlalchemy_i18n.manager import BaseTranslationMixin
from tests import ClassicBase, ClassicTestCase, DeclarativeTestCase


class Suite(object):
    def test_translatable_dict_copied_to_each_child_class(self):
        assert (
            self.Article.__translatable__['class'] == self.ArticleTranslation
        )
        assert (
            self.TextItem.__translatable__['class'] == self.TextItemTranslation
        )


class TestDeclarative(Suite, DeclarativeTestCase):
    def create_models(self):
        class Base(self.Model, Translatable):
            __abstract__ = True
            __translatable__ = {
                'locales': ['en', 'fi'],
            }
            locale = 'en'

        class TextItem(Base):
            __tablename__ = 'text_item'

            id = sa.Column(sa.Integer, primary_key=True)

        class TextItemTranslation(translation_base(TextItem)):
            __tablename__ = 'text_item_translation'

            name = sa.Column(sa.Unicode(255))

        class Article(Base):
            __tablename__ = 'article'
            id = sa.Column(
                sa.Integer, primary_key=True
            )

        class ArticleTranslation(translation_base(Article)):
            __tablename__ = 'article_translation'

            name = sa.Column(sa.Unicode(255))

        self.TextItem = TextItem
        self.TextItemTranslation = TextItemTranslation
        self.Article = Article
        self.ArticleTranslation = ArticleTranslation


class TestClassic(Suite, ClassicTestCase):
    def create_tables(self):
        ClassicTestCase.create_tables(self)

        self.textitems = sa.Table(
            'textitems', self.metadata,
            sa.Column('id', sa.Integer,
                      autoincrement=True,
                      primary_key=True,
                      nullable=False))
        self.textitems_translation = sa.Table(
            'textitems_translation', self.metadata,
            sa.Column('id', sa.Integer, sa.ForeignKey('textitems'),
                      primary_key=True,
                      nullable=False),
            sa.Column('locale', sa.types.CHAR(2),
                      primary_key=True,
                      nullable=False),
            sa.Column('name', sa.UnicodeText))

    def create_models(self):
        class CommonBase(ClassicBase, Translatable):
            __translatable__ = {
                'locales': self.locales
            }
            locale = 'en'

        class TextItem(CommonBase):
            __translated_columns__ = [
                sa.Column('name', sa.UnicodeText),
            ]

        class TextItemTranslation(BaseTranslationMixin):
            __parent_class__ = TextItem

        class Article(CommonBase, Translatable):
            __translated_columns__ = [
                sa.Column('content', sa.UnicodeText),
            ]

        class ArticleTranslation(BaseTranslationMixin):
            __parent_class__ = Article

        self.TextItem = TextItem
        self.TextItemTranslation = TextItemTranslation
        self.Article = Article
        self.ArticleTranslation = ArticleTranslation

    def create_mappers(self):
        sa.orm.mapper(self.TextItem, self.textitems)
        sa.orm.mapper(self.TextItemTranslation, self.textitems_translation)
        ClassicTestCase.create_mappers(self)