| 1 | import unittest |
| 2 | |
| 3 | from odoo.tests import TransactionCase, can_import, loaded_demo_data, tagged |
| 4 | from odoo.tools.misc import file_open |
| 5 | |
| 6 | |
| 7 | @tagged("post_install", "-at_install") |
| 8 | class TestImportFiles(TransactionCase): |
| 9 | |
| 10 | @unittest.skipUnless( |
| 11 | can_import("xlrd.xlsx") or can_import("openpyxl"), "XLRD/XLSX not available", |
| 12 | ) |
| 13 | def test_import_quotation_template_xls(self): |
| 14 | if not loaded_demo_data(self.env): |
| 15 | self.skipTest('Needs demo data to be able to import those files') |
| 16 | model = "sale.order" |
| 17 | filename = "quotations_import_template.xlsx" |
| 18 | |
| 19 | file_content = file_open(f"sale/static/xls/{filename}", "rb").read() |
| 20 | import_wizard = self.env["base_import.import"].create( |
| 21 | { |
| 22 | "res_model": model, |
| 23 | "file": file_content, |
| 24 | "file_type": "application/vnd.ms-excel", |
| 25 | }, |
| 26 | ) |
| 27 | |
| 28 | result = import_wizard.parse_preview( |
| 29 | { |
| 30 | "has_headers": True, |
| 31 | }, |
| 32 | ) |
| 33 | self.assertIsNone(result.get("error")) |
| 34 | field_names = ['/'.join(v) for v in result["matches"].values()] |
| 35 | results = import_wizard.execute_import( |
| 36 | field_names, |
| 37 | [r.lower() for r in result["headers"]], |
| 38 | { |
| 39 | "import_skip_records": [], |
| 40 | "import_set_empty_fields": [], |
| 41 | "fallback_values": {}, |
| 42 | "name_create_enabled_fields": {}, |
| 43 | "encoding": "", |
| 44 | "separator": "", |
| 45 | "quoting": '"', |
| 46 | "date_format": "", |
| 47 | "datetime_format": "", |
| 48 | "float_thousand_separator": ",", |
| 49 | "float_decimal_separator": ".", |
| 50 | "advanced": True, |
| 51 | "has_headers": True, |
| 52 | "keep_matches": False, |
| 53 | "limit": 2000, |
| 54 | "skip": 0, |
| 55 | "tracking_disable": True, |
| 56 | }, |
| 57 | ) |
| 58 | self.assertFalse( |
| 59 | results["messages"], |
| 60 | "results should be empty on successful import of ", |
| 61 | ) |