def test_export_import(self): """ Ensure export+import gives the same result as loading a language """ # load language and generate missing terms to create missing empty terms with mute_logger('coffice.addons.base.models.ir_translation'): self.env["base.language.install"].create({ 'lang': 'fr_FR', 'overwrite': True }).lang_install() self.env["base.update.translations"].create({ 'lang': 'fr_FR' }).act_update() translations = self.env["ir.translation"].search([ ('lang', '=', 'fr_FR'), ('module', '=', 'test_translation_import') ]) # minus 3 as the original fr.po contains 3 fake code translations (cf # test_no_duplicate test) which are not found by babel_extract_terms init_translation_count = len(translations) - 3 module = self.env.ref('base.module_test_translation_import') export = self.env["base.language.export"].create({ 'lang': 'fr_FR', 'format': 'po', 'modules': [(6, 0, [module.id])] }) export.act_getfile() po_file = export.data self.assertIsNotNone(po_file) translations.unlink() import_fr = self.env["base.language.import"].create({ 'name': 'French', 'code': 'fr_FR', 'data': export.data, 'filename': export.name, 'overwrite': False, }) with mute_logger('coffice.addons.base.models.res_lang'): import_fr.with_context(create_empty_translation=True).import_lang() import_translation = self.env["ir.translation"].search([ ('lang', '=', 'fr_FR'), ('module', '=', 'test_translation_import') ]) self.assertEqual(init_translation_count, len(import_translation))
def test_import_from_csv_file(self): """Test the import from a single CSV file works""" with file_open('test_translation_import/i18n/dot.csv', 'rb') as f: po_file = base64.encodestring(f.read()) import_tlh = self.env["base.language.import"].create({ 'name': 'Dothraki', 'code': 'dot', 'data': po_file, 'filename': 'dot.csv', }) with mute_logger('coffice.addons.base.models.res_lang'): import_tlh.import_lang() dot_lang = self.env['res.lang']._lang_get('dot') self.assertTrue(dot_lang, "The imported language was not creates") trans_count = self.env['ir.translation'].search_count([('lang', '=', 'dot')]) self.assertEqual(trans_count, 1, "The imported translations were not created") self.env.context = dict(self.env.context, lang="dot") self.assertEqual(_("Accounting"), "samva", "The code translation was not applied")
def test_lazy_translation(self): """Test the import from a single po file works""" with file_open('test_translation_import/i18n/tlh.po', 'rb') as f: po_file = base64.encodestring(f.read()) import_tlh = self.env["base.language.import"].create({ 'name': 'Klingon', 'code': 'tlh', 'data': po_file, 'filename': 'tlh.po', }) with mute_logger('coffice.addons.base.models.res_lang'): import_tlh.import_lang() context = {'lang': "tlh"} self.assertEqual(_("Klingon"), "tlhIngan", "The direct code translation was not applied") context = None # Comparison of lazy strings must be explicitely casted to string with self.assertRaises(NotImplementedError): TRANSLATED_TERM == "Klingon" self.assertEqual(str(TRANSLATED_TERM), "Klingon", "The translation should not be applied yet") context = {'lang': "tlh"} self.assertEqual(str(TRANSLATED_TERM), "tlhIngan", "The lazy code translation was not applied")
def test_import_from_po_file(self): """Test the import from a single po file works""" with file_open('test_translation_import/i18n/tlh.po', 'rb') as f: po_file = base64.encodestring(f.read()) import_tlh = self.env["base.language.import"].create({ 'name': 'Klingon', 'code': 'tlh', 'data': po_file, 'filename': 'tlh.po', }) with mute_logger('coffice.addons.base.models.res_lang'): import_tlh.import_lang() tlh_lang = self.env['res.lang']._lang_get('tlh') self.assertTrue(tlh_lang, "The imported language was not creates") trans_count = self.env['ir.translation'].search_count([('lang', '=', 'tlh')]) self.assertEqual(trans_count, 1, "The imported translations were not created") self.env.context = dict(self.env.context, lang="tlh") self.assertEqual(_("Klingon"), "tlhIngan", "The code translation was not applied")
def test_followers_no_DID(self): """Test that a follower cannot suffer from dissociative identity disorder. It cannot be both a partner and a channel. """ with self.assertRaises(IntegrityError), mute_logger('coffice.sql_db'): self.env['mail.followers'].create({ 'res_model': self.test_record._name, 'res_id': self.test_record.id, 'partner_id': self.user_employee.partner_id.id, 'channel_id': self.channel_listen.id, })
def test_fields_browse_restriction(self): """Test access to records having restricted fields""" partner = self.env['res.partner'].with_user(self.demo_user) self._set_field_groups(partner, 'email', GROUP_SYSTEM) # accessing fields must no raise exceptions... partner = partner.search([], limit=1) partner.name # ... except if they are restricted with self.assertRaises(AccessError): with mute_logger('coffice.models'): partner.email
def test_02_delete(self): """ Delete: NOT NULL and ON DELETE RESTRICT constraints """ b1 = self.rpc("test_rpc.model_b", "create", {"name": "B1"}) b2 = self.rpc("test_rpc.model_b", "create", {"name": "B2"}) self.rpc("test_rpc.model_a", "create", { "name": "A1", "field_b1": b1, "field_b2": b2 }) try: with mute_logger("coffice.sql_db"): self.rpc("test_rpc.model_b", "unlink", b1) raise except Exception as e: self.assertIn("The operation cannot be completed:", e.faultString) self.assertIn( "another model requires the record being deleted. If possible, archive it instead.", e.faultString, ) self.assertIn( "Model: Model A (test_rpc.model_a), Constraint: test_rpc_model_a_field_b1_fkey", e.faultString, ) # Unlink b2 => ON DELETE RESTRICT constraint raises try: with mute_logger("coffice.sql_db"): self.rpc("test_rpc.model_b", "unlink", b2) raise except Exception as e: self.assertIn("The operation cannot be completed:", e.faultString) self.assertIn( " another model requires the record being deleted. If possible, archive it instead.", e.faultString, ) self.assertIn( "Model: Model A (test_rpc.model_a), Constraint: test_rpc_model_a_field_b2_fkey", e.faultString, )
def test_export_pollution(self): """ Test that exporting the translation only exports the translations of the module """ with file_open('test_translation_import/i18n/dot.csv', 'rb') as f: csv_file = base64.b64encode(f.read()) # dot.csv only contains one term import_tlh = self.env["base.language.import"].create({ 'name': 'Dothraki', 'code': 'dot', 'data': csv_file, 'filename': 'dot.csv', }) with mute_logger('coffice.addons.base.models.res_lang'): import_tlh.import_lang() # create a translation that has the same src as an existing field but no module # information and a different res_id that the real field # this translation should not be included in the export self.env['ir.translation'].create({ 'src': '1XBUO5PUYH2RYZSA1FTLRYS8SPCNU1UYXMEYMM25ASV7JC2KTJZQESZYRV9L8CGB', 'value': '1XBUO5PUYH2RYZSA1FTLRYS8SPCNU1UYXMEYMM25ASV7JC2KTJZQESZYRV9L8CGB in Dothraki', 'type': 'model', 'name': 'ir.model.fields,field_description', 'res_id': -1, 'lang': 'dot', }) module = self.env.ref('base.module_test_translation_import') export = self.env["base.language.export"].create({ 'lang': 'dot', 'format': 'po', 'modules': [(6, 0, [module.id])] }) export.act_getfile() po_file = export.data reader = TranslationFileReader(base64.b64decode(po_file).decode(), fileformat='po') for row in reader: if row['value']: # should contains only one row from the csv, not the manual one self.assertEqual(row['src'], "Accounting") self.assertEqual(row['value'], "samva")
def test_01_create(self): """ Create: mandatory field not provided """ self.rpc("test_rpc.model_b", "create", {"name": "B1"}) try: with mute_logger("coffice.sql_db"): self.rpc("test_rpc.model_b", "create", {}) raise except Exception as e: self.assertIn("The operation cannot be completed:", e.faultString) self.assertIn("Create/update: a mandatory field is not set.", e.faultString) self.assertIn( "Delete: another model requires the record being deleted. If possible, archive it instead.", e.faultString, ) self.assertIn( "Model: Model B (test_rpc.model_b), Field: Name (name)", e.faultString)