Contributors mailing list archives

contributors@odoo-community.org

Browse archives

Avatar

Re: Odoo Test framework and rollback issue

by
Acsone SA/NV, Laurent Mignon
- 16/11/2023 08:46:01
Hi,

In Odoo 14, you should append self.cr.savepoint() when you want to add additional tests after the assertRaises. It's required to restore the db cursor, the environment and the cache at the state prior to the exception. You should also question why you want to add additional tests after this assertion? Maybe they can be put into another specific test method. In Odoo 16 (at least), odoo overrides the assertRaises method to add the call to the savepoint and ensures a cleanup of the environment. https://github.com/odoo/odoo/blob/16.0/odoo/tests/common.py#L367

Regards,

lmi

On Wed, Nov 15, 2023 at 6:24 PM Yann Papouin <notifications@odoo-community.org> wrote:
Hi everyone,

I'm currently developing on 14.0 a test for one of my module and I'm having a headache trying to understand why an action is not properly rolled back after the exception is raised.

My code sample:
class TestSoftwareLicensePass(TransactionCase):
    def test_activation(self):
        ...
        self.assertEqual(pass_lic1.get_remaining_activation(), 1)
        with self.assertRaisesRegex(ValidationError, r"Max activation reached"):
            pass_lic1.activate("device_uuid_4/3")
        self.assertEqual(pass_lic1.get_remaining_activation(), 1)
        with self.assertRaisesRegex(ValidationError, r"Max activation reached"):
            pass_lic1.activate("device_uuid_4/3")

The second assertEqual statement FAILS because get_remaining_activation() returns 0, but since the exception has been correctly captured by the  with self.assertRaisesRegex statement, this part should have been rolled back by the odoo framework.
(and no cr.commit in the activate function)

Looking in other tests, I can see that a lot of with self.assert statements have a self.cr.savepoint() at their side (like this code sample in odoo/odoo/addons/base/tests/test_views.py):

class TestViewInheritance(ViewCase):   
    def test_no_recursion(self):
        r1 = self.makeView('R1')
        with self.assertRaises(ValidationError), self.cr.savepoint():
            r1.write({'inherit_id': r1.id})

Is this related to the TransactionCase class ? (Probably not, I made a test with SavepointCase and same issue)
Should I always append self.cr.savepoint() ?

Yann.

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

Reference