From c430e1e75f82973f2f39d338be4351b707d49af9 Mon Sep 17 00:00:00 2001 From: Pablo Date: Fri, 20 Sep 2019 12:58:21 +0200 Subject: [PATCH] [ADD] product pricelist constraint and test cases --- hotel/models/inherited_product_pricelist.py | 12 +++++--- hotel/tests/__init__.py | 1 + .../tests/test_inherited_product_pricelist.py | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 hotel/tests/test_inherited_product_pricelist.py diff --git a/hotel/models/inherited_product_pricelist.py b/hotel/models/inherited_product_pricelist.py index 915fa7056..4d43b06c7 100644 --- a/hotel/models/inherited_product_pricelist.py +++ b/hotel/models/inherited_product_pricelist.py @@ -28,7 +28,11 @@ class ProductPricelist(models.Model): if record.pricelist_type == 'daily' and len(record.hotel_ids) != 1: raise ValidationError(_("A daily pricelist is used as a daily Rate Plan for room types " "and therefore must be related with one and only one hotel.")) - if record.pricelist_type == 'daily' and len(record.hotel_ids) == 1 \ - and record.hotel_ids.id != record.hotel_ids.default_pricelist_id.hotel_ids.id: - raise ValidationError(_("Relationship mismatch.") + " " + - _("This pricelist is used as default in a different hotel.")) + + if record.pricelist_type == 'daily' and len(record.hotel_ids) == 1: + hotel_id = self.env['hotel.property'].search([ + ('default_pricelist_id', '=', record.id) + ]) or None + if hotel_id and hotel_id != record.hotel_ids: + raise ValidationError(_("Relationship mismatch.") + " " + + _("This pricelist is used as default in a different hotel.")) diff --git a/hotel/tests/__init__.py b/hotel/tests/__init__.py index 4428c2cc2..654bd2825 100644 --- a/hotel/tests/__init__.py +++ b/hotel/tests/__init__.py @@ -23,6 +23,7 @@ # from . import test_reservation # from . import test_folio from . import test_inherited_ir_http +from . import test_inherited_product_pricelist from . import test_hotel_room_type from . import test_hotel_room from . import test_massive_changes diff --git a/hotel/tests/test_inherited_product_pricelist.py b/hotel/tests/test_inherited_product_pricelist.py new file mode 100644 index 000000000..c60a2084d --- /dev/null +++ b/hotel/tests/test_inherited_product_pricelist.py @@ -0,0 +1,30 @@ +from .common import TestHotel +from odoo import fields +from odoo.exceptions import ValidationError + + +class TestInheritedProductPricelist(TestHotel): + + # be aware using self.env.user.hotel_id because it is the value configure for the user running the tests + + def test_daily_pricelist(self): + # A daily pricelist must be related with one and only one hotel #1 + with self.assertRaises(ValidationError): + self.list0.hotel_ids += self.demo_hotel_property + + # A daily pricelist must be related with one and only one hotel #2 + with self.assertRaises(ValidationError): + self.list0.hotel_ids = False + + # create a valid record using a daily pricelist + test_result = self.env['product.pricelist'].create({ + 'name': 'Test Daily Pricelist', + 'hotel_ids': [(4, self.demo_hotel_property.id)] + }) + self.assertEqual(test_result.pricelist_type, 'daily') + self.assertEqual(test_result.hotel_ids, self.demo_hotel_property) + + def test_pricelist_by_hotel(self): + # A daily pricelist must be related with one and only one hotel #1 + with self.assertRaises(ValidationError): + self.list0.hotel_ids = self.demo_hotel_property