def test_format_field_time_defaults_to_babels_default(self): formatter = i18n.LocalizingFormatter() tzinfo = unittest.mock.Mock() t = datetime.now(tz=pytz.utc).timetz() loc = object() with contextlib.ExitStack() as stack: format_time = stack.enter_context( unittest.mock.patch("babel.dates.format_time") ) s = formatter.format_field(t, "", locale=loc, tzinfo=tzinfo) self.assertSequenceEqual( [ unittest.mock.call(t, locale=loc) ], format_time.mock_calls ) self.assertEqual( format_time(), s )
def test_format_field_date_forwards_to_babel(self): formatter = i18n.LocalizingFormatter() tzinfo = unittest.mock.Mock() d = datetime.now(tz=pytz.utc).date() loc = object() with contextlib.ExitStack() as stack: format_date = stack.enter_context( unittest.mock.patch("babel.dates.format_date") ) s = formatter.format_field(d, "full barbaz", locale=loc, tzinfo=tzinfo) self.assertSequenceEqual( [ unittest.mock.call(d, locale=loc, format="full barbaz") ], format_date.mock_calls ) self.assertEqual( format_date(), s )
def test_localize(self): formatter = i18n.LocalizingFormatter() translator = unittest.mock.Mock() s = "abc {a} def {} ghi {foo}" ls = i18n.LocalizableString(s) with unittest.mock.patch.object(formatter, "vformat") as vformat: ls.localize( formatter, translator, "foobar", a=123, foo=34.5 ) self.assertSequenceEqual( [ unittest.mock.call.gettext(s) ], translator.mock_calls ) self.assertSequenceEqual( [ unittest.mock.call( translator.gettext(), ("foobar",), { "a": 123, "foo": 34.5, }, ) ], vformat.mock_calls )
def test_format_field_timedelta_defaults_to_babels_default(self): formatter = i18n.LocalizingFormatter() tzinfo = unittest.mock.Mock() td = timedelta(seconds=120) loc = object() with contextlib.ExitStack() as stack: format_timedelta = stack.enter_context( unittest.mock.patch("babel.dates.format_timedelta") ) s = formatter.format_field(td, "", locale=loc, tzinfo=tzinfo) self.assertSequenceEqual( [ unittest.mock.call(td, locale=loc) ], format_timedelta.mock_calls ) self.assertEqual( format_timedelta(), s )
def test_localize(self): fmt = i18n.LocalizingFormatter() tx = gettext.NullTranslations() s = unittest.mock.Mock() ue = errors.UserError(s, 10, abc="baz") expected_result = s.localize() s.reset_mock() self.assertEqual( expected_result, ue.localize(fmt, tx) ) self.assertSequenceEqual( [ unittest.mock.call.localize( fmt, tx, 10, abc="baz") ], s.mock_calls )
def test_format_field_datetime_forwards_to_babel_without_tzinfo(self): formatter = i18n.LocalizingFormatter() formatter.tzinfo = unittest.mock.Mock() dt = datetime.utcnow() self.assertIsNone(dt.tzinfo) with contextlib.ExitStack() as stack: format_datetime = stack.enter_context( unittest.mock.patch("babel.dates.format_datetime") ) s = formatter.format_field(dt, "full barbaz") self.assertSequenceEqual( [ unittest.mock.call(dt, locale=formatter.locale, format="full barbaz") ], format_datetime.mock_calls ) self.assertEqual( format_datetime(), s )
def test_localize_plural(self): formatter = i18n.LocalizingFormatter() translator = unittest.mock.Mock() s1 = "{a} thing" sn = "{a} things" ls = i18n.LocalizableString(s1, sn, "a") with contextlib.ExitStack() as stack: vformat = stack.enter_context( unittest.mock.patch.object(formatter, "vformat")) get_field = stack.enter_context( unittest.mock.patch.object(formatter, "get_field")) get_field.return_value = 123, "a" ls.localize(formatter, translator, "foobar", a=123, foo=34.5) self.assertSequenceEqual([ unittest.mock.call("a", ("foobar", ), { "a": 123, "foo": 34.5 }), ], get_field.mock_calls) self.assertSequenceEqual([unittest.mock.call.ngettext(s1, sn, 123)], translator.mock_calls) self.assertSequenceEqual([ unittest.mock.call( translator.ngettext(), ("foobar", ), { "a": 123, "foo": 34.5, }, ) ], vformat.mock_calls)
def test_convert_field_datetime_locale(self): formatter = i18n.LocalizingFormatter() loc = object() tzinfo = unittest.mock.Mock() dt = datetime.now(tz=pytz.utc) with contextlib.ExitStack() as stack: format_datetime = stack.enter_context( unittest.mock.patch("babel.dates.format_datetime") ) s = formatter.convert_field(dt, "s", locale=loc, tzinfo=tzinfo) self.assertSequenceEqual( [ unittest.mock.call(tzinfo.normalize(dt), locale=loc), ], format_datetime.mock_calls ) self.assertEqual( format_datetime(), s )
def test_convert_field_date_repr(self): formatter = i18n.LocalizingFormatter() d = date.today() with unittest.mock.patch("babel.dates.format_date") as format_date: r = formatter.convert_field(d, "r") self.assertSequenceEqual([], format_date.mock_calls) self.assertEqual(repr(d), r)
def test_convert_field_time_repr(self): formatter = i18n.LocalizingFormatter() t = datetime.now(tz=pytz.utc).timetz() with unittest.mock.patch("babel.dates.format_time") as format_time: r = formatter.convert_field(t, "r") self.assertSequenceEqual([], format_time.mock_calls) self.assertEqual(repr(t), r)
def test_convert_field_timedelta_repr(self): formatter = i18n.LocalizingFormatter() td = timedelta(seconds=123) with unittest.mock.patch( "babel.dates.format_timedelta") as format_timedelta: r = formatter.convert_field(td, "r") self.assertSequenceEqual([], format_timedelta.mock_calls) self.assertEqual(repr(td), r)
def test_convert_field_datetime_repr(self): formatter = i18n.LocalizingFormatter() loc = object() dt = datetime.now(tz=pytz.utc) with unittest.mock.patch( "babel.dates.format_datetime") as format_datetime: r = formatter.convert_field(dt, "r", locale=loc) self.assertEqual(repr(dt), r) self.assertSequenceEqual([], format_datetime.mock_calls)
def test_convert_field_time_default_locale(self): formatter = i18n.LocalizingFormatter() t = datetime.now(tz=pytz.utc).timetz() with unittest.mock.patch("babel.dates.format_time") as format_time: s = formatter.convert_field(t, "s") self.assertSequenceEqual([ unittest.mock.call(t, locale=formatter.locale), ], format_time.mock_calls) self.assertEqual(format_time(), s)
def test_convert_field_date_default_locale(self): formatter = i18n.LocalizingFormatter() d = date.today() with unittest.mock.patch("babel.dates.format_date") as format_date: s = formatter.convert_field(d, "s") self.assertSequenceEqual([ unittest.mock.call(d, locale=formatter.locale), ], format_date.mock_calls) self.assertEqual(format_date(), s)
def test_format_works_normally(self): formatter = i18n.LocalizingFormatter() testsets = [ ("abc {} def {:10d} ghi {:.2f}", ("foo", 12, 34.5), {}), ] for fmt, args, kwargs in testsets: self.assertEqual( fmt.format(*args, **kwargs), formatter.format(fmt, *args, **kwargs), )
def test_convert_field_timedelta_default_locale(self): formatter = i18n.LocalizingFormatter() td = timedelta(seconds=123) with unittest.mock.patch( "babel.dates.format_timedelta") as format_timedelta: s = formatter.convert_field(td, "s") self.assertSequenceEqual([ unittest.mock.call(td, locale=formatter.locale), ], format_timedelta.mock_calls) self.assertEqual(format_timedelta(), s)
def test_convert_other_types(self): formatter = i18n.LocalizingFormatter() values = [ 123, 1.2, "foobar", b"barbaz", ] for value in values: self.assertEqual(str(value), formatter.convert_field(value, "s")) for value in values: self.assertEqual(repr(value), formatter.convert_field(value, "r"))
def test_format_field_time_forwards_to_babel_with_defaults(self): formatter = i18n.LocalizingFormatter() formatter.tzinfo = unittest.mock.Mock() t = datetime.now(tz=pytz.utc).timetz() with contextlib.ExitStack() as stack: format_time = stack.enter_context( unittest.mock.patch("babel.dates.format_time")) s = formatter.format_field(t, "full barbaz") self.assertSequenceEqual([ unittest.mock.call( t, locale=formatter.locale, format="full barbaz") ], format_time.mock_calls) self.assertEqual(format_time(), s)
def test_format_field_timedelta_forwards_to_babel_with_defaults(self): formatter = i18n.LocalizingFormatter() formatter.tzinfo = unittest.mock.Mock() td = timedelta(seconds=120) with contextlib.ExitStack() as stack: format_timedelta = stack.enter_context( unittest.mock.patch("babel.dates.format_timedelta")) s = formatter.format_field(td, "full barbaz") self.assertSequenceEqual([ unittest.mock.call( td, locale=formatter.locale, format="full barbaz") ], format_timedelta.mock_calls) self.assertEqual(format_timedelta(), s)
def test_format_field_number_forwards_to_babel_format_decimal(self): formatter = i18n.LocalizingFormatter() v = 123 loc = object() with contextlib.ExitStack() as stack: format_decimal = stack.enter_context( unittest.mock.patch("babel.numbers.format_decimal")) s = formatter.format_field(v, "##.###n", locale=loc) self.assertSequenceEqual([ unittest.mock.call(v, format="##.###", locale=loc), ], format_decimal.mock_calls) self.assertEqual(format_decimal(), s)
def test_format_field_number_uses_format_number_on_empty_format(self): formatter = i18n.LocalizingFormatter() v = 123 loc = object() with contextlib.ExitStack() as stack: format_number = stack.enter_context( unittest.mock.patch("babel.numbers.format_number")) s = formatter.format_field(v, "n", locale=loc) self.assertSequenceEqual([ unittest.mock.call(v, locale=loc), ], format_number.mock_calls) self.assertEqual(format_number(), s)
def test_init_with_explicit_timezone(self): formatter = i18n.LocalizingFormatter(self.foreign_locale, self.foreign_timezone) self.assertEqual(formatter.tzinfo, self.foreign_timezone)
def test_init_default(self): formatter = i18n.LocalizingFormatter() self.assertEqual(formatter.locale, babel.default_locale()) self.assertEqual(formatter.tzinfo, self.local_timezone)