From 8d711086602fa68a6e30d27bf08a274cabf5dcb0 Mon Sep 17 00:00:00 2001 From: Kevin Khao Date: Tue, 17 Oct 2023 08:41:00 +0300 Subject: [PATCH] wip --- wms_connector/data/ir_filters.xml | 13 +++++- wms_connector/models/stock_picking.py | 5 --- wms_connector/models/stock_warehouse.py | 22 +++++----- .../models/synchronize_exportable_mixin.py | 2 +- wms_connector/security/wms_product_sync.xml | 5 +-- wms_connector/tests/common.py | 37 ++++++++++++----- wms_connector/tests/model.py | 15 +++++++ wms_connector/tests/test_activate_sync.py | 4 +- wms_connector/tests/test_export.py | 40 ++++++++++++++++--- 9 files changed, 102 insertions(+), 41 deletions(-) diff --git a/wms_connector/data/ir_filters.xml b/wms_connector/data/ir_filters.xml index a406998d4fe..1734570e406 100644 --- a/wms_connector/data/ir_filters.xml +++ b/wms_connector/data/ir_filters.xml @@ -6,16 +6,25 @@ WMS: Default filter for products product.product + [ + ("wms_export_date", "=", False) + ] WMS: Default filter for picking in stock.picking - [("picking_type_id.code", "=", "incoming")] + [ + ("wms_export_date", "=", False), + ("picking_type_id.code", "=", "incoming") + ] WMS: Default filter picking out stock.picking - [("picking_type_id.code", "=", "outgoing")] + [ + ("wms_export_date", "=", False), + ("picking_type_id.code", "=", "outgoing") + ] diff --git a/wms_connector/models/stock_picking.py b/wms_connector/models/stock_picking.py index 35b55109253..80bd9720c9d 100644 --- a/wms_connector/models/stock_picking.py +++ b/wms_connector/models/stock_picking.py @@ -7,8 +7,3 @@ class StockPicking(models.Model): _inherit = ["stock.picking", "synchronize.exportable.mixin"] _name = "stock.picking" - - def _get_export_name(self): - if self.file_creation_mode == "per_record": - return self.name + ".csv" - return super()._get_export_name() diff --git a/wms_connector/models/stock_warehouse.py b/wms_connector/models/stock_warehouse.py index 41078ebeaa2..5cd29c8fe99 100644 --- a/wms_connector/models/stock_warehouse.py +++ b/wms_connector/models/stock_warehouse.py @@ -15,9 +15,9 @@ "filetype": "export", "name_fragment": "exports (products, awaiting receptions, preparation orders", "code": "wh = env['stock.warehouse'].browse({0})\n" - 'wh.{1}.scheduler_export("model",wh["taskfield"]._get_eval_domain())\n' - 'wh.{1}.scheduler_export("model",wh["taskfield"]._get_eval_domain())\n' - 'wh.{1}.scheduler_export("model",wh["taskfield"]._get_eval_domain())', + 'wh.{1}.scheduler_export("wms.product.sync", wh["wms_export_product_filter_id"]._get_eval_domain())\n' + 'wh.{1}.scheduler_export("stock.picking", wh["wms_export_picking_in_filter_id"]._get_eval_domain())\n' + 'wh.{1}.scheduler_export("stock.picking", wh["wms_export_picking_out_filter_id"]._get_eval_domain())', }, "reception": { "fieldname_task": "wms_import_confirm_reception_task_id", @@ -82,7 +82,7 @@ def _activate_crons_tasks(self): "attachment.synchronize.task" ].create( rec._prepare_wms_task_vals( - mappings["name_fragment"], mappings["filetype"] + mappings["filetype"], mappings["name_fragment"] ) ) cron_field_name = mappings["fieldname_cron"] @@ -91,12 +91,8 @@ def _activate_crons_tasks(self): cron.active = True else: code = mappings["code"].format(self.id, task_field_name) - if kind == "export": - for el in MAPPINGS_FILTERS: - code = code.replace("taskfield", el[0], 1) - code = code.replace("model", el[1], 1) rec[cron_field_name] = self.env["ir.cron"].create( - rec._prepare_wms_cron_vals(mappings["name_fragment"], code) + rec._prepare_wms_cron_vals(code, mappings["name_fragment"]) ) for field in [fieldname for fieldname, _ in MAPPINGS_FILTERS]: if not getattr(rec, field): @@ -104,18 +100,18 @@ def _activate_crons_tasks(self): "wms_connector.default_{}".format(field[:-3]) ) - def _prepare_wms_task_vals(self, name, filetype): + def _prepare_wms_task_vals(self, filetype, name_fragment=""): return { - "name": "WMS task for {} ({})".format(self.name, name), + "name": "WMS task for {} {}".format(self.name, name_fragment), "method_type": "export", "filepath": "OUT/", "backend_id": self.env.ref("storage_backend.default_storage_backend").id, "file_type": filetype, } - def _prepare_wms_cron_vals(self, name, code=""): + def _prepare_wms_cron_vals(self, code="", name_fragment=""): return { - "name": "WMS cron for {} ({})".format(self.name, name), + "name": "WMS cron for {} {}".format(self.name, name_fragment), "active": False, "interval_type": "days", "interval_number": 1, diff --git a/wms_connector/models/synchronize_exportable_mixin.py b/wms_connector/models/synchronize_exportable_mixin.py index 776da48315d..b4b9f1e4d4b 100644 --- a/wms_connector/models/synchronize_exportable_mixin.py +++ b/wms_connector/models/synchronize_exportable_mixin.py @@ -9,7 +9,7 @@ from odoo import fields, models -DEBUGMODE = True +DEBUGMODE = False class SynchronizeExportableMixin(models.AbstractModel): diff --git a/wms_connector/security/wms_product_sync.xml b/wms_connector/security/wms_product_sync.xml index 6431f358c7d..cdcc77bc25c 100644 --- a/wms_connector/security/wms_product_sync.xml +++ b/wms_connector/security/wms_product_sync.xml @@ -4,10 +4,9 @@ - - wms.product.sync access name + + wms.product.sync access name - diff --git a/wms_connector/tests/common.py b/wms_connector/tests/common.py index a507333a5cb..65a981c3e43 100644 --- a/wms_connector/tests/common.py +++ b/wms_connector/tests/common.py @@ -4,36 +4,55 @@ from odoo.tests.common import SavepointCase import uuid from odoo_test_helper import FakeModelLoader -from odoo.addons.component.tests.common import SavepointComponentCase +from odoo.addons.component.tests.common import ComponentMixin +from odoo.addons.attachment_synchronize.tests.common import SyncCommon +from odoo.addons.base.tests.common import SavepointCase +import datetime -class WmsConnectorCommon(SavepointComponentCase): +class WmsConnectorCommon(ComponentMixin, SavepointCase): + # pylint: disable=W8106 + def setUp(self): + self.registry.enter_test_mode(self.env.cr) + super().setUp() + ComponentMixin.setUp(self) + + def setAllExported(self): + self.env["stock.picking"].search([]).wms_export_date = datetime.date.today() + self.env["wms.product.sync"].search([]).wms_export_date = datetime.date.today() + @classmethod def setUpClass(cls): super().setUpClass() + cls.setUpComponent() cls.backend = cls.env.ref("wms_connector.demo_wms_backend") cls.backend.directory_path = str(uuid.uuid1()) + "/" cls.aq_before = cls.env["attachment.queue"].search([]) cls.warehouse = cls.env.ref("stock.warehouse0") cls.loader = FakeModelLoader(cls.env, cls.__module__) cls.loader.backup_registry() + from .model import WmsProductSync, StockPicking - from .model import WmsProductSync - - cls.loader.update_registry((WmsProductSync,)) + cls.loader.update_registry( + ( + WmsProductSync, + StockPicking, + ) + ) def tearDown(self): super().tearDown() - files = self.backend.list_files("OUT/") + self.registry.leave_test_mode() + files = self.backend.list_files(self.backend.directory_path) for f in files: - self.backend.delete("OUT/" + f) + self.backend.delete(self.backend.directory_path + f) @classmethod def tearDownClass(cls): cls.loader.restore_registry() super().tearDownClass() - def assertNewAttachmentQueue(self): + def assertNewAttachmentQueues(self, n=1): aq_after = self.env["attachment.queue"].search([]) - self.assertEqual(len(aq_after - self.env["attachment.queue"].search([])), 1) + self.assertEqual(len(aq_after - self.aq_before), n) return aq_after diff --git a/wms_connector/tests/model.py b/wms_connector/tests/model.py index 45635483be5..6e3a5f4f2f1 100644 --- a/wms_connector/tests/model.py +++ b/wms_connector/tests/model.py @@ -17,3 +17,18 @@ def _prepare_export_data(self): def _get_export_name(self): return str(uuid.uuid4()) + + +class StockPicking(models.Model): + _inherit = "stock.picking" + + def _prepare_export_data(self): + return [ + { + "name": rec.name, + } + for rec in self + ] + + def _get_export_name(self): + return str(uuid.uuid4()) diff --git a/wms_connector/tests/test_activate_sync.py b/wms_connector/tests/test_activate_sync.py index 7d78e84621c..6f85bc5d91d 100644 --- a/wms_connector/tests/test_activate_sync.py +++ b/wms_connector/tests/test_activate_sync.py @@ -1,10 +1,10 @@ # Copyright 2023 Akretion # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo.tests.common import TransactionCase +from odoo.tests.common import SavepointCase -class TestActivateSync(TransactionCase): +class TestActivateSync(SavepointCase): def setUp(self): super().setUp() self.warehouse = self.env.ref("stock.warehouse0") diff --git a/wms_connector/tests/test_export.py b/wms_connector/tests/test_export.py index 0e8959fad0b..fb4b7c03b2a 100644 --- a/wms_connector/tests/test_export.py +++ b/wms_connector/tests/test_export.py @@ -1,19 +1,47 @@ # Copyright 2023 Akretion # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo.tests.common import TransactionCase from .common import WmsConnectorCommon +import mock + +FN_EXPORT_VALS = ( + "odoo.addons.wms_connector.tests.model.WmsProductSync._prepare_export_data" +) class TestExportFile(WmsConnectorCommon): def setUp(self): super().setUp() self.warehouse.active_wms_sync = True + self.cron_export = self.warehouse.wms_export_cron_id + self.demo_product = self.env.ref("product.product_product_1") + + def test_export_filter(self): + self.warehouse.refresh_wms_products() + self.setAllExported() + self.env["wms.product.sync"].search( + [("product_id", "=", self.demo_product.id)] + ).wms_export_date = False + self.cron_export.method_direct_trigger() + self.assertNewAttachmentQueues(1) - # def test_run_export_cron(self): - # self.warehouse.wms_export_cron_id.method_direct_trigger() + # def test_export_error(self): + # self.setAllExported() + # self.demo_product.wms_export_date = False + # # with mock.patch(FN_EXPORT_VALS) as mocked: + # # # mocked. + # # mocked.side_effect = derp + # self.cron_export.method_direct_trigger() + # wms_product = self.env["wms.product.sync"].search( + # [("product_id", "=", self.demo_product.id)] + # ) + # # self.assertIn(wms_product.wms_export_error, "ValueError") - def test_export_product(self): + def test_export_repeat(self): self.warehouse.refresh_wms_products() - self.warehouse.wms_export_cron_id.method_direct_trigger() - self.assertNewAttachmentQueue() + self.cron_export.method_direct_trigger() + n_products = len(self.env["wms.product.sync"].search([]).ids) + n_pickings = len(self.env["stock.picking"].search([]).ids) + self.assertNewAttachmentQueues(n_pickings + n_products) + self.cron_export.method_direct_trigger() + self.assertNewAttachmentQueues(n_pickings + n_products)