Initial commit
This commit is contained in:
146
skills/odoo-test-creator/assets/test_model_basic.py
Normal file
146
skills/odoo-test-creator/assets/test_model_basic.py
Normal file
@@ -0,0 +1,146 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from odoo.tests.common import TransactionCase
|
||||
from odoo.exceptions import UserError, ValidationError
|
||||
|
||||
|
||||
class TestModelName(TransactionCase):
|
||||
"""Test cases for model.name functionality."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
super().setUp()
|
||||
|
||||
self.Model = self.env['model.name']
|
||||
|
||||
# Use existing records when possible
|
||||
self.partner = self.env['res.partner'].search([], limit=1)
|
||||
if not self.partner:
|
||||
self.skipTest("No partner available for testing")
|
||||
|
||||
# Or create with .sudo()
|
||||
self.test_partner = self.env['res.partner'].sudo().create({
|
||||
'name': 'Test Partner',
|
||||
'is_company': True,
|
||||
})
|
||||
|
||||
def test_01_create_record(self):
|
||||
"""Test creating a new record with valid data."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
# Add other required fields
|
||||
})
|
||||
|
||||
self.assertTrue(record)
|
||||
self.assertEqual(record.name, 'Test Record')
|
||||
self.assertEqual(record.state, 'draft') # Adjust as needed
|
||||
|
||||
def test_02_update_record(self):
|
||||
"""Test updating an existing record."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
# Update record
|
||||
record.write({
|
||||
'name': 'Updated Record',
|
||||
})
|
||||
|
||||
self.assertEqual(record.name, 'Updated Record')
|
||||
|
||||
def test_03_search_records(self):
|
||||
"""Test searching for records."""
|
||||
# Create test records
|
||||
self.Model.create({
|
||||
'name': 'Record A',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
self.Model.create({
|
||||
'name': 'Record B',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
# Search for records
|
||||
records = self.Model.search([('partner_id', '=', self.partner.id)])
|
||||
|
||||
self.assertGreaterEqual(len(records), 2)
|
||||
|
||||
def test_04_computed_field(self):
|
||||
"""Test computed field calculation."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
'quantity': 10,
|
||||
'unit_price': 5.0,
|
||||
})
|
||||
|
||||
# Test computed total
|
||||
self.assertEqual(record.total_amount, 50.0)
|
||||
|
||||
# Update dependency and verify recomputation
|
||||
record.write({'quantity': 20})
|
||||
self.assertEqual(record.total_amount, 100.0)
|
||||
|
||||
def test_05_onchange_method(self):
|
||||
"""Test onchange method behavior."""
|
||||
record = self.Model.new({
|
||||
'name': 'Test Record',
|
||||
})
|
||||
|
||||
# Trigger onchange
|
||||
record.partner_id = self.partner
|
||||
record._onchange_partner_id()
|
||||
|
||||
# Verify onchange updated fields
|
||||
# self.assertEqual(record.some_field, expected_value)
|
||||
|
||||
def test_06_constraint_validation(self):
|
||||
"""Test constraint validation."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
# Test that invalid value raises ValidationError
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
record.write({'invalid_field': 'invalid_value'})
|
||||
|
||||
# Verify error message
|
||||
self.assertIn('expected error message', str(context.exception))
|
||||
|
||||
def test_07_state_transition(self):
|
||||
"""Test state transition workflow."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
# Initially in draft state
|
||||
self.assertEqual(record.state, 'draft')
|
||||
|
||||
# Confirm record
|
||||
record.action_confirm()
|
||||
self.assertEqual(record.state, 'confirmed')
|
||||
|
||||
# Test invalid transition
|
||||
with self.assertRaises(UserError) as context:
|
||||
record.action_confirm() # Already confirmed
|
||||
|
||||
self.assertIn('Cannot confirm', str(context.exception))
|
||||
|
||||
def test_08_delete_record(self):
|
||||
"""Test deleting a record (if applicable)."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
record_id = record.id
|
||||
|
||||
# Delete record
|
||||
record.unlink()
|
||||
|
||||
# Verify record no longer exists
|
||||
self.assertFalse(self.Model.browse(record_id).exists())
|
||||
144
skills/odoo-test-creator/assets/test_model_constraints.py
Normal file
144
skills/odoo-test-creator/assets/test_model_constraints.py
Normal file
@@ -0,0 +1,144 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from odoo.tests.common import TransactionCase
|
||||
from odoo.exceptions import ValidationError
|
||||
from psycopg2 import IntegrityError
|
||||
|
||||
|
||||
class TestModelConstraints(TransactionCase):
|
||||
"""Test cases for model constraints and validation."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
super().setUp()
|
||||
|
||||
self.Model = self.env['model.name']
|
||||
|
||||
# Set up minimal 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_python_constraint_positive_value(self):
|
||||
"""Test Python constraint for positive values."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
'amount': 100.0,
|
||||
})
|
||||
|
||||
# Test valid positive value
|
||||
record.write({'amount': 50.0})
|
||||
self.assertEqual(record.amount, 50.0)
|
||||
|
||||
# Test that negative value raises ValidationError
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
record.write({'amount': -10.0})
|
||||
|
||||
self.assertIn('must be positive', str(context.exception).lower())
|
||||
|
||||
def test_02_sql_constraint_unique(self):
|
||||
"""Test SQL constraint for unique values."""
|
||||
# Create first record
|
||||
self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'code': 'UNIQUE001',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
# Try to create duplicate
|
||||
with self.assertRaises(IntegrityError):
|
||||
with self.cr.savepoint():
|
||||
self.Model.create({
|
||||
'name': 'Test Record 2',
|
||||
'code': 'UNIQUE001', # Duplicate code
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
def test_03_required_field_validation(self):
|
||||
"""Test that required fields are enforced."""
|
||||
# Test missing required field raises ValidationError
|
||||
with self.assertRaises(ValidationError):
|
||||
self.Model.create({
|
||||
'name': 'Test Record',
|
||||
# Missing required 'partner_id'
|
||||
})
|
||||
|
||||
def test_04_field_domain_constraint(self):
|
||||
"""Test field domain constraints."""
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
'state': 'draft',
|
||||
})
|
||||
|
||||
# Test valid state
|
||||
record.write({'state': 'confirmed'})
|
||||
self.assertEqual(record.state, 'confirmed')
|
||||
|
||||
# Test invalid state raises ValidationError
|
||||
with self.assertRaises(ValidationError):
|
||||
record.write({'state': 'invalid_state'})
|
||||
|
||||
def test_05_dependent_field_constraint(self):
|
||||
"""Test constraints that depend on multiple fields."""
|
||||
# Test that start_date must be before end_date
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
'start_date': '2024-12-31',
|
||||
'end_date': '2024-01-01', # End before start
|
||||
})
|
||||
|
||||
self.assertIn('end date', str(context.exception).lower())
|
||||
self.assertIn('start date', str(context.exception).lower())
|
||||
|
||||
def test_06_conditional_constraint(self):
|
||||
"""Test constraints that apply conditionally."""
|
||||
# Create record in state where constraint doesn't apply
|
||||
record = self.Model.create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
'state': 'draft',
|
||||
'approval_required': False,
|
||||
})
|
||||
|
||||
# Confirm - now constraint should apply
|
||||
record.write({'state': 'confirmed', 'approval_required': True})
|
||||
|
||||
# Test that missing approval raises error
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
record.write({'approved_by': False}) # Clear approval
|
||||
|
||||
self.assertIn('approval', str(context.exception).lower())
|
||||
|
||||
def test_07_cascading_constraint(self):
|
||||
"""Test constraints that cascade to related records."""
|
||||
parent = self.Model.create({
|
||||
'name': 'Parent Record',
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
child = self.Model.create({
|
||||
'name': 'Child Record',
|
||||
'parent_id': parent.id,
|
||||
'partner_id': self.partner.id,
|
||||
})
|
||||
|
||||
# Test that deleting parent with children raises error
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
parent.unlink()
|
||||
|
||||
self.assertIn('child', str(context.exception).lower())
|
||||
|
||||
def test_08_constraint_bypass_with_context(self):
|
||||
"""Test bypassing constraints with context (if applicable)."""
|
||||
# Some constraints can be bypassed with special context
|
||||
record = self.Model.with_context(skip_validation=True).create({
|
||||
'name': 'Test Record',
|
||||
'partner_id': self.partner.id,
|
||||
'amount': -100.0, # Normally not allowed
|
||||
})
|
||||
|
||||
self.assertEqual(record.amount, -100.0)
|
||||
153
skills/odoo-test-creator/assets/test_model_inheritance.py
Normal file
153
skills/odoo-test-creator/assets/test_model_inheritance.py
Normal file
@@ -0,0 +1,153 @@
|
||||
# -*- 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)
|
||||
Reference in New Issue
Block a user