Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions auditlog/models/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ def __enter__(self):
don't swap them all out here.
"""
self._original_cache = self._transaction.cache
# Copy the sets of records, which are popped on recompute but do not
# copy the keys because they do not match the original field object
# afterwards.
self._original_tocompute = defaultdict(OrderedSet)
# Also swap out the list of fields to recompute. Their compute methods
# may depend on fields in the cache that are not yet flushed, and as is
# the case with account.bank.statement.line's _compute_internal_index,
# may not be resilient to some of the values (c.q. 'date') missing.
self._original_tocompute = self._transaction.tocompute
self._transaction.tocompute = defaultdict(OrderedSet)
for key, value in self._transaction.tocompute.items():
self._original_tocompute[key] = OrderedSet(value)
temporary_cache = api.Cache()
Expand Down
1 change: 1 addition & 0 deletions test_auditlog/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import test_account_bank_statement_line
from . import test_account_move_reverse
from . import test_product_tax_multicompany
52 changes: 52 additions & 0 deletions test_auditlog/tests/test_account_bank_statement_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from odoo.tests import tagged

from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.addons.auditlog.tests.common import AuditLogRuleCommon


@tagged("post_install", "-at_install")
class TestAccountBankStatementLine(AccountTestInvoicingCommon, AuditLogRuleCommon):
def setUp(self):
super().setUp()
self.rule = self.env["auditlog.rule"].create(
{
"name": __name__,
"model_id": self.env.ref("account.model_account_move").id,
"log_read": True,
"log_create": True,
"log_write": True,
"log_unlink": True,
"log_type": "full",
}
)
self.rule.subscribe()

def test_create_statement_line(self):
"""Statement line can be created with logging on journal entries enabled.

Because we swap out the cache when fetching previous values during full
logging using the ThrowAwayCache, some values that are assumed by
compute methods (c.q. 'date' in account.bank.statement.line's
_compute_internal_index) might be missing. If a recompute of those fields
is inadvertently triggered when using the ThrowAwayCache, the missing
values will raise an exception (in this case: `AttributeError: 'bool'
object has no attribute 'strftime'`). This test verifies that the queued
recomputes are consistent with the values in the cache such that this
exception does not occur.
"""
partner = self.env["res.partner"].create({"name": "test"})
stmt = self.env["account.bank.statement"].create(
{"journal_id": self.company_data["default_journal_bank"].id}
)
line = self.env["account.bank.statement.line"].create(
{
"date": "2023-04-01",
"account_number": "NL45 TRIO 0198100000",
"amount": 5.75,
"journal_id": self.company_data["default_journal_bank"].id,
"payment_ref": "1234",
"partner_id": partner.id,
"statement_id": stmt.id,
},
)
line.flush_recordset()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add an assert?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Without the fix, this test (with the flush) reproduces the issue. There is no specific value to assert.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that it tests the code that no error is raised, however, it would be nice to add an assert to know what we are expecting (a created auditlog for example)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be misleading, because that is not the goal of the test. Checking that auditlogs are created is covered by lots of other tests already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, the test must include the reason of it's existance. Without checking the PR it is not clear why we need this test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, thanks! I now added documentation of the issue and the exception it causes to the test method docstring.