154 lines
4.9 KiB
Python
154 lines
4.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo.tests.common import TransactionCase
|
|
from odoo.exceptions import UserError
|
|
|
|
|
|
class TestModelInheritance(TransactionCase):
|
|
"""Test cases for model inheritance and extensions."""
|
|
|
|
def setUp(self):
|
|
"""Set up test data."""
|
|
super().setUp()
|
|
|
|
self.Model = self.env['base.model.name'] # The model being extended
|
|
|
|
# Set up test data
|
|
self.partner = self.env['res.partner'].search([], limit=1)
|
|
if not self.partner:
|
|
self.skipTest("No partner available for testing")
|
|
|
|
def test_01_new_fields_exist(self):
|
|
"""Test that new fields added by inheritance exist."""
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
# Base fields
|
|
})
|
|
|
|
# Test that new fields exist and have default values
|
|
self.assertTrue(hasattr(record, 'new_field'))
|
|
self.assertEqual(record.new_field, False) # Or expected default
|
|
|
|
def test_02_inherited_method_override(self):
|
|
"""Test that overridden methods work correctly."""
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
})
|
|
|
|
# Call overridden method
|
|
result = record.action_confirm()
|
|
|
|
# Verify custom behavior was applied
|
|
# self.assertEqual(record.state, 'custom_state')
|
|
self.assertTrue(result)
|
|
|
|
def test_03_super_call_behavior(self):
|
|
"""Test that super() calls preserve base functionality."""
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
})
|
|
|
|
initial_state = record.state
|
|
|
|
# Call overridden method that should call super()
|
|
record.write({'name': 'Updated Record'})
|
|
|
|
# Verify both base and custom behavior applied
|
|
self.assertEqual(record.name, 'Updated Record') # Base behavior
|
|
# self.assertEqual(record.custom_field, 'value') # Custom behavior
|
|
|
|
def test_04_added_constraint(self):
|
|
"""Test new constraints added by inheritance."""
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
'new_field': 'valid_value',
|
|
})
|
|
|
|
# Test new constraint
|
|
with self.assertRaises(UserError) as context:
|
|
record.write({'new_field': 'invalid_value'})
|
|
|
|
self.assertIn('expected error', str(context.exception))
|
|
|
|
def test_05_computed_field_extension(self):
|
|
"""Test computed fields added by inheritance."""
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
'quantity': 10,
|
|
'unit_price': 5.0,
|
|
})
|
|
|
|
# Test new computed field
|
|
self.assertEqual(record.total_with_tax, 52.5) # Example with 5% tax
|
|
|
|
def test_06_onchange_extension(self):
|
|
"""Test onchange methods added by inheritance."""
|
|
record = self.Model.new({
|
|
'name': 'Test Record',
|
|
})
|
|
|
|
# Trigger new onchange
|
|
record.partner_id = self.partner
|
|
record._onchange_partner_id_custom()
|
|
|
|
# Verify custom onchange behavior
|
|
# self.assertEqual(record.custom_field, expected_value)
|
|
|
|
def test_07_api_depends_extension(self):
|
|
"""Test that @api.depends works correctly on inherited fields."""
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
'line_ids': [(0, 0, {
|
|
'product_id': self.env['product.product'].search([], limit=1).id,
|
|
'quantity': 5,
|
|
'price_unit': 10.0,
|
|
})],
|
|
})
|
|
|
|
# Initial computed value
|
|
initial_total = record.total_amount
|
|
|
|
# Add more lines
|
|
record.write({
|
|
'line_ids': [(0, 0, {
|
|
'product_id': self.env['product.product'].search([], limit=1).id,
|
|
'quantity': 3,
|
|
'price_unit': 20.0,
|
|
})],
|
|
})
|
|
|
|
# Verify recomputation
|
|
self.assertGreater(record.total_amount, initial_total)
|
|
|
|
def test_08_prevent_base_operation(self):
|
|
"""Test blocking base operations with custom validation."""
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
'state': 'draft',
|
|
})
|
|
|
|
# Transition to state that prevents deletion
|
|
record.write({'state': 'confirmed'})
|
|
|
|
# Test that deletion is now blocked
|
|
with self.assertRaises(UserError) as context:
|
|
record.unlink()
|
|
|
|
self.assertIn('cannot delete', str(context.exception).lower())
|
|
|
|
def test_09_backward_compatibility(self):
|
|
"""Test that base functionality still works after inheritance."""
|
|
# Test base model functionality isn't broken
|
|
record = self.Model.create({
|
|
'name': 'Test Record',
|
|
})
|
|
|
|
# Base operations should still work
|
|
record.write({'name': 'Updated Name'})
|
|
self.assertEqual(record.name, 'Updated Name')
|
|
|
|
# Base methods should still be callable
|
|
if hasattr(record, 'base_method'):
|
|
result = record.base_method()
|
|
self.assertTrue(result)
|