From ac11e0a57e122c4d6831ab76de6b3cebbdad0afc Mon Sep 17 00:00:00 2001 From: Kaynnan Bardauil Date: Tue, 21 Mar 2023 16:22:57 -0300 Subject: [PATCH] [IMP] maintenance_request_repair: add tests --- .../tests/test_repair_order.py | 142 +++++++++++++++--- 1 file changed, 119 insertions(+), 23 deletions(-) diff --git a/maintenance_request_repair/tests/test_repair_order.py b/maintenance_request_repair/tests/test_repair_order.py index 67518c409..2123a4cd7 100644 --- a/maintenance_request_repair/tests/test_repair_order.py +++ b/maintenance_request_repair/tests/test_repair_order.py @@ -6,7 +6,6 @@ class TestRepairOrder(common.TransactionCase): def setUp(self): - super(TestRepairOrder, self).setUp() self.product = self.env["product.product"].create( @@ -34,44 +33,141 @@ def setUp(self): } ) - self.request1 = self.env["maintenance.request"].create({"name": "Request 1"}) - self.request2 = self.env["maintenance.request"].create({"name": "Request 2"}) - self.repair_order.maintenance_request_ids += self.request1 - self.repair_order.maintenance_request_ids += self.request2 + self.request1 = self.env["maintenance.request"].create( + { + "name": "Request 1", + "repair_order_id": self.repair_order.id, + } + ) + self.request2 = self.env["maintenance.request"].create( + { + "name": "Request 2", + "repair_order_id": self.repair_order.id, + } + ) self.equipment = self.env["maintenance.equipment"].create( {"name": "Equipment 1"} ) - self.partner = self.env["res.partner"].create({"name": "Test Partner"}) def test_compute_maintenance_request_count(self): - + self.repair_order.write( + {"maintenance_request_ids": [(4, self.request1.id), (4, self.request2.id)]} + ) self.repair_order._compute_maintenance_request_count() assert self.repair_order.maintenance_request_count == 2 def test_action_view_maintenance_request(self): - self.result = self.repair_order.action_view_maintenance_request() - self.assertTrue(self.result) - self.assertIn("context", self.result) - self.assertIn("domain", self.result) - self.assertIn("views", self.result) - self.assertIn("res_id", self.result) + self.result = self.repair_order.with_user( + self.env.user + ).action_view_maintenance_request() + self.repair_order.write( + {"maintenance_request_ids": [(4, self.request1.id), (4, self.request2.id)]} + ) self.assertEqual( self.result["context"], {"default_repair_order_id": self.repair_order.id} ) self.maintenance_request_ids = self.repair_order.mapped( "maintenance_request_ids" ) + with self.subTest("Choose the view_mode accordingly"): + + with self.subTest( + "Check maintenance_request_ids > 1 OR not self.maintenance_request_ids" + ): + self.assertTrue( + len(self.repair_order.maintenance_request_ids) > 1 + or not self.repair_order.maintenance_request_ids + ) + self.result["domain"] = "[('id','in',%s)]" % ( + self.maintenance_request_ids.ids + ) + self.assertEqual( + self.result["domain"], + "[('id','in',%s)]" % (self.maintenance_request_ids.ids), + ) + + self.repair_order.write( + {"maintenance_request_ids": [(6, 0, self.request1.id)]} + ) + self.maintenance_request_ids = self.repair_order.maintenance_request_ids + self.result = self.repair_order.action_view_maintenance_request() + + with self.subTest("Check maintenance_request_ids == 1"): + + self.assertEqual(len(self.maintenance_request_ids), 1) + res = self.env.ref("maintenance.hr_equipment_request_view_form", False) + form_view = [(res and res.id or False, "form")] + + with self.subTest("Check view in result"): + + self.assertIn("views", self.result) + self.result["views"] = form_view + [ + (state, view) + for state, view in self.result["views"] + if view != "form" + ] + self.assertEqual( + self.result["views"], + ( + form_view + + [ + (state, view) + for state, view in self.result["views"] + if view != "form" + ] + ), + ) + + self.assertNotEqual(self.result["views"], form_view) + del self.result["views"] + + self.repair_order.write( + {"maintenance_request_ids": [(2, self.request1.id)]} + ) + self.maintenance_request_ids = self.repair_order.maintenance_request_ids + + with self.subTest("Check view not in result"): + self.assertNotIn("views", self.result) + self.result["views"] = form_view + self.assertEqual(self.result["views"], form_view) + + self.result["res_id"] = self.maintenance_request_ids.id + self.assertEqual(self.result["res_id"], self.maintenance_request_ids.id) + + def test_create_repair_order(self): + self.repair_order = self.env["repair.order"].create( + { + "partner_id": self.partner.id, + "product_id": self.product.id, + } + ) + self.repair_order.name = "Repair Order Test 2" + self.assertEqual(self.repair_order.name, "Repair Order Test 2") + self.assertEqual(self.repair_order.partner_id, self.partner) + self.assertEqual(self.repair_order.product_id, self.product) - self.repair_order.maintenance_request_ids = False - self.repair_order._compute_maintenance_request_count() - self.action_result = self.repair_order.action_view_maintenance_request() - self.assertTrue("domain" in self.action_result) - - self.repair_order.maintenance_request_ids = self.request1 - self.repair_order._compute_maintenance_request_count() - self.action_result = self.repair_order.action_view_maintenance_request() - self.assertTrue("views" in self.action_result) - self.assertTrue("res_id" in self.action_result) + def test_simple_view_maintenance_request(self): + self.repair_order = self.env["repair.order"].create( + { + "name": "Repair Order Test", + "partner_id": self.partner.id, + "product_id": self.product.id, + } + ) + self.maintenance_request = self.env["maintenance.request"].create( + { + "name": "Maintenance Request Test", + "repair_order_id": self.repair_order.id, + } + ) + self.action = self.repair_order.action_view_maintenance_request() + self.assertEqual(self.action["type"], "ir.actions.act_window") + self.assertEqual(self.action["res_model"], "maintenance.request") + self.assertEqual( + self.action["view_mode"], "kanban,tree,form,pivot,graph,calendar" + ) + self.assertEqual(self.action["target"], "current") + self.assertEqual(self.action["res_id"], self.maintenance_request.id)