|
| 1 | +# Copyright 2024 Camptocamp |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 3 | + |
| 4 | +from odoo.tests.common import RecordCapturer, TransactionCase |
| 5 | + |
| 6 | +from ..models.base_import_import import OPT_USE_QUEUE |
| 7 | + |
| 8 | + |
| 9 | +class TestBaseImportImport(TransactionCase): |
| 10 | + @classmethod |
| 11 | + def setUpClass(cls): |
| 12 | + super().setUpClass() |
| 13 | + cls.res_partners = cls.env["res.partner"] |
| 14 | + cls.import_wizard = cls.env["base_import.import"] |
| 15 | + |
| 16 | + def test_normal_import_res_partners(self): |
| 17 | + values = [ |
| 18 | + [ |
| 19 | + "name", |
| 20 | + "email", |
| 21 | + "is_company", |
| 22 | + ], |
| 23 | + [ |
| 24 | + "partner 1", |
| 25 | + "partner1@example.com", |
| 26 | + "1", |
| 27 | + ], |
| 28 | + [ |
| 29 | + "partner 2", |
| 30 | + "partner2@example.com", |
| 31 | + "0", |
| 32 | + ], |
| 33 | + ] |
| 34 | + import_vals = { |
| 35 | + "res_model": self.res_partners._name, |
| 36 | + "file": "\n".join([";".join(values) for values in values]), |
| 37 | + "file_type": "text/csv", |
| 38 | + } |
| 39 | + self.import_wizard |= self.import_wizard.create(import_vals) |
| 40 | + opts = {"quoting": '"', "separator": ";", "has_headers": True} |
| 41 | + preview = self.import_wizard.parse_preview(opts) |
| 42 | + self.assertEqual( |
| 43 | + preview["matches"], |
| 44 | + { |
| 45 | + 0: ["name"], |
| 46 | + 1: ["email"], |
| 47 | + 2: ["is_company"], |
| 48 | + }, |
| 49 | + ) |
| 50 | + with RecordCapturer(self.res_partners, []) as capture: |
| 51 | + results = self.import_wizard.execute_import( |
| 52 | + [fnames[0] for fnames in preview["matches"].values()], |
| 53 | + [], |
| 54 | + opts, |
| 55 | + ) |
| 56 | + # if result is empty, no import error |
| 57 | + self.assertItemsEqual(results["messages"], []) |
| 58 | + records_created = capture.records |
| 59 | + self.assertEqual(len(records_created), 2) |
| 60 | + self.assertIn("partner1", records_created[0].email) |
| 61 | + |
| 62 | + def test_wrong_import_res_partners(self): |
| 63 | + values = [ |
| 64 | + [ |
| 65 | + "name", |
| 66 | + "email", |
| 67 | + "date", # Adding date field to trigger parsing error |
| 68 | + ], |
| 69 | + [ |
| 70 | + "partner 1", |
| 71 | + "partner1@example.com", |
| 72 | + "21-13-2024", |
| 73 | + ], |
| 74 | + [ |
| 75 | + "partner 2", |
| 76 | + "partner2@example.com", |
| 77 | + "2024-13-45", |
| 78 | + ], |
| 79 | + ] |
| 80 | + opts = { |
| 81 | + "quoting": '"', |
| 82 | + "separator": ";", |
| 83 | + "has_headers": True, |
| 84 | + "date_format": "%Y-%m-%d", # Set specific date format |
| 85 | + OPT_USE_QUEUE: True, |
| 86 | + } |
| 87 | + import_vals = { |
| 88 | + "res_model": self.res_partners._name, |
| 89 | + "file": "\n".join([";".join(row) for row in values]), |
| 90 | + "file_type": "text/csv", |
| 91 | + } |
| 92 | + import_wizard = self.import_wizard.create(import_vals) |
| 93 | + preview = import_wizard.parse_preview(opts) |
| 94 | + results = import_wizard.execute_import( |
| 95 | + [field[0] for field in preview["matches"].values()], |
| 96 | + ["name", "email", "date"], # Include date in fields to import |
| 97 | + opts, |
| 98 | + ) |
| 99 | + self.assertTrue(any(msg["type"] == "error" for msg in results["messages"])) |
0 commit comments