Skip to content

Commit c3d9946

Browse files
committed
[IMP] real_estate: add test cases in real estate module
In this tutorial, get knowledge about runbot and create a test cases which create offer for sold property and Sell a property with no accepted offers implement this two test cases. also add reset of Garden Area and Orientation when you uncheck the Garden checkbox this test case also implement. this is useful for test some type of cases which can raise error.
1 parent ea0d4d0 commit c3d9946

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed

real_estate/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
],
2424
"demo": [
2525
'demo/estate_property_demo_data.xml',
26-
],
26+
]
2727
}

real_estate/models/estate.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ def _onchange_garden(self):
100100

101101
def action_mark_sold(self):
102102
for record in self:
103+
if not any(offer.status == 'accepted' for offer in record.offer_ids):
104+
raise UserError("You cannot sell a property without an accepted offer.")
103105
if record.state == 'cancelled':
104106
raise UserError("Canceled properties cannot be sold.")
105-
record.state = 'sold'
107+
record.state = 'sold'
106108

107109
def action_mark_cancelled(self):
108110
for record in self:

real_estate/models/estate_property_offer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ def create(self, vals_list):
6969
for vals in vals_list:
7070
property_id = vals.get('property_id')
7171
amount = vals.get('price')
72+
property = self.env['estate.property'].browse(property_id)
73+
existing_offers = property.offer_ids.filtered(lambda o: o.price is not None and amount is not None and o.price >= amount)
7274

7375
if property_id and amount:
74-
existing_offers = self.search([
75-
('property_id', '=', property_id),
76-
('price', '>=', amount)
77-
])
76+
if property.state == 'sold':
77+
raise UserError("Cannot create offer for a sold property.")
78+
7879
if existing_offers:
7980
raise ValidationError("An offer with a higher or equal price already exists.")
8081

81-
property = self.env['estate.property'].browse(property_id)
8282
if property.state == 'new':
8383
property.state = 'offer_received'
8484

real_estate/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_property_offer
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from odoo.tests.common import TransactionCase
2+
from odoo.tests import Form
3+
from odoo.exceptions import UserError
4+
from odoo.tests import tagged
5+
6+
7+
@tagged('post_install', '-at_install')
8+
class TestEstatePropertyLogic(TransactionCase):
9+
def setUp(self):
10+
super().setUp()
11+
Property = self.env['estate.property']
12+
Offer = self.env['estate.property.offer']
13+
14+
self.partner = self.env['res.partner'].create({
15+
'name': 'Test Buyer',
16+
'email': '[email protected]'
17+
})
18+
19+
self.property = Property.create({
20+
'name': 'Test Property',
21+
'state': 'new',
22+
'expected_price': 150000,
23+
'garden': True,
24+
'garden_area': 100,
25+
'garden_orientation': 'north',
26+
})
27+
28+
self.offer = Offer.create({
29+
'property_id': self.property.id,
30+
'partner_id': self.partner.id,
31+
'price': 160000,
32+
})
33+
34+
def test_cannot_create_offer_on_sold_property(self):
35+
self.property.state = 'sold'
36+
with self.assertRaises(UserError):
37+
self.env['estate.property.offer'].create({
38+
'property_id': self.property.id,
39+
'partner_id': self.partner.id,
40+
'price': 170000
41+
})
42+
43+
def test_cannot_sell_property_without_accepted_offer(self):
44+
self.property.offer_ids.unlink()
45+
with self.assertRaises(UserError):
46+
self.property.action_mark_sold()
47+
48+
def test_can_sell_property_with_accepted_offer(self):
49+
self.offer.action_accept()
50+
self.property.action_mark_sold()
51+
self.assertEqual(self.property.selling_price, self.offer.price)
52+
self.assertEqual(self.property.buyer, self.partner)
53+
54+
def test_reset_garden_fields_when_unchecked(self):
55+
form = Form(self.env['estate.property'])
56+
form.name = 'Garden Test'
57+
form.garden = True
58+
form.expected_price = 15000
59+
form.garden_area = 50
60+
form.garden_orientation = 'east'
61+
prop = form.save()
62+
63+
prop.garden = False
64+
prop._onchange_garden()
65+
66+
self.assertEqual(prop.garden_area, 0)
67+
self.assertFalse(prop.garden_orientation)

0 commit comments

Comments
 (0)