Hello,
I have been writing an extension which tracks when users have "marked as read" a given document in Odoo Knowledge. I am working on adding unit testing. I would like to ask what I might be doing wrong and what best practices are for testing in this context.
Here is an example of a unit test that fails, where as the actual behaviour seems to work in manual testing.
def test_ack_outdated(self):
"""
When a user acknowledges an article, 2s pass, then the article changes,
the ack is marked outdated.
"""
self.article_1.must_acknowledge = True
self.article_1.with_user(self.user_1).verify_acknowledgement()
self.article_1.sudo().write({"body": "<h1>Updated Article</h1>"})
self.env.flush_all() # Flush changes to DB session after writing!
sleep(2.0)
ack_record = self.env['knowledge.article.ack'].search([
('user_id', '=', self.user_1.id),
('article_id', '=', self.article_1.id)
])
self.assertEqual(ack_record.user_id.id, self.user_1.id)
self.assertEqual(ack_record.article_id.id, self.article_1.id)
self.assertIsNotNone(ack_record.ack_datetime)
self.assertTrue(ack_record.is_outdated)
The final assertion fails - the acknowledgment by the user of the article has not been marked as outdated. The field is computed as follows - it is very simple.
ack_datetime = fields.Datetime(
string='Acknowledged Date & Time',
default=fields.Datetime.now
)
is_outdated = fields.Boolean(
compute='_compute_is_outdated',
store=True,
)
@api.depends('ack_datetime', 'article_id.write_date')
def _compute_is_outdated(self):
for rec in self:
rec.is_outdated = all([
rec.ack_datetime,
rec.article_id.write_date,
rec.ack_datetime <= rec.article_id.write_date
])
Also, should i be using the Form class from the testing modules provided by Odoo?