Skip to content

Commit

Permalink
Improved tests for PO translation modules and fixed a bug in the reader
Browse files Browse the repository at this point in the history
  • Loading branch information
llacroix committed Mar 2, 2023
1 parent 04c4337 commit ef45d68
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
4 changes: 4 additions & 0 deletions odoo_tools/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def get_pot_path(source_name):
return pot_path.exists() and str(pot_path) or False
return False

pot_path = None

# polib accepts a path or the file content as a string, not a fileobj
if isinstance(source, str):
self.pofile = polib.pofile(source)
Expand All @@ -89,6 +91,8 @@ def get_pot_path(source_name):
# PO comments tends to be outdated. See LP bug 933496.)
self.pofile.merge(polib.pofile(pot_path))

self.options = options

def __iter__(self):
for entry in self.pofile:
if entry.obsolete:
Expand Down
130 changes: 130 additions & 0 deletions tests/modules/test_translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from mock import patch, MagicMock
import pytest
from pathlib import Path
from io import BytesIO

from odoo_tools.modules.translate import PoFileReader, PoFileWriter


@pytest.fixture
def modules():
odoo = MagicMock()
return {
"odoo": odoo,
"odoo.release": odoo.release,
}


class MockPOFile(object):
def __init__(self, filename):
self.filename = filename

def __iter__(self):
entry1 = MagicMock()

occurence1 = "model:ir.model,name:account_debit_note.model_account"
code = "code:file.py:0"
sel = "selection:file,model"
# Strict doesn't include this
cons = "sql_constraint:abc"
# will be ignored as invalid
blarg = "whoa"

entry2 = MagicMock()
entry2.obsolete = False
entry2.occurrences = [
(occurence1, 100),
(code, 102),
# will be ignored as double occurence
(code, 103),
(sel, 103),
(cons, 104),
(blarg, 105),
]

entries = [
entry1,
entry2
]

for entry in entries:
yield entry

def merge(self, other):
pass


def test_po_reader():

with patch.object(Path, 'exists') as exists, \
patch('polib.pofile') as pofile, \
patch('polib.POFile', MockPOFile):

pofile.side_effect = MockPOFile

exists.return_value = True

reader = PoFileReader('test.po', options={'read_pot': True})
assert reader.pofile.filename == 'test.po'

entries = [
entry for entry in reader
]

assert len(entries) == 3

# parse po as a buffer
reader = PoFileReader(MagicMock())

# create reader directly with po file
reader = PoFileReader(MockPOFile('test.po'))


def test_po_reader_strict():
with patch.object(Path, 'exists') as exists, \
patch('polib.pofile') as pofile:

pofile.side_effect = MockPOFile

exists.return_value = True

reader = PoFileReader('test.po', options={'strict': True})
assert reader.pofile.filename == 'test.po'

entries = [
entry for entry in reader
]

assert len(entries) == 2


def test_po_writer(modules):
buffer = BytesIO()

with patch.dict('sys.modules', modules):
writer = PoFileWriter(buffer, 'fr_CA')
writer.write_rows([
('base', 'code', 'file.py', '0', 'yes', 'oui', ''),
# If translation is the same as source then don't translate it
('base', 'code', 'file.py', '0', 'no', 'no', ''),
])

writer.merge(MagicMock())

# Create pot file
with patch.dict('sys.modules', modules):
writer = PoFileWriter(buffer, None)
writer.write_rows([
('base', 'code', 'file.py', '0', 'yes', 'oui', ''),
])

writer.merge(MagicMock())

# Create pot file
with patch.dict('sys.modules', modules, pofile=MagicMock()):
writer = PoFileWriter(buffer, None)
writer.write_rows([
('base', 'code', 'file.py', '0', 'yes', 'oui', ''),
])

writer.merge(MagicMock())

0 comments on commit ef45d68

Please sign in to comment.