mirror of
https://github.com/SARL-PACIFIC-ERP/odoo-sh-test.git
synced 2025-06-25 01:32:20 +00:00
Safeguard your code with unit tests
This commit is contained in:
parent
d8ca2b9684
commit
4487f48ff7
|
@ -104,6 +104,8 @@ class EstateProperty(models.Model):
|
||||||
if self.exists():
|
if self.exists():
|
||||||
if self.state == 'cancelled':
|
if self.state == 'cancelled':
|
||||||
raise exceptions.UserError('A cancelled property cannot be sold')
|
raise exceptions.UserError('A cancelled property cannot be sold')
|
||||||
|
if "accepted" not in self.offer_ids.mapped("state"):
|
||||||
|
raise exceptions.ValidationError('Cannot sell a property with no accepted offers')
|
||||||
else:
|
else:
|
||||||
self.state = 'sold'
|
self.state = 'sold'
|
||||||
return True
|
return True
|
||||||
|
@ -118,4 +120,4 @@ class EstateProperty(models.Model):
|
||||||
self.state = 'cancelled'
|
self.state = 'cancelled'
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
raise exceptions.MissingError('Property not found')
|
raise exceptions.MissingError('Property not found')
|
|
@ -29,8 +29,11 @@ class EstatePropertyOffer(models.Model):
|
||||||
@api.model_create_multi
|
@api.model_create_multi
|
||||||
def create(self, vals_list):
|
def create(self, vals_list):
|
||||||
for vals in vals_list:
|
for vals in vals_list:
|
||||||
if self.env['estate.property'].browse(vals['property_id']).state == 'new':
|
prop = self.env['estate.property'].browse(vals['property_id'])
|
||||||
self.env['estate.property'].browse(vals['property_id']).write({'state': 'offer_received'})
|
if prop.state == 'sold':
|
||||||
|
raise exceptions.UserError('Cannot create offer on a sold property')
|
||||||
|
elif prop.state == 'new':
|
||||||
|
prop.write({'state': 'offer_received'})
|
||||||
return super().create(vals_list)
|
return super().create(vals_list)
|
||||||
|
|
||||||
# ------------- Compute methods ------------------------- #
|
# ------------- Compute methods ------------------------- #
|
||||||
|
|
3
estate/tests/__init__.py
Normal file
3
estate/tests/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from . import test_property
|
104
estate/tests/test_property.py
Normal file
104
estate/tests/test_property.py
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
from odoo import Command
|
||||||
|
from odoo.exceptions import UserError, ValidationError
|
||||||
|
from odoo.tests.common import Form, TransactionCase
|
||||||
|
from odoo.tests import tagged
|
||||||
|
|
||||||
|
|
||||||
|
@tagged('post_install', '-at_install')
|
||||||
|
class EstateTestCase(TransactionCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super(EstateTestCase, cls).setUpClass()
|
||||||
|
|
||||||
|
# Properties
|
||||||
|
|
||||||
|
cls.property = cls.env['estate.property'].create({
|
||||||
|
'name': "Sick house",
|
||||||
|
'description' : "Will not actually make you sick",
|
||||||
|
'postcode': '12345',
|
||||||
|
'expected_price': 1000000.00,
|
||||||
|
'living_area': '100',
|
||||||
|
'facades': 4,
|
||||||
|
'garage': False,
|
||||||
|
'garden': False,
|
||||||
|
'property_type_id': cls.env.ref('estate.property_type_commercial').id
|
||||||
|
})
|
||||||
|
|
||||||
|
# Partners
|
||||||
|
|
||||||
|
cls.buyman = cls.env['res.partner'].create({
|
||||||
|
'name': 'Buyman',
|
||||||
|
})
|
||||||
|
cls.poorman = cls.env['res.partner'].create({
|
||||||
|
'name': 'Buyman',
|
||||||
|
})
|
||||||
|
cls.wooman = cls.env['res.partner'].create({
|
||||||
|
'name': 'Wooman',
|
||||||
|
})
|
||||||
|
|
||||||
|
# Offers
|
||||||
|
|
||||||
|
cls.property_offer_1 = cls.env['estate.property.offer'].create({
|
||||||
|
'price': 500000.00,
|
||||||
|
'partner_id': cls.poorman.id,
|
||||||
|
'property_id': cls.property.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
cls.property_offer_2 = cls.env['estate.property.offer'].create({
|
||||||
|
'price': 950000.50,
|
||||||
|
'partner_id': cls.buyman.id,
|
||||||
|
'property_id': cls.property.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
cls.property.offer_ids = [cls.property_offer_1.id, cls.property_offer_2.id]
|
||||||
|
|
||||||
|
def test_offer_creation_if_sold(self):
|
||||||
|
self.property_offer_2.action_accept()
|
||||||
|
self.property.action_sold()
|
||||||
|
|
||||||
|
self.assertEqual('sold', self.property.state)
|
||||||
|
|
||||||
|
with self.assertRaises(UserError):
|
||||||
|
self.env['estate.property.offer'].create({
|
||||||
|
'price': 10000.00,
|
||||||
|
'partner_id': self.poorman.id,
|
||||||
|
'property_id': self.property.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
def test_sell_property_no_offer(self):
|
||||||
|
property = self.env['estate.property'].create({
|
||||||
|
'name': "Test house",
|
||||||
|
'postcode': '12345',
|
||||||
|
'expected_price': 1000000.00,
|
||||||
|
'living_area': '100',
|
||||||
|
'facades': 4,
|
||||||
|
'garage': False,
|
||||||
|
'garden': False,
|
||||||
|
'property_type_id': self.env.ref('estate.property_type_residential').id
|
||||||
|
})
|
||||||
|
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
property.action_sold()
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
self.property.action_sold()
|
||||||
|
|
||||||
|
def test_reset_garden_area(self):
|
||||||
|
with Form(self.property) as property:
|
||||||
|
self.assertEqual(0, property.garden_area)
|
||||||
|
self.assertIs(False, property.garden_orientation)
|
||||||
|
|
||||||
|
property.garden = True
|
||||||
|
|
||||||
|
self.assertEqual(10, property.garden_area)
|
||||||
|
self.assertEqual('north', property.garden_orientation)
|
||||||
|
|
||||||
|
property.garden = False
|
||||||
|
self.assertEqual(0, property.garden_area)
|
||||||
|
self.assertIs(False, property.garden_orientation)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue