1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/test_locale_files.py
Stefan Pratter 85bb3d0cd1 fix #444: default filter all in admin views
fix #426: fix issue where asn email validation was case sensitive in some cases
disable test_locale_files as this is now handled by weblate and locale files no longer reside in this repo
2019-03-14 08:27:46 +00:00

70 lines
2.6 KiB
Python

import os
import re
from django.test import TestCase, Client
from django.conf import settings
from peeringdb_server.models import Organization, User
from string import Formatter
class LocaleFilesTest(TestCase):
def load_messages(self, language, filename="django.po"):
path = os.path.join(
os.path.dirname(__file__), "..", "locale", language, "LC_MESSAGES")
with open(os.path.join(path, filename), "r") as fh:
content = fh.read()
message_id = re.findall(r"\nmsgid (.+)\n", content)
message_str = re.findall(r"\nmsgstr (.+)\n", content)
messages = dict(zip(message_id, message_str))
return messages
# weblate handles all this now, and these tests are failing
# atm because the locale files no longer reside here
#
# weblate also makes sure that variable formatting matches, so this
# test is somewhat redundant at this point.
#
# either need to redo this test and make sure it generates the locale
# or remove it.
def _test_pt(self):
"""
Test portuguese locale files
"""
self.assert_variables(
self.load_messages("en_US"), self.load_messages("pt"), "PT")
self.assert_variables(
self.load_messages("en_US", filename="djangojs.po"),
self.load_messages("pt", filename="djangojs.po"), "PT")
def assert_variables(self, en_messages, other_messages, language):
"""
Assert that the correct formatting variables exist
"""
errors = 0
for msgid, msgstr in en_messages.items():
# %(name)s and %s type variables
variables_a = sorted(re.findall("%\([^\(]+\)s|%s", msgid))
variables_b = sorted(
re.findall("%\([^\(]+\)s|%s", other_messages[msgid]))
if variables_a != variables_b:
errors += 1
print "{} Locale variable error at msgid {} -> {}".format(
language, msgid, other_messages[msgid])
# {name} and {} type variables
variables_a = sorted([
fn for _, fn, _, _ in Formatter().parse(msgid)
if fn is not None
])
variables_b = ([
fn for _, fn, _, _ in Formatter().parse(other_messages[msgid])
if fn is not None
])
if variables_a != variables_b:
errors += 1
print "{} Locale variable error at msgid {} -> {}".format(
language, msgid, other_messages[msgid])
assert errors == 0