Skip to content

Commit

Permalink
[IMP] estate: Tutorial - unit tests, with the exception of the form-r…
Browse files Browse the repository at this point in the history
…elated one
  • Loading branch information
nape-odoo committed Nov 26, 2024
1 parent 9fba6c2 commit a2bb0c2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
8 changes: 5 additions & 3 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions estate/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import test_estate_property
from . import test_estate_property_offer
53 changes: 53 additions & 0 deletions estate/tests/test_estate_property.py
Original file line number Diff line number Diff line change
@@ -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')
34 changes: 34 additions & 0 deletions estate/tests/test_estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -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,
})

0 comments on commit a2bb0c2

Please sign in to comment.