File: test_integration.py

package info (click to toggle)
flask-babel 4.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 316 kB
  • sloc: python: 885; sh: 17; makefile: 14
file content (164 lines) | stat: -rw-r--r-- 4,317 bytes parent folder | download | duplicates (2)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pickle

import flask
from babel.support import NullTranslations

import flask_babel as babel
from flask_babel import get_translations, gettext, lazy_gettext


def test_no_request_context():
    b = babel.Babel()
    app = flask.Flask(__name__)
    b.init_app(app)

    with app.app_context():
        assert isinstance(get_translations(), NullTranslations)


def test_multiple_directories():
    """
    Ensure we can load translations from multiple directories.

    This also ensures that directories without any translation files
    are not taken into account.
    """
    b = babel.Babel()
    app = flask.Flask(__name__)

    app.config.update(
        {
            "BABEL_TRANSLATION_DIRECTORIES": ";".join(
                ("translations", "renamed_translations")
            ),
            "BABEL_DEFAULT_LOCALE": "de_DE",
        }
    )

    b.init_app(app)

    with app.test_request_context():
        translations = sorted(b.list_translations(), key=str)

        assert len(translations) == 4
        assert str(translations[0]) == "de"
        assert str(translations[1]) == "de"
        assert str(translations[2]) == "de_DE"
        assert str(translations[3]) == "ja"

        assert gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!"


def test_multiple_directories_multiple_domains():
    """
    Ensure we can load translations from multiple directories with a
    custom domain.
    """
    b = babel.Babel()
    app = flask.Flask(__name__)

    app.config.update(
        {
            "BABEL_TRANSLATION_DIRECTORIES": ";".join(
                (
                    "renamed_translations",
                    "translations_different_domain",
                )
            ),
            "BABEL_DEFAULT_LOCALE": "de_DE",
            "BABEL_DOMAIN": ";".join(
                (
                    "messages",
                    "myapp",
                )
            ),
        }
    )

    b.init_app(app)

    with app.test_request_context():
        translations = b.list_translations()

        assert len(translations) == 3
        assert str(translations[0]) == "de"
        assert str(translations[1]) == "de"
        assert str(translations[2]) == "de_DE"

        assert gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!"
        assert gettext("Good bye") == "Auf Wiedersehen"


def test_multiple_directories_different_domain():
    """
    Ensure we can load translations from multiple directories with a
    custom domain.
    """
    b = babel.Babel()
    app = flask.Flask(__name__)

    app.config.update(
        {
            "BABEL_TRANSLATION_DIRECTORIES": ";".join(
                ("translations_different_domain", "renamed_translations")
            ),
            "BABEL_DEFAULT_LOCALE": "de_DE",
            "BABEL_DOMAIN": "myapp",
        }
    )

    b.init_app(app)

    with app.test_request_context():
        translations = b.list_translations()

        assert len(translations) == 3
        assert str(translations[0]) == "de"
        assert str(translations[1]) == "de"
        assert str(translations[2]) == "de_DE"

        assert gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!"
        assert gettext("Good bye") == "Auf Wiedersehen"


def test_different_domain():
    """
    Ensure we can load translations from multiple directories.
    """
    b = babel.Babel()
    app = flask.Flask(__name__)

    app.config.update(
        {
            "BABEL_TRANSLATION_DIRECTORIES": "translations_different_domain",
            "BABEL_DEFAULT_LOCALE": "de_DE",
            "BABEL_DOMAIN": "myapp",
        }
    )

    b.init_app(app)

    with app.test_request_context():
        translations = b.list_translations()

        assert len(translations) == 2
        assert str(translations[0]) == "de"
        assert str(translations[1]) == "de_DE"

        assert gettext("Good bye") == "Auf Wiedersehen"


def test_lazy_old_style_formatting():
    lazy_string = lazy_gettext("Hello %(name)s")
    assert lazy_string % {"name": "test"} == "Hello test"

    lazy_string = lazy_gettext("test")
    assert "Hello %s" % lazy_string == "Hello test"


def test_lazy_pickling():
    lazy_string = lazy_gettext("Foo")
    pickled = pickle.dumps(lazy_string)
    unpickled = pickle.loads(pickled)

    assert unpickled == lazy_string