Skip to content

Commit 1592d6d

Browse files
committed
[IMP] estate: Adding safeguard your code with unit tests
1 parent 88e2655 commit 1592d6d

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

estate/models/estate_property.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ def _onchange_partner_id(self):
6363

6464
def action_sold(self):
6565
for record in self:
66-
if record.state != 'canceled':
67-
record.state = 'sold'
68-
else:
66+
if record.state != 'offer_accepted':
67+
error_msg = "You cannot sell a property that has not an accepted offer."
68+
raise UserError(error_msg)
69+
if record.state == 'canceled':
6970
error_msg = "You cannot sell a canceled property."
7071
raise UserError(error_msg)
72+
record.state = 'sold'
7173
return True
7274

7375
def action_cancel(self):

estate/models/estate_property_offer.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ def create(self, vals):
5555
if len(vals) == 0:
5656
return super().create(vals)
5757
property_id = vals[0].get('property_id')
58+
property_record = self.env['estate.property'].browse(property_id)
59+
60+
if property_record.state in ['sold']:
61+
error_msg = "You cannot make an offer on a property that is already sold."
62+
raise ValidationError(error_msg)
63+
5864
price = vals[0].get('price')
59-
if property_id and price:
60-
property_record = self.env['estate.property'].browse(property_id)
61-
if property_record.best_price and price <= property_record.best_price:
62-
error_msg = "The offer price should be higher than the best offer of the property."
63-
raise ValidationError(error_msg)
64-
property_record.state = 'offer_received'
65+
if property_record.best_price and price <= property_record.best_price:
66+
error_msg = "The offer price should be higher than the best offer of the property."
67+
raise ValidationError(error_msg)
68+
69+
property_record.state = 'offer_received'
6570
return super().create(vals)

estate/tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Part of Odoo. See LICENSE file for full copyright and licensing details.
2+
3+
from . import test_estate

estate/tests/test_estate.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from odoo.exceptions import UserError
2+
from odoo.tests import Form, tagged
3+
from odoo.tests.common import TransactionCase
4+
5+
6+
@tagged('post_install', '-at_install')
7+
class EstateTestCase(TransactionCase):
8+
9+
@classmethod
10+
def setUpClass(cls):
11+
super().setUpClass()
12+
cls.partner = cls.env['res.partner'].create({
13+
'name': 'Test Partner',
14+
})
15+
cls.properties = cls.env['estate.property'].create([
16+
{
17+
'name': 'Property 1',
18+
'expected_price': 100,
19+
},
20+
{
21+
'name': 'Property 2',
22+
'expected_price': 200,
23+
'state': 'sold',
24+
},
25+
{
26+
'name': 'Property 3',
27+
'expected_price': 300,
28+
'state': 'sold',
29+
},
30+
])
31+
32+
cls.offer_partner = cls.env['res.partner'].create({
33+
'name': 'Test Partner',
34+
})
35+
36+
def test_cannot_create_offer_on_sold_property(self):
37+
"""Test that we cannot create an offer on a sold property."""
38+
self.properties[0].write({'state': 'sold'})
39+
40+
with self.assertRaises(UserError):
41+
self.env['estate.property.offer'].create([{
42+
'price': 300,
43+
'partner_id': self.offer_partner.id,
44+
'property_id': self.properties[0].id,
45+
}])
46+
47+
def test_cannot_sold_a_property_without_accepted_offer(self):
48+
"""Test that we cannot sold a property without an accepted offer."""
49+
50+
# Add an offer but not accept it
51+
with self.assertRaises(UserError):
52+
self.env['estate.property.offer'].create([{
53+
'price': 300,
54+
'partner_id': self.offer_partner.id,
55+
'property_id': self.properties[1].id,
56+
}])
57+
# Sold without any offer
58+
with self.assertRaises(UserError):
59+
self.properties[2].action_sold()
60+
61+
def test_rest_garden_fields(self):
62+
"""Test that the garden fields are reset when the garden field is set to False."""
63+
property_record = self.properties[0]
64+
65+
with Form(property_record) as property_form:
66+
property_form.garden = True
67+
self.assertEqual(property_record.garden_area, 10)
68+
self.assertEqual(property_record.garden_orientation, 'north')
69+
70+
with Form(property_record) as property_form:
71+
property_form.garden = False
72+
self.assertEqual(property_record.garden_area, 0)
73+
self.assertFalse(property_record.garden_orientation)

0 commit comments

Comments
 (0)