From 8e57bbb8f8f3d793e5f42e1a6dd3c955358cf947 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sat, 31 Jan 2026 13:49:40 +0100 Subject: [PATCH 1/2] Simplify some tests --- tests/messages/frontend/test_cli.py | 6 +---- tests/test_core.py | 11 +++----- tests/test_date_intervals.py | 41 ++++++++++++++++------------- tests/test_languages.py | 6 ++--- tests/test_localedata.py | 15 ++++++----- tests/test_numbers_parsing.py | 9 ++++--- tests/test_plural.py | 11 ++++---- tests/test_support_translations.py | 16 ++++++----- 8 files changed, 57 insertions(+), 58 deletions(-) diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index 200632ec3..fb085984e 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -59,11 +59,7 @@ def test_usage(cli): with pytest.raises(SystemExit) as ei: cli.run(["pybabel"]) assert ei.value.code == 2 - assert sys.stderr.getvalue().lower() == """\ -usage: pybabel command [options] [args] - -pybabel: error: no valid command or option passed. try the -h/--help option for more information. -""" + assert "error: no valid command or option passed" in sys.stderr.getvalue().lower() def test_list_locales(cli): diff --git a/tests/test_core.py b/tests/test_core.py index 461d70782..4e56250d4 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -78,7 +78,6 @@ def test_hash(): class TestLocaleClass: - def test_attributes(self): locale = Locale('en', 'US') assert locale.language == 'en' @@ -158,8 +157,7 @@ def test_territories_property(self): assert Locale('es', 'CO').territories['DE'] == 'Alemania' def test_variants_property(self): - assert (Locale('de', 'DE').variants['1901'] == - 'Alte deutsche Rechtschreibung') + assert Locale('de', 'DE').variants['1901'] == 'Alte deutsche Rechtschreibung' def test_currencies_property(self): assert Locale('en').currencies['COP'] == 'Colombian Peso' @@ -194,10 +192,9 @@ def test_decimal_formats(self): assert Locale('en', 'US').decimal_formats[None].pattern == '#,##0.###' def test_currency_formats_property(self): - assert (Locale('en', 'US').currency_formats['standard'].pattern == - '\xa4#,##0.00') - assert (Locale('en', 'US').currency_formats['accounting'].pattern == - '\xa4#,##0.00;(\xa4#,##0.00)') + en_us_currency_format = Locale('en', 'US').currency_formats + assert en_us_currency_format['standard'].pattern == '\xa4#,##0.00' + assert en_us_currency_format['accounting'].pattern == '\xa4#,##0.00;(\xa4#,##0.00)' def test_percent_formats_property(self): assert Locale('en', 'US').percent_formats[None].pattern == '#,##0%' diff --git a/tests/test_date_intervals.py b/tests/test_date_intervals.py index c0532c9d2..e9c4c7cd8 100644 --- a/tests/test_date_intervals.py +++ b/tests/test_date_intervals.py @@ -1,5 +1,7 @@ import datetime +import pytest + from babel import dates from babel.util import UTC @@ -8,20 +10,17 @@ TEST_DATE = TEST_DT.date() -def test_format_interval_same_instant_1(): - assert dates.format_interval(TEST_DT, TEST_DT, "yMMMd", fuzzy=False, locale="fi") == "8.1.2016" - - -def test_format_interval_same_instant_2(): - assert dates.format_interval(TEST_DT, TEST_DT, "xxx", fuzzy=False, locale="fi") == "8.1.2016 11.46.15" - - -def test_format_interval_same_instant_3(): - assert dates.format_interval(TEST_TIME, TEST_TIME, "xxx", fuzzy=False, locale="fi") == "11.46.15" - - -def test_format_interval_same_instant_4(): - assert dates.format_interval(TEST_DATE, TEST_DATE, "xxx", fuzzy=False, locale="fi") == "8.1.2016" +@pytest.parametrize( + ("start", "end", "skeleton", "expected"), + [ + (TEST_DT, TEST_DT, "yMMMd", "8.1.2016"), + (TEST_DT, TEST_DT, "xxx", "8.1.2016 11.46.15"), + (TEST_TIME, TEST_TIME, "xxx", "11.46.15"), + (TEST_DATE, TEST_DATE, "xxx", "8.1.2016"), + ], +) +def test_format_interval_same_instant(start, end, skeleton, expected): + assert dates.format_interval(start, end, skeleton, fuzzy=False, locale="fi") == expected def test_format_interval_no_difference(): @@ -34,27 +33,31 @@ def test_format_interval_in_tz(timezone_getter): t1 = TEST_DT.replace(tzinfo=UTC) t2 = t1 + datetime.timedelta(minutes=18) hki_tz = timezone_getter("Europe/Helsinki") - assert dates.format_interval(t1, t2, "Hmv", tzinfo=hki_tz, locale="fi") == "13.46\u201314.04 aikavyöhyke: Suomi" + formatted = dates.format_interval(t1, t2, "Hmv", tzinfo=hki_tz, locale="fi") + assert formatted == "13.46\u201314.04 aikavyöhyke: Suomi" def test_format_interval_12_hour(): t2 = TEST_DT t1 = t2 - datetime.timedelta(hours=1) - assert dates.format_interval(t1, t2, "hm", locale="en") == "10:46\u2009\u2013\u200911:46\u202fAM" + formatted = dates.format_interval(t1, t2, "hm", locale="en") + assert formatted == "10:46\u2009\u2013\u200911:46\u202fAM" def test_format_interval_invalid_skeleton(): t1 = TEST_DATE t2 = TEST_DATE + datetime.timedelta(days=1) - assert dates.format_interval(t1, t2, "mumumu", fuzzy=False, locale="fi") == "8.1.2016\u20139.1.2016" + formatted = dates.format_interval(t1, t2, "mumumu", fuzzy=False, locale="fi") + assert formatted == "8.1.2016\u20139.1.2016" assert dates.format_interval(t1, t2, fuzzy=False, locale="fi") == "8.1.2016\u20139.1.2016" def test_issue_825(): - assert dates.format_timedelta( + formatted = dates.format_timedelta( datetime.timedelta(hours=1), granularity='hour', threshold=100, format='short', locale='pt', - ) == '1 h' + ) + assert formatted == '1 h' diff --git a/tests/test_languages.py b/tests/test_languages.py index 41fcc9e83..4a8b2dc60 100644 --- a/tests/test_languages.py +++ b/tests/test_languages.py @@ -10,7 +10,5 @@ def test_official_languages(): def test_get_language_info(): - assert ( - set(get_territory_language_info("HU")) == - {"hu", "fr", "en", "de", "ro", "hr", "sk", "sl"} - ) + expected = {"hu", "fr", "en", "de", "ro", "hr", "sk", "sl"} + assert set(get_territory_language_info("HU")) == expected diff --git a/tests/test_localedata.py b/tests/test_localedata.py index 03cbed1dc..5962a53cb 100644 --- a/tests/test_localedata.py +++ b/tests/test_localedata.py @@ -14,7 +14,6 @@ import pickle import random import sys -import tempfile import pytest @@ -54,7 +53,10 @@ def test_merge_with_alias_and_resolve(): localedata.merge(d1, d2) assert d1 == {'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14}, 'y': (alias, {'b': 22, 'e': 25})} d = localedata.LocaleDataDict(d1) - assert dict(d.items()) == {'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14}, 'y': {'a': 1, 'b': 22, 'c': 3, 'd': 14, 'e': 25}} + assert dict(d.items()) == { + 'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14}, + 'y': {'a': 1, 'b': 22, 'c': 3, 'd': 14, 'e': 25}, + } def test_load(): @@ -136,16 +138,15 @@ def listdir_spy(*args): assert len(listdir_calls) == 2 -def test_locale_name_cleanup(): +def test_locale_name_cleanup(tmp_path): """ Test that locale identifiers are cleaned up to avoid directory traversal. """ - no_exist_name = os.path.join(tempfile.gettempdir(), "babel%d.dat" % random.randint(1, 99999)) - with open(no_exist_name, "wb") as f: - pickle.dump({}, f) + no_exist_path = tmp_path / f"babel{random.randint(1, 99999):d}.dat" + no_exist_path.write_bytes(pickle.dumps({})) try: - name = os.path.splitext(os.path.relpath(no_exist_name, localedata._dirname))[0] + name = os.path.splitext(os.path.relpath(no_exist_path, localedata._dirname))[0] except ValueError: if sys.platform == "win32": pytest.skip("unable to form relpath") diff --git a/tests/test_numbers_parsing.py b/tests/test_numbers_parsing.py index 0b1d03cad..16eae65aa 100644 --- a/tests/test_numbers_parsing.py +++ b/tests/test_numbers_parsing.py @@ -18,10 +18,11 @@ def test_can_parse_decimals(): - assert decimal.Decimal('1099.98') == numbers.parse_decimal('1,099.98', locale='en_US') - assert decimal.Decimal('1099.98') == numbers.parse_decimal('1.099,98', locale='de') - assert decimal.Decimal('1099.98') == numbers.parse_decimal('1,099.98', locale='ar', numbering_system="default") - assert decimal.Decimal('1099.98') == numbers.parse_decimal('1٬099٫98', locale='ar_EG', numbering_system="default") + v = decimal.Decimal('1099.98') + assert numbers.parse_decimal('1,099.98', locale='en_US') == v + assert numbers.parse_decimal('1.099,98', locale='de') == v + assert numbers.parse_decimal('1,099.98', locale='ar', numbering_system="default") == v # fmt: skip + assert numbers.parse_decimal('1٬099٫98', locale='ar_EG', numbering_system="default") == v # fmt: skip with pytest.raises(numbers.NumberFormatError): numbers.parse_decimal('2,109,998', locale='de') with pytest.raises(numbers.UnsupportedNumberingSystemError): diff --git a/tests/test_plural.py b/tests/test_plural.py index bde356bc6..94d917de4 100644 --- a/tests/test_plural.py +++ b/tests/test_plural.py @@ -13,7 +13,7 @@ import pytest -from babel import localedata, plural +from babel import Locale, localedata, plural EPSILON = decimal.Decimal("0.0001") @@ -67,8 +67,8 @@ def test_plural_other_is_ignored(): def test_to_javascript(): - assert (plural.to_javascript({'one': 'n is 1'}) - == "(function(n) { return (n == 1) ? 'one' : 'other'; })") + src = plural.to_javascript({'one': 'n is 1'}) + assert src == "(function(n) { return (n == 1) ? 'one' : 'other'; })" def test_to_python(): @@ -82,8 +82,8 @@ def test_to_python(): def test_to_gettext(): - assert (plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'}) - == 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);') + src = plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'}) + assert src == 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);' def test_in_range_list(): @@ -133,7 +133,6 @@ def test_plural_within_rules(): def test_locales_with_no_plural_rules_have_default(): - from babel import Locale pf = Locale.parse('ii').plural_form assert pf(1) == 'other' assert pf(2) == 'other' diff --git a/tests/test_support_translations.py b/tests/test_support_translations.py index 7e6dc59f9..bf2eb2b98 100644 --- a/tests/test_support_translations.py +++ b/tests/test_support_translations.py @@ -186,15 +186,19 @@ def get_gettext_method_names(obj): def test_null_translations_have_same_methods(empty_translations, null_translations): for name in get_gettext_method_names(empty_translations): - assert hasattr(null_translations, name), f'NullTranslations does not provide method {name!r}' + assert hasattr(null_translations, name), ( + f'NullTranslations does not provide method {name!r}' + ) -def test_null_translations_method_signature_compatibility(empty_translations, null_translations): +def test_null_translations_method_signature_compatibility( + empty_translations, + null_translations, +): for name in get_gettext_method_names(empty_translations): - assert ( - inspect.getfullargspec(getattr(empty_translations, name)) == - inspect.getfullargspec(getattr(null_translations, name)) - ) + spec1 = inspect.getfullargspec(getattr(empty_translations, name)) + spec2 = inspect.getfullargspec(getattr(null_translations, name)) + assert spec1 == spec2 def test_null_translations_same_return_values(empty_translations, null_translations): From 03419ad5a472742322b2685d3162f0cb3ad29857 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sat, 31 Jan 2026 13:58:42 +0100 Subject: [PATCH 2/2] Add frozen_time fixture --- tests/conftest.py | 9 +++++++ tests/messages/frontend/test_cli.py | 31 ++++++++++-------------- tests/messages/frontend/test_extract.py | 17 +++++-------- tests/messages/frontend/test_init.py | 32 ++++++++++--------------- tests/test_dates.py | 6 ++--- 5 files changed, 41 insertions(+), 54 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index dab67a9a3..8832d864c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,7 @@ +import datetime + import pytest +from freezegun import freeze_time try: import zoneinfo @@ -37,3 +40,9 @@ def timezone_getter(request): pytest.skip("zoneinfo not available") else: raise NotImplementedError + + +@pytest.fixture() +def frozen_time() -> datetime.datetime: + with freeze_time(datetime.frozen_time) as frozen: + yield frozen.time_to_freeze diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index fb085984e..1aeaf986d 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -21,7 +21,6 @@ from io import StringIO import pytest -from freezegun import freeze_time from babel import __version__ as VERSION from babel.dates import format_datetime @@ -111,8 +110,7 @@ def test_help(cli): assert all(command in content for command in ('init', 'update', 'compile', 'extract')) -@freeze_time("1994-11-11") -def test_extract_with_default_mapping(cli, pot_file): +def test_extract_with_default_mapping(frozen_time, cli, pot_file): cli.run([ 'pybabel', 'extract', @@ -122,7 +120,7 @@ def test_extract_with_default_mapping(cli, pot_file): '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', '-o', pot_file, 'project', ]) - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Translations template for TestProject. # Copyright (C) {time.strftime('%Y')} FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -165,8 +163,7 @@ def test_extract_with_default_mapping(cli, pot_file): assert expected_content == pot_file.read_text() -@freeze_time("1994-11-11") -def test_extract_with_mapping_file(cli, pot_file): +def test_extract_with_mapping_file(frozen_time, cli, pot_file): cli.run([ 'pybabel', 'extract', @@ -177,7 +174,7 @@ def test_extract_with_mapping_file(cli, pot_file): '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', '-o', pot_file, 'project', ]) - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Translations template for TestProject. # Copyright (C) {time.strftime('%Y')} FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -214,8 +211,7 @@ def test_extract_with_mapping_file(cli, pot_file): assert expected_content == pot_file.read_text() -@freeze_time("1994-11-11") -def test_extract_with_exact_file(cli, pot_file): +def test_extract_with_exact_file(frozen_time, cli, pot_file): """Tests that we can call extract with a particular file and only strings from that file get extracted. (Note the absence of strings from file1.py) """ @@ -230,7 +226,7 @@ def test_extract_with_exact_file(cli, pot_file): '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', '-o', pot_file, file_to_extract, ]) - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Translations template for TestProject. # Copyright (C) {time.strftime('%Y')} FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -261,8 +257,7 @@ def test_extract_with_exact_file(cli, pot_file): assert expected_content == pot_file.read_text() -@freeze_time("1994-11-11") -def test_init_with_output_dir(cli): +def test_init_with_output_dir(frozen_time, cli): po_file = get_po_file_path('en_US') cli.run([ 'pybabel', @@ -271,7 +266,7 @@ def test_init_with_output_dir(cli): '-d', os.path.join(i18n_dir), '-i', os.path.join(i18n_dir, 'messages.pot'), ]) - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -311,8 +306,7 @@ def test_init_with_output_dir(cli): assert expected_content == actual_content -@freeze_time("1994-11-11") -def test_init_singular_plural_forms(cli): +def test_init_singular_plural_forms(frozen_time, cli): po_file = get_po_file_path('ja_JP') cli.run([ 'pybabel', @@ -321,7 +315,7 @@ def test_init_singular_plural_forms(cli): '-d', os.path.join(i18n_dir), '-i', os.path.join(i18n_dir, 'messages.pot'), ]) - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Japanese (Japan) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -360,8 +354,7 @@ def test_init_singular_plural_forms(cli): assert expected_content == actual_content -@freeze_time("1994-11-11") -def test_init_more_than_2_plural_forms(cli): +def test_init_more_than_2_plural_forms(frozen_time, cli): po_file = get_po_file_path('lv_LV') cli.run([ 'pybabel', @@ -370,7 +363,7 @@ def test_init_more_than_2_plural_forms(cli): '-d', i18n_dir, '-i', os.path.join(i18n_dir, 'messages.pot'), ]) - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Latvian (Latvia) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject diff --git a/tests/messages/frontend/test_extract.py b/tests/messages/frontend/test_extract.py index 712200fbb..dd98a24df 100644 --- a/tests/messages/frontend/test_extract.py +++ b/tests/messages/frontend/test_extract.py @@ -13,10 +13,8 @@ from __future__ import annotations import time -from datetime import datetime import pytest -from freezegun import freeze_time from babel import __version__ as VERSION from babel.dates import format_datetime @@ -100,8 +98,7 @@ def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd, pot_file extract_cmd.finalize_options() -@freeze_time("1994-11-11") -def test_extraction_with_default_mapping(extract_cmd, pot_file): +def test_extraction_with_default_mapping(frozen_time, extract_cmd, pot_file): extract_cmd.copyright_holder = 'FooBar, Inc.' extract_cmd.msgid_bugs_address = 'bugs.address@email.tld' extract_cmd.output_file = pot_file @@ -110,7 +107,7 @@ def test_extraction_with_default_mapping(extract_cmd, pot_file): extract_cmd.finalize_options() extract_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Translations template for TestProject. # Copyright (C) {time.strftime('%Y')} FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -153,8 +150,7 @@ def test_extraction_with_default_mapping(extract_cmd, pot_file): assert expected_content == pot_file.read_text() -@freeze_time("1994-11-11") -def test_extraction_with_mapping_file(extract_cmd, pot_file): +def test_extraction_with_mapping_file(frozen_time, extract_cmd, pot_file): extract_cmd.copyright_holder = 'FooBar, Inc.' extract_cmd.msgid_bugs_address = 'bugs.address@email.tld' extract_cmd.mapping_file = 'mapping.cfg' @@ -164,7 +160,7 @@ def test_extraction_with_mapping_file(extract_cmd, pot_file): extract_cmd.finalize_options() extract_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Translations template for TestProject. # Copyright (C) {time.strftime('%Y')} FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -201,9 +197,8 @@ def test_extraction_with_mapping_file(extract_cmd, pot_file): assert expected_content == pot_file.read_text() -@freeze_time("1994-11-11") @pytest.mark.parametrize("ignore_pattern", ['**/ignored/**.*', 'ignored']) -def test_extraction_with_mapping_dict(extract_cmd, pot_file, ignore_pattern): +def test_extraction_with_mapping_dict(frozen_time, extract_cmd, pot_file, ignore_pattern): extract_cmd.distribution.message_extractors = { 'project': [ (ignore_pattern, 'ignore', None), @@ -218,7 +213,7 @@ def test_extraction_with_mapping_dict(extract_cmd, pot_file, ignore_pattern): extract_cmd.finalize_options() extract_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Translations template for TestProject. # Copyright (C) {time.strftime('%Y')} FooBar, Inc. # This file is distributed under the same license as the TestProject diff --git a/tests/messages/frontend/test_init.py b/tests/messages/frontend/test_init.py index e69e5cce3..2f5c6043c 100644 --- a/tests/messages/frontend/test_init.py +++ b/tests/messages/frontend/test_init.py @@ -14,10 +14,8 @@ import os import shutil -from datetime import datetime import pytest -from freezegun import freeze_time from babel import __version__ as VERSION from babel.dates import format_datetime @@ -59,8 +57,7 @@ def test_no_locale(init_cmd): init_cmd.finalize_options() -@freeze_time("1994-11-11") -def test_with_output_dir(init_cmd): +def test_with_output_dir(frozen_time, init_cmd): init_cmd.input_file = 'project/i18n/messages.pot' init_cmd.locale = 'en_US' init_cmd.output_dir = 'project/i18n' @@ -68,7 +65,7 @@ def test_with_output_dir(init_cmd): init_cmd.finalize_options() init_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -108,8 +105,7 @@ def test_with_output_dir(init_cmd): assert expected_content == actual_content -@freeze_time("1994-11-11") -def test_keeps_catalog_non_fuzzy(init_cmd): +def test_keeps_catalog_non_fuzzy(frozen_time, init_cmd): init_cmd.input_file = 'project/i18n/messages_non_fuzzy.pot' init_cmd.locale = 'en_US' init_cmd.output_dir = 'project/i18n' @@ -117,7 +113,7 @@ def test_keeps_catalog_non_fuzzy(init_cmd): init_cmd.finalize_options() init_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -157,8 +153,7 @@ def test_keeps_catalog_non_fuzzy(init_cmd): assert expected_content == actual_content -@freeze_time("1994-11-11") -def test_correct_init_more_than_2_plurals(init_cmd): +def test_correct_init_more_than_2_plurals(frozen_time, init_cmd): init_cmd.input_file = 'project/i18n/messages.pot' init_cmd.locale = 'lv_LV' init_cmd.output_dir = 'project/i18n' @@ -166,7 +161,7 @@ def test_correct_init_more_than_2_plurals(init_cmd): init_cmd.finalize_options() init_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') expected_content = fr"""# Latvian (Latvia) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -208,8 +203,7 @@ def test_correct_init_more_than_2_plurals(init_cmd): assert expected_content == actual_content -@freeze_time("1994-11-11") -def test_correct_init_singular_plural_forms(init_cmd): +def test_correct_init_singular_plural_forms(frozen_time, init_cmd): init_cmd.input_file = 'project/i18n/messages.pot' init_cmd.locale = 'ja_JP' init_cmd.output_dir = 'project/i18n' @@ -217,7 +211,7 @@ def test_correct_init_singular_plural_forms(init_cmd): init_cmd.finalize_options() init_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='ja_JP') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='ja_JP') expected_content = fr"""# Japanese (Japan) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -256,8 +250,7 @@ def test_correct_init_singular_plural_forms(init_cmd): assert expected_content == actual_content -@freeze_time("1994-11-11") -def test_supports_no_wrap(init_cmd): +def test_supports_no_wrap(frozen_time, init_cmd): init_cmd.input_file = 'project/i18n/long_messages.pot' init_cmd.locale = 'en_US' init_cmd.output_dir = 'project/i18n' @@ -274,7 +267,7 @@ def test_supports_no_wrap(init_cmd): init_cmd.finalize_options() init_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US') expected_content = fr"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -314,8 +307,7 @@ def test_supports_no_wrap(init_cmd): assert expected_content == actual_content -@freeze_time("1994-11-11") -def test_supports_width(init_cmd): +def test_supports_width(frozen_time, init_cmd): init_cmd.input_file = 'project/i18n/long_messages.pot' init_cmd.locale = 'en_US' init_cmd.output_dir = 'project/i18n' @@ -331,7 +323,7 @@ def test_supports_width(init_cmd): init_cmd.finalize_options() init_cmd.run() - date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US') + date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US') expected_content = fr"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject diff --git a/tests/test_dates.py b/tests/test_dates.py index 12bb23433..4e04ff146 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -753,10 +753,8 @@ def test_zh_TW_format(): assert dates.format_time(datetime(2016, 4, 8, 12, 34, 56), locale='zh_TW') == '中午12:34:56' -def test_format_current_moment(): - frozen_instant = datetime.now(UTC) - with freezegun.freeze_time(time_to_freeze=frozen_instant): - assert dates.format_datetime(locale="en_US") == dates.format_datetime(frozen_instant, locale="en_US") +def test_format_current_moment(frozen_time): + assert dates.format_datetime(locale="en_US") == dates.format_datetime(frozen_time, locale="en_US") @pytest.mark.all_locales