| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import io |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from babel.messages import Catalog |
| 8 |
from babel.messages.extract import DEFAULT_KEYWORDS, extract, extract_python |
| 9 |
from babel.messages.mofile import read_mo |
| 10 |
from babel.messages.pofile import read_po |
| 11 |
from tests.benchmarks.helpers import build_catalog, dump_mo, dump_po |
| 12 |
|
| 13 |
MESSAGE_COUNT = 100 |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture() |
| 17 |
def catalog() -> Catalog: |
| 18 |
return build_catalog(MESSAGE_COUNT) |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture() |
| 22 |
def po_source(catalog) -> str: |
| 23 |
return dump_po(catalog) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture() |
| 27 |
def mo_bytes(catalog) -> bytes: |
| 28 |
return dump_mo(catalog) |
| 29 |
|
| 30 |
|
| 31 |
_VIEW_TEMPLATE = ''' |
| 32 |
def view_{i}(request, count): |
| 33 |
title = _("Page title {i}") |
| 34 |
body = gettext("Body text for page {i}") |
| 35 |
# NOTE: shown next to the item counter |
| 36 |
footer = ngettext("%(num)d item", "%(num)d items", count) % {{"num": count}} |
| 37 |
return render(request, title=title, body=body, footer=footer) |
| 38 |
''' |
| 39 |
|
| 40 |
PYTHON_SOURCE = ( |
| 41 |
"from gettext import gettext, ngettext\n\n_ = gettext\n" |
| 42 |
+ "".join(_VIEW_TEMPLATE.format(i=i) for i in range(7)) |
| 43 |
).encode("utf-8") |
| 44 |
|
| 45 |
|
| 46 |
def test_catalog_add(benchmark): |
| 47 |
# Constructed inside the callable, since adding messages mutates the catalog. |
| 48 |
assert benchmark(lambda: len(build_catalog(MESSAGE_COUNT))) == MESSAGE_COUNT |
| 49 |
|
| 50 |
|
| 51 |
def test_catalog_iter(benchmark, catalog): |
| 52 |
# Iteration also yields the header message, hence the + 1. |
| 53 |
assert benchmark(lambda: len(list(catalog))) == MESSAGE_COUNT + 1 |
| 54 |
|
| 55 |
|
| 56 |
def test_catalog_get(benchmark, catalog): |
| 57 |
# A plural ID exercises the tuple branch of the key lookup. |
| 58 |
assert benchmark(lambda: catalog.get(("5 apple", "5 apples")).string) == ( |
| 59 |
"5 omena", |
| 60 |
"5 omenaa", |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
def test_read_po(benchmark, po_source): |
| 65 |
benchmark(lambda: read_po(io.StringIO(po_source), locale="fi")) |
| 66 |
|
| 67 |
|
| 68 |
def test_write_po(benchmark, catalog): |
| 69 |
benchmark(lambda: dump_po(catalog)) |
| 70 |
|
| 71 |
|
| 72 |
def test_write_mo(benchmark, catalog): |
| 73 |
benchmark(lambda: dump_mo(catalog)) |
| 74 |
|
| 75 |
|
| 76 |
def test_read_mo(benchmark, mo_bytes): |
| 77 |
# Fuzzy messages are not written to the MO, so this catalog is smaller. |
| 78 |
catalog = benchmark(lambda: read_mo(io.BytesIO(mo_bytes))) |
| 79 |
assert catalog.get("Message number 42").string == "Viesti numero 42" |
| 80 |
|
| 81 |
|
| 82 |
def test_extract_python(benchmark): |
| 83 |
def run(): |
| 84 |
return list(extract_python(io.BytesIO(PYTHON_SOURCE), DEFAULT_KEYWORDS, ["NOTE:"], {})) |
| 85 |
|
| 86 |
assert len(benchmark(run)) == 21 |
| 87 |
|
| 88 |
|
| 89 |
def test_extract_method_python(benchmark): |
| 90 |
# Same work as above, plus the extraction method lookup/dispatch in extract(). |
| 91 |
def run(): |
| 92 |
return list(extract("python", io.BytesIO(PYTHON_SOURCE), DEFAULT_KEYWORDS, ["NOTE:"])) |
| 93 |
|
| 94 |
assert len(benchmark(run)) == 21 |