From b78b544c8346e34f30d6cebee286e790484d0245 Mon Sep 17 00:00:00 2001 From: Pablo Date: Wed, 18 Sep 2019 13:46:29 +0200 Subject: [PATCH] [ADD] multihotel room consistency and test cases --- hotel/models/hotel_room.py | 30 +++++++++++++++++++ hotel/tests/__init__.py | 2 +- ...hotel_room_model.py => test_hotel_room.py} | 24 +++++++++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) rename hotel/tests/{test_hotel_room_model.py => test_hotel_room.py} (58%) diff --git a/hotel/models/hotel_room.py b/hotel/models/hotel_room.py index b04363491..b564eeed3 100644 --- a/hotel/models/hotel_room.py +++ b/hotel/models/hotel_room.py @@ -41,6 +41,36 @@ class HotelRoom(models.Model): active = fields.Boolean('Active', default=True) sequence = fields.Integer('Sequence', default=0) + # Constraints and onchanges + @api.constrains('capacity') + def _check_capacity(self): + for record in self: + if record.capacity < 1: + raise ValidationError(_("The capacity of the room must be greater than 0.")) + + # CRUD methods + @api.model + def create(self, vals): + if vals.get('hotel_id', self.env.user.hotel_id.id) != self.env['hotel.room.type'].browse( + vals['room_type_id']).hotel_id.id: + raise ValidationError(_("A room cannot be created in a room type of another hotel.")) + return super().create(vals) + + @api.multi + def write(self, vals): + for record in self: + if vals.get('hotel_id', record.hotel_id.id) != record.hotel_id.id: + raise ValidationError(_("A room cannot be changed to another hotel.") + " " + + _("%s does not belong to %s.") + % (record, record.hotel_id)) + room_type_ids = self.env['hotel.room.type'].search([ + ('hotel_id', '=', record.hotel_id.id) + ]).ids + if vals.get('room_type_id', record.room_type_id.id) not in room_type_ids: + raise ValidationError(_("A room cannot be changed to a room type of another hotel or " + "unlinked from a room type.")) + return super().write(vals) + # Business methods @api.multi def get_capacity(self, extra_bed=0): diff --git a/hotel/tests/__init__.py b/hotel/tests/__init__.py index 97e3c067a..2dec7c131 100644 --- a/hotel/tests/__init__.py +++ b/hotel/tests/__init__.py @@ -22,6 +22,6 @@ ############################################################################## # from . import test_reservation # from . import test_folio -# from . import test_hotel_room_model from . import test_inherited_ir_http from . import test_hotel_room_type +from . import test_hotel_room diff --git a/hotel/tests/test_hotel_room_model.py b/hotel/tests/test_hotel_room.py similarity index 58% rename from hotel/tests/test_hotel_room_model.py rename to hotel/tests/test_hotel_room.py index 4c28fdb2c..d047b00e0 100644 --- a/hotel/tests/test_hotel_room_model.py +++ b/hotel/tests/test_hotel_room.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -26,8 +25,29 @@ from odoo.exceptions import ValidationError class TestHotelRoom(TestHotel): + def test_rooms_by_hotel(self): + # A room cannot be created in a room type of another hotel + with self.assertRaises(ValidationError): + record = self.env['hotel.room'].sudo().create({ + 'name': 'Test Room', + 'hotel_id': self.demo_hotel_property.id, + 'room_type_id': self.room_type_0.id, + }) + # A room cannot be changed to another hotel + with self.assertRaises(ValidationError): + self.room_0.sudo().write({ + 'hotel_id': self.demo_room_type_0.hotel_id.id + }) + + def test_rooms_by_room_type(self): + # A room cannot be changed to a room type of another hotel + with self.assertRaises(ValidationError): + self.room_0.sudo().write({ + 'room_type_id': self.demo_room_type_1.id + }) + def test_check_capacity(self): - # TODO Do the test using different users + # The capacity of the room must be greater than 0 with self.assertRaises(ValidationError): self.room_0.sudo().write({ 'capacity': 0