Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
1 Svar
142 Visninger

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? 

Avatar
Kassér
Forfatter Bedste svar

OK, I might have found an answer to this, and wow it's weird.

So when we write to a model the write_date appears to be set to the time of the start of the database transaction. I present as evidence the following. There are a few other places in the file where cr.now() is used.

https://github.com/odoo/odoo/blob/18.0/odoo/models.py#L4732

My unit test is within one transaction. It's not that the write_date isn't updated when I modify the article. It is set to the same date as before, because we're in the same transaction. Rather than the actual time which includes a few seconds that have passed (I did fix the position of the sleep() call from OP)

Because my computed field checks write_date, it seems there are some barriers to testing it

Avatar
Kassér
Related Posts Besvarelser Visninger Aktivitet
2
nov. 25
132
3
sep. 25
1260
4
aug. 25
1964
1
jun. 25
1635
1
maj 25
1664