aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_locale.py
blob: eaeb8e1058c9cf838f92a48e823a7fad35d35208 (plain) (blame)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import datetime
import unittest.mock

import dateutil
import pytest

from nikola.nikola import LEGAL_VALUES
from nikola.utils import (
    LocaleBorg,
    LocaleBorgUninitializedException,
    TranslatableSetting,
)

TESLA_BIRTHDAY = datetime.date(1856, 7, 10)
TESLA_BIRTHDAY_DT = datetime.datetime(1856, 7, 10, 12, 34, 56)
DT_EN_US = "July 10, 1856 at 12:34:56 PM UTC"
DT_PL = "10 lipca 1856 12:34:56 UTC"


@pytest.mark.parametrize("initial_lang", [None, ""])
def test_initilalize_failure(initial_lang):
    with pytest.raises(ValueError):
        LocaleBorg.initialize({}, initial_lang)

    assert not LocaleBorg.initialized


@pytest.mark.parametrize("initial_lang", ["en", "pl"])
def test_initialize(initial_lang):
    LocaleBorg.initialize({}, initial_lang)
    assert LocaleBorg.initialized
    assert LocaleBorg().current_lang == initial_lang


def test_uninitialized_error():
    with pytest.raises(LocaleBorgUninitializedException):
        LocaleBorg()


@pytest.mark.parametrize(
    "locale, expected_current_lang",
    [
        ("pl", "pl"),
        pytest.param(
            "xx", "xx", id="fake language"
        ),  # used to ensure any locale can be supported
    ],
)
def test_set_locale(base_config, locale, expected_current_lang):
    LocaleBorg().set_locale(locale)
    assert LocaleBorg.initialized
    assert LocaleBorg().current_lang == expected_current_lang


def test_set_locale_for_template():
    LocaleBorg.initialize({}, "en")
    assert LocaleBorg().set_locale("xz") == ""  # empty string for template ease of use


def test_format_date_webiso_basic(base_config):
    with unittest.mock.patch("babel.dates.format_datetime") as m:
        formatted_date = LocaleBorg().formatted_date("webiso", TESLA_BIRTHDAY_DT)
        assert formatted_date == "1856-07-10T12:34:56"
        m.assert_not_called()


@pytest.mark.parametrize("lang", ["en", "pl"])
def test_format_date_basic(base_config, lang):
    LocaleBorg.initialize({}, lang)
    formatted_date = LocaleBorg().formatted_date(
        "yyyy-MM-dd HH:mm:ss", TESLA_BIRTHDAY_DT
    )
    assert formatted_date == "1856-07-10 12:34:56"


def test_format_date_long(base_config):
    assert LocaleBorg().formatted_date("long", TESLA_BIRTHDAY_DT) == DT_EN_US
    assert LocaleBorg().formatted_date("long", TESLA_BIRTHDAY_DT, "en") == DT_EN_US
    assert LocaleBorg().formatted_date("long", TESLA_BIRTHDAY_DT, "pl") == DT_PL
    LocaleBorg().set_locale("pl")
    assert LocaleBorg().formatted_date("long", TESLA_BIRTHDAY_DT) == DT_PL
    assert LocaleBorg().formatted_date("long", TESLA_BIRTHDAY_DT, "en") == DT_EN_US


def test_format_date_timezone(base_config):
    tesla_150_birthday_dtz = datetime.datetime(
        2006, 7, 10, 12, 34, 56, tzinfo=dateutil.tz.gettz("America/New_York")
    )
    formatted_date = LocaleBorg().formatted_date("long", tesla_150_birthday_dtz)
    assert formatted_date == "July 10, 2006 at 12:34:56 PM -0400"

    nodst = datetime.datetime(
        2006, 1, 10, 12, 34, 56, tzinfo=dateutil.tz.gettz("America/New_York")
    )
    formatted_date = LocaleBorg().formatted_date("long", nodst)
    assert formatted_date == "January 10, 2006 at 12:34:56 PM -0500"


@pytest.mark.parametrize(
    "english_variant, expected_date",
    [
        pytest.param("en_US", DT_EN_US, id="US"),
        pytest.param("en_GB", "10 July 1856 at 12:34:56 UTC", id="GB"),
    ],
)
def test_format_date_locale_variants(english_variant, expected_date):
    LocaleBorg.initialize({"en": english_variant}, "en")
    assert LocaleBorg().formatted_date("long", TESLA_BIRTHDAY_DT, "en") == expected_date


@pytest.mark.parametrize(
    "lang, expected_string", [("en", "en July"), ("pl", "lipca pl")]
)
def test_format_date_translatablesetting(base_config, lang, expected_string):
    df = TranslatableSetting(
        "DATE_FORMAT", {"en": "'en' MMMM", "pl": "MMMM 'pl'"}, {"en": "", "pl": ""}
    )
    assert LocaleBorg().formatted_date(df, TESLA_BIRTHDAY_DT, lang) == expected_string


@pytest.mark.parametrize(
    "lang, expected_string",
    [
        pytest.param(None, "Foo July Bar", id="default"),
        pytest.param("pl", "Foo lipiec Bar", id="pl"),
    ],
)
def test_format_date_in_string_month(base_config, lang, expected_string):
    formatted_date = LocaleBorg().format_date_in_string(
        "Foo {month} Bar", TESLA_BIRTHDAY, lang
    )
    assert formatted_date == expected_string


@pytest.mark.parametrize(
    "lang, expected_string",
    [
        pytest.param(None, "Foo July 1856 Bar", id="default"),
        pytest.param("pl", "Foo lipiec 1856 Bar", id="pl"),
    ],
)
def test_format_date_in_string_month_year(base_config, lang, expected_string):
    formatted_date = LocaleBorg().format_date_in_string(
        "Foo {month_year} Bar", TESLA_BIRTHDAY, lang
    )
    assert formatted_date == expected_string


@pytest.mark.parametrize(
    "lang, expected_string",
    [
        pytest.param(None, "Foo July 10, 1856 Bar", id="default"),
        pytest.param("pl", "Foo 10 lipca 1856 Bar", id="pl"),
    ],
)
def test_format_date_in_string_month_day_year(base_config, lang, expected_string):
    formatted_date = LocaleBorg().format_date_in_string(
        "Foo {month_day_year} Bar", TESLA_BIRTHDAY, lang
    )
    assert formatted_date == expected_string


@pytest.mark.parametrize(
    "lang, expected_string",
    [
        pytest.param(None, "Foo 10 July 1856 Bar", id="default"),
        pytest.param("pl", "Foo 10 lipca 1856 Bar", id="pl"),
    ],
)
def test_format_date_in_string_month_day_year_gb(lang, expected_string):
    LocaleBorg.initialize({"en": "en_GB"}, "en")
    formatted_date = LocaleBorg().format_date_in_string(
        "Foo {month_day_year} Bar", TESLA_BIRTHDAY, lang
    )
    assert formatted_date == expected_string


@pytest.mark.parametrize(
    "message, expected_string",
    [
        ("Foo {month:'miesiąca' MMMM} Bar", "Foo miesiąca lipca Bar"),
        ("Foo {month_year:MMMM yyyy} Bar", "Foo lipca 1856 Bar"),
    ],
)
def test_format_date_in_string_customization(base_config, message, expected_string):
    formatted_date = LocaleBorg().format_date_in_string(message, TESLA_BIRTHDAY, "pl")
    assert formatted_date == expected_string


@pytest.mark.parametrize(
    "lang, expected_format",
    [("sr", "10. јул 1856. 12:34:56 UTC"), ("sr_latin", "10. jul 1856. 12:34:56 UTC")],
)
def test_locale_base(lang, expected_format):
    LocaleBorg.initialize(LEGAL_VALUES["LOCALES_BASE"], "en")
    formatted_date = LocaleBorg().formatted_date("long", TESLA_BIRTHDAY_DT, lang)
    assert formatted_date == expected_format


@pytest.fixture(autouse=True)
def localeborg_reset():
    """
    Reset the LocaleBorg before and after every test.
    """
    LocaleBorg.reset()
    assert not LocaleBorg.initialized
    try:
        yield
    finally:
        LocaleBorg.reset()
        assert not LocaleBorg.initialized


@pytest.fixture
def base_config():
    """A base config of LocaleBorg."""
    LocaleBorg.initialize({}, "en")