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('flectra.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'), ('value', '!=', ''), ]) # 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('flectra.addons.base.models.res_lang'): import_fr.import_lang() import_translation = self.env["ir.translation"].search([ ('lang', '=', 'fr_FR'), ('module', '=', 'test_translation_import'), ('value', '!=', ''), ]) self.assertEqual(init_translation_count, len(import_translation))
def test_export_import_csv(self): """ Ensure can reimport exported csv """ self.env.ref("base.lang_fr").active = True module = self.env.ref('base.module_test_translation_import') export = self.env["base.language.export"].create({ 'lang': 'fr_FR', 'format': 'csv', 'modules': [(6, 0, [module.id])] }) export.act_getfile() po_file = export.data self.assertIsNotNone(po_file) self.env["ir.translation"].search([ ('lang', '=', 'fr_FR'), ('module', '=', 'test_translation_import') ]).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('flectra.addons.base.models.res_lang'): import_fr.with_context(create_empty_translation=True).import_lang()
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.encodebytes(f.read()) import_tlh = self.env["base.language.import"].create({ 'name': 'Dothraki', 'code': 'dot', 'data': po_file, 'filename': 'dot.csv', }) with mute_logger('flectra.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_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.encodebytes(f.read()) import_tlh = self.env["base.language.import"].create({ 'name': 'Klingon', 'code': 'tlh', 'data': po_file, 'filename': 'tlh.po', }) with mute_logger('flectra.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_fields_browse_restriction(self): """Test access to records having restricted fields""" partner = self.env['res.partner'].sudo(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('flectra.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("flectra.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("flectra.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('flectra.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_no_DID(self): """Test that a follower cannot suffer from dissociative identity disorder. It cannot be both a partner and a channel. """ test_record = self.env['mail.channel'].create({ 'name': 'I used to be schizo, but now we are alright.' }) test_channel = self.env['mail.channel'].create({'name': 'Follower Channel'}) with self.assertRaises(IntegrityError), mute_logger('flectra.sql_db'): self.env['mail.followers'].create({ 'res_model': test_record._name, 'res_id': test_record.id, 'partner_id': self.user_employee.partner_id.id, 'channel_id': test_channel.id, })
def test_fields_browse_restriction(self): """Test access to records having restricted fields""" # Invalidate cache to avoid restricted value to be available # in the cache self.user_demo.invalidate_cache() partner = self.env['res.partner'].with_user(self.user_demo) 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('flectra.models'): partner.email
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('flectra.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_01_create(self): """ Create: mandatory field not provided """ self.rpc("test_rpc.model_b", "create", {"name": "B1"}) try: with mute_logger("flectra.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)
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.encodebytes(f.read()) import_tlh = self.env["base.language.import"].create({ 'name': 'Klingon', 'code': 'tlh', 'data': po_file, 'filename': 'tlh.po', }) with mute_logger('flectra.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") self.assertEqual("Do you speak " + TRANSLATED_TERM, "Do you speak tlhIngan", "str + _lt concatenation failed") self.assertEqual(TRANSLATED_TERM + ", I speak it", "tlhIngan, I speak it", "_lt + str concatenation failed") self.assertEqual(TRANSLATED_TERM + TRANSLATED_TERM, "tlhIngantlhIngan", "_lt + _lt concatenation failed")