Skip to content

Commit eff89b1

Browse files
committed
[IMP] estate: Unit Tests
1 parent 48d5ad6 commit eff89b1

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

estate/models/estate_property.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ def _unlink_if_new_or_cancelled(self):
9191
def action_mark_as_sold(self):
9292
self.ensure_one()
9393
if self.state == "cancelled":
94-
raise UserError("A cancelled property cannot be set as sold.")
94+
raise UserError(_("A cancelled property cannot be set as sold."))
95+
if not any(offer.status == "accepted" for offer in self.offer_ids):
96+
raise UserError(_("A property must have an accepted offer to be marked as sold."))
9597
self.state = "sold"
9698
return True
9799

estate/models/estate_property_offer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def _inverse_deadline(self):
3939
def create(self, vals_list: list[dict]):
4040
for val in vals_list:
4141
estate_property = self.env["estate.property"].browse(val["property_id"])
42+
if estate_property.state == "sold":
43+
raise UserError(_("Cannot create a new offer for a sold property."))
4244
if any(offer.price > val["price"] for offer in estate_property.offer_ids):
4345
raise UserError(_("Cannot create a new offer with a lower price than an existing offer."))
4446
if estate_property.state == 'new':

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_estate_property
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from odoo.tests.common import TransactionCase
2+
from odoo.exceptions import UserError
3+
from odoo.tests import tagged, Form
4+
5+
6+
ADMINISTRATOR_ID = 3
7+
8+
9+
@tagged("post_install", "-at_install")
10+
class EstateTestCase(TransactionCase):
11+
12+
@classmethod
13+
def setUpClass(cls):
14+
super(EstateTestCase, cls).setUpClass()
15+
cls.new_property = cls.env["estate.property"].create([{
16+
"name": "New Property",
17+
"state": "new",
18+
"expected_price": 1000.0
19+
}])
20+
cls.received_property = cls.env["estate.property"].create([{
21+
"name": "Received offer Property",
22+
"state": "received",
23+
"expected_price": 2000.0
24+
}])
25+
cls.accepted_property = cls.env["estate.property"].create([{
26+
"name": "Accepeted offer Property",
27+
"state": "accepted",
28+
"expected_price": 3000.0
29+
}])
30+
cls.create_sold_property()
31+
cls.cancelled_property = cls.env["estate.property"].create([{
32+
"name": "Cancelled Property",
33+
"state": "cancelled",
34+
"expected_price": 5000.0
35+
}])
36+
cls.env["estate.property.offer"].create([
37+
{
38+
"partner_id": ADMINISTRATOR_ID,
39+
"property_id": cls.accepted_property.id,
40+
"status": "accepted",
41+
"price": 3000.0
42+
},
43+
{
44+
"partner_id": ADMINISTRATOR_ID,
45+
"property_id": cls.received_property.id,
46+
"status": "refused",
47+
"price": 1999.0
48+
},
49+
{
50+
"partner_id": ADMINISTRATOR_ID,
51+
"property_id": cls.received_property.id,
52+
"price": 2000.0
53+
}
54+
])
55+
56+
@classmethod
57+
def create_sold_property(cls):
58+
cls.sold_property = cls.env["estate.property"].create([{
59+
"name": "Sold Property",
60+
"state": "new",
61+
"expected_price": 4000.0
62+
}])
63+
cls.env["estate.property.offer"].create([{
64+
"partner_id": ADMINISTRATOR_ID,
65+
"property_id": cls.sold_property.id,
66+
"status": "accepted",
67+
"price": 4000.0
68+
}])
69+
cls.sold_property.state = "sold"
70+
71+
72+
def test_offer_for_sold_property(self):
73+
offer_for_sold_property = {
74+
"partner_id": ADMINISTRATOR_ID,
75+
"property_id": self.sold_property.id
76+
}
77+
correct_offers = [
78+
{"partner_id": ADMINISTRATOR_ID, "property_id": self.new_property.id, "price": 1000},
79+
{"partner_id": ADMINISTRATOR_ID, "property_id": self.received_property.id, "price": 2000},
80+
{"partner_id": ADMINISTRATOR_ID, "property_id": self.accepted_property.id, "price": 3000},
81+
{"partner_id": ADMINISTRATOR_ID, "property_id": self.cancelled_property.id, "price": 5000},
82+
]
83+
with self.assertRaises(UserError, msg="Creating an offer for a sold property should raise a UserError but it did not."):
84+
self.env["estate.property.offer"].create([offer_for_sold_property])
85+
for offer in correct_offers:
86+
try:
87+
self.env["estate.property.offer"].create([offer])
88+
except UserError:
89+
self.fail()
90+
91+
def test_sell_property_with_no_accepted_offer(self):
92+
non_sellable_properties = [
93+
self.new_property,
94+
self.received_property
95+
]
96+
for property in non_sellable_properties:
97+
with self.assertRaises(UserError):
98+
property.action_mark_as_sold()
99+
self.accepted_property.action_mark_as_sold()
100+
101+
def test_sold_state_correctly_set(self):
102+
self.accepted_property.mark_as_sold()
103+
self.assertEqual(self.accepted_property.state, "sold", "When a property is sold, its state should be set to 'sold'.")
104+
105+
def test_garden_area_orientation_reset(self):
106+
property_form = Form(self.env["estate.property"])
107+
self.assertFalse(property_form.garden)
108+
self.assertEqual(property_form.garden_area, 0)
109+
self.assertFalse(property_form.garden_orientation)
110+
property_form.garden = True
111+
self.assertTrue(property_form.garden)
112+
self.assertEqual(property_form.garden_area, 10)
113+
self.assertEqual(property_form.garden_orientation, "north")
114+
property_form.garden = False
115+
self.assertFalse(property_form.garden)
116+
self.assertEqual(property_form.garden_area, 0)
117+
self.assertFalse(property_form.garden_orientation)

0 commit comments

Comments
 (0)