From a2bb0c2bcd6ce7c1d94dc8b20db535e7a6f6540e Mon Sep 17 00:00:00 2001 From: nape-odoo Date: Tue, 26 Nov 2024 15:34:01 +0100 Subject: [PATCH] [IMP] estate: Tutorial - unit tests, with the exception of the form-related one --- estate/models/estate_property.py | 8 ++-- estate/models/estate_property_offer.py | 2 + estate/tests/__init__.py | 2 + estate/tests/test_estate_property.py | 53 ++++++++++++++++++++++ estate/tests/test_estate_property_offer.py | 34 ++++++++++++++ 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 estate/tests/__init__.py create mode 100644 estate/tests/test_estate_property.py create mode 100644 estate/tests/test_estate_property_offer.py diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py index 667f0b6003..0c84d77e5a 100644 --- a/estate/models/estate_property.py +++ b/estate/models/estate_property.py @@ -92,10 +92,12 @@ def _onchange_garden(self): def action_set_sold(self): for record in self: - if record.state != 'cancelled': - record.state = 'sold' - else: + if record.state == 'cancelled': raise UserError(self.env._("The property is already cancelled.")) + elif not record.buyer_id: # Checking for buyer_id to be set instead of going through the offers in search of an accepted one + raise UserError(self.env._("The property does not have any accepted offers.")) + else: + record.state = 'sold' return True def action_set_cancelled(self): diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py index 40bb4cf468..699096f0af 100644 --- a/estate/models/estate_property_offer.py +++ b/estate/models/estate_property_offer.py @@ -62,5 +62,7 @@ def create(self, vals): property = self.env['estate.property'].browse(vals['property_id']) if property.state != 'new' and (float_compare(vals['price'], property.best_offer, precision_digits=3) < 0): raise UserError(self.env._("A better priced offer already exists.")) + elif property.state in ('sold', 'cancelled'): + raise UserError(self.env._("Cannot add offer to a property that is already sold or cancelled.")) property.state = 'recieved' return super().create(vals) diff --git a/estate/tests/__init__.py b/estate/tests/__init__.py new file mode 100644 index 0000000000..d6724ad4c7 --- /dev/null +++ b/estate/tests/__init__.py @@ -0,0 +1,2 @@ +from . import test_estate_property +from . import test_estate_property_offer diff --git a/estate/tests/test_estate_property.py b/estate/tests/test_estate_property.py new file mode 100644 index 0000000000..1105ab08a3 --- /dev/null +++ b/estate/tests/test_estate_property.py @@ -0,0 +1,53 @@ +from odoo.tests.common import TransactionCase +from odoo.exceptions import UserError +from odoo.tests import tagged + +@tagged('post_install', '-at_install') +class EstatePropertyTestCase(TransactionCase): + + @classmethod + def setUpClass(cls): + super(EstatePropertyTestCase, cls).setUpClass() + cls.property = cls.env['estate.property'].create({ + 'name': 'Test', + 'expected_price': '1', + }) + + def test_sell_no_offers(self): + """Test that a property cannot be sold when there are no accepted offers: no offers at all.""" + self.property.offer_ids = [] + with self.assertRaises(UserError): + self.property.action_set_sold() + + def test_sell_no_accepted_offers(self): + """Test that a property cannot be sold when there are no accepted offers: some unaccepted offers are present.""" + offers = [ + self.env['estate.property.offer'].create({ + 'property_id': self.property.id, + 'partner_id': 1, # Trying to avoid having to create a new (irrelevant) res.partner record, appears to work + 'price': 1, + }), + self.env['estate.property.offer'].create({ + 'property_id': self.property.id, + 'partner_id': 1, + 'price': 1, + 'state': 'refused' + }), + ] + self.property.offer_ids = [offer.id for offer in offers] + with self.assertRaises(UserError): + self.property.action_set_sold() + + def test_sell_sets_sold(self): + """Test that selling a property sets it to 'Sold'.""" + offers = [ + self.env['estate.property.offer'].create({ + 'property_id': self.property.id, + 'partner_id': 1, + 'price': 1, + }), + ] + self.property.offer_ids = [offer.id for offer in offers] + offers[0].action_accept() + self.property.action_set_sold() + self.assertEqual(self.property.state, 'sold') diff --git a/estate/tests/test_estate_property_offer.py b/estate/tests/test_estate_property_offer.py new file mode 100644 index 0000000000..75c677c011 --- /dev/null +++ b/estate/tests/test_estate_property_offer.py @@ -0,0 +1,34 @@ +from odoo.tests.common import TransactionCase +from odoo.exceptions import UserError +from odoo.tests import tagged + +@tagged('post_install', '-at_install') +class EstatePropertyOfferTestCase(TransactionCase): + + @classmethod + def setUpClass(cls): + super(EstatePropertyOfferTestCase, cls).setUpClass() + cls.property = cls.env['estate.property'].create({ + 'name': 'Test', + 'expected_price': '1', + }) + + def test_create_offer_for_cancelled(self): + """Test that an offer cannot be created for a cancelled property.""" + self.property.state = 'cancelled' + with self.assertRaises(UserError): + self.env['estate.property.offer'].create({ + 'property_id': self.property.id, + 'partner_id': 1, + 'price': 1, + }) + + def test_create_offer_for_sold(self): + """Test that an offer cannot be created for a sold property.""" + self.property.state = 'sold' + with self.assertRaises(UserError): + self.env['estate.property.offer'].create({ + 'property_id': self.property.id, + 'partner_id': 1, + 'price': 1, + })