Skip to content

Commit

Permalink
[IMP] shipment_advice_planner_toursolver: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbejaoui committed Jan 22, 2024
1 parent f5b7e61 commit 68305b5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
12 changes: 7 additions & 5 deletions shipment_advice_planner_toursolver/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo.osv.expression import AND, distribute_not
from odoo.osv.expression import AND, OR


class StockPicking(models.Model):
Expand Down Expand Up @@ -42,14 +42,16 @@ def _compute_can_be_planned_in_shipment_advice(self):
return res

def _search_can_be_planned_in_shipment_advice(self, operator, value):
domain = super()._search_can_be_planned_in_shipment_advice(operator, value)
if (operator == "=" and value) or (operator == "!=" and not value):
domain = super()._search_can_be_planned_in_shipment_advice(operator, value)
extra_domain = [
"|",
("toursolver_task_id", "=", False),
("toursolver_task_id.state", "not in", ("draft", "in_progress")),
]
return AND([domain, extra_domain])
return distribute_not(
["!"] + self._search_can_be_planned_in_shipment_advice("=", True)
)
extra_domain = [
("toursolver_task_id", "!=", False),
("toursolver_task_id.state", "in", ("draft", "in_progress")),
]
return OR([domain, extra_domain])
2 changes: 2 additions & 0 deletions shipment_advice_planner_toursolver/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from . import common
from . import test_shipment_advice_planner_toursolver
from . import test_toursolver_delivery_window
from . import test_picking_can_be_planned
37 changes: 37 additions & 0 deletions shipment_advice_planner_toursolver/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from odoo.tests.common import Form, TransactionCase


class TestShipmentAdvicePlannerToursolverCommon(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.pickings = cls.env["stock.picking"].search([])
cls.context = {
"active_ids": cls.pickings.ids,
"active_model": "stock.picking",
}
cls.warehouse = cls.env.ref("stock.warehouse0")
cls.resource_1 = cls.env.ref(
"shipment_advice_planner_toursolver.toursolver_resource_r1_demo"
)
cls.resource_2 = cls.env.ref(
"shipment_advice_planner_toursolver.toursolver_resource_r2_demo"
)

def setUp(self):
super().setUp()
self.wizard_form = Form(
self.env["shipment.advice.planner"].with_context(**self.context)
)
self.wizard_form.warehouse_id = self.warehouse

def _create_task(self):
self.wizard_form.shipment_planning_method = "toursolver"
wizard = self.wizard_form.save()
wizard.delivery_resource_ids = self.resource_1 | self.resource_2
wizard.button_plan_shipments()
return wizard.picking_to_plan_ids.toursolver_task_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.addons.shipment_advice_planner.tests.test_picking_can_be_planned import (
TestPickingCanBePlanned as TestPickingCanBePlannedBase,
)

from .common import TestShipmentAdvicePlannerToursolverCommon


class TestPickingCanBePlanned(
TestPickingCanBePlannedBase, TestShipmentAdvicePlannerToursolverCommon
):
def test_01(self):
picking = self.pickings.filtered(lambda p: p.state == "assigned")[0]
self.assert_picking_can_be_planned_in_shipment_advice(picking)
self._create_task()
self.assertTrue(picking.toursolver_task_id)
self.assert_can_not_be_planned_in_shipment_advice(picking)
picking.toursolver_task_id.state = "done"
self.assert_picking_can_be_planned_in_shipment_advice(picking)
picking.toursolver_task_id.state = "cancelled"
self.assert_picking_can_be_planned_in_shipment_advice(picking)
picking.toursolver_task_id.state = "draft"
self.assert_can_not_be_planned_in_shipment_advice(picking)
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,14 @@
from freezegun import freeze_time
from vcr_unittest import VCRTestCase

from odoo.tests.common import Form, TransactionCase
from odoo.tools import mute_logger

from .common import TestShipmentAdvicePlannerToursolverCommon

class TestShipmentAdvicePlannerToursolver(VCRTestCase, TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.pickings = cls.env["stock.picking"].search([])
cls.context = {
"active_ids": cls.pickings.ids,
"active_model": "stock.picking",
}
cls.warehouse = cls.env.ref("stock.warehouse0")
cls.resource_1 = cls.env.ref(
"shipment_advice_planner_toursolver.toursolver_resource_r1_demo"
)
cls.resource_2 = cls.env.ref(
"shipment_advice_planner_toursolver.toursolver_resource_r2_demo"
)

def setUp(self):
super().setUp()
self.wizard_form = Form(
self.env["shipment.advice.planner"].with_context(**self.context)
)
self.wizard_form.shipment_planning_method = "toursolver"
self.wizard_form.warehouse_id = self.warehouse

def _create_task(self):
wizard = self.wizard_form.save()
wizard.delivery_resource_ids = self.resource_1 | self.resource_2
wizard.button_plan_shipments()
return wizard.picking_to_plan_ids.toursolver_task_id

class TestShipmentAdvicePlannerToursolver(
VCRTestCase, TestShipmentAdvicePlannerToursolverCommon
):
def test_query_url(self):
expected_query_url = (
"https://geoservices.geoconcept.com/ToursolverCloud/api/ts/toursolver/"
Expand All @@ -51,6 +24,7 @@ def test_query_url(self):
self.assertEqual(query_url, expected_query_url)

def test_toursolver_task_creation(self):
self.wizard_form.shipment_planning_method = "toursolver"
wizard = self.wizard_form.save()
self.assertFalse(wizard.picking_to_plan_ids.toursolver_task_id)
wizard.button_plan_shipments()
Expand Down

0 comments on commit 68305b5

Please sign in to comment.