[16.0][MIG] account_analytic_distribution_model_recalculate#837
[16.0][MIG] account_analytic_distribution_model_recalculate#837andrel-exo wants to merge 1 commit intoOCA:16.0from
Conversation
9c2b430 to
721079e
Compare
721079e to
f9a2773
Compare
|
There hasn't been any activity on this pull request in the past 4 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days. |
marcos-mendez
left a comment
There was a problem hiding this comment.
Automated Review -- Tests Failed
1. Root Cause
The test failure occurs because the method _check_duplicate_dates() is called on an account.analytic.distribution.model object, but this method does not exist in the current implementation. It seems like the test expects a constraint to be enforced for overlapping date ranges, but the corresponding validation logic was not implemented or is missing.
2. Suggested Fix
Add the _check_duplicate_dates() method to the AccountAnalyticDistributionModel class in account_analytic_distribution_model.py. This method should validate that no two models exist with overlapping date ranges for the same partner and account prefix.
# In account_analytic_distribution_model.py
@api.constrains("start_date", "end_date", "partner_id", "account_prefix")
def _check_duplicate_dates(self):
for record in self:
if not record.start_date or not record.end_date:
continue
# Find other records with overlapping dates and same partner/account
domain = [
("id", "!=", record.id),
("start_date", "!=", False),
("end_date", "!=", False),
("start_date", "<=", record.end_date),
("end_date", ">=", record.start_date),
("partner_id", "=", record.partner_id.id),
("account_prefix", "=", record.account_prefix),
]
overlapping = self.search(domain)
if overlapping:
raise ValidationError(
_("Cannot have overlapping dates for the same partner and account prefix.")
)This method should be added after the _check_start_date_before_end_date constraint and before the _get_distribution method.
3. Additional Code Issues
- Missing
@api.constrainsdecorator: The_check_duplicate_datesmethod is not decorated with@api.constrains, which means it will not be automatically triggered during record creation or update. - Incomplete
_get_distributionmethod: The_get_distributionmethod is defined but only includes a docstring and no actual logic. It should be implemented to support date filtering logic when retrieving distribution models.
4. Test Improvements
Add the following test cases to improve coverage:
a. Test overlapping date constraints
Use TransactionCase or SavepointCase to test that creating two models with overlapping dates raises a ValidationError.
def test_overlapping_dates_constraint(self):
model1 = self.env['account.analytic.distribution.model'].create({
'partner_id': self.partner.id,
'account_prefix': '400',
'start_date': '2024-01-01',
'end_date': '2024-12-31',
})
with self.assertRaises(ValidationError):
self.env['account.analytic.distribution.model'].create({
'partner_id': self.partner.id,
'account_prefix': '400',
'start_date': '2024-06-01',
'end_date': '2025-06-01',
})b. Test date filtering in _get_distribution
Ensure that _get_distribution respects start_date and end_date filters by creating journal items with different dates and asserting correct model selection.
def test_distribution_model_date_filtering(self):
model = self.env['account.analytic.distribution.model'].create({
'partner_id': self.partner.id,
'account_prefix': '400',
'start_date': '2024-01-01',
'end_date': '2024-12-31',
})
# Create move line with date within range
move_line = self.env['account.move.line'].create({
'move_id': self.invoice.id,
'account_id': self.account.id,
'partner_id': self.partner.id,
'date': '2024-06-01',
})
# Check if model is correctly applied
distribution = model._get_distribution({'account_id': self.account.id})
self.assertTrue(distribution)c. Use @tagged for better test organization
Tag tests with @tagged('post_install', 'manual') or similar to ensure they run in appropriate environments.
from odoo.tests import tagged
@tagged('post_install', 'manual')
def test_constraints(self):
...These improvements will ensure robustness and better test coverage for the new date-based filtering logic.
Environment via OCA Neural Reviewer: Minikube + K8s Job + oca-ci/py3.10-odoo16.0 | Odoo 16.0
Automated review by OCA Neural Reviewer + qwen3-coder:30b
account_analytic_distribution_model_recalculate: Migration to 16.0
attrs