- Mailing Lists
- Contributors
- Re: Odoo Test framework and rollback issue
Archives
- By thread 1419
-
By date
- August 2019 59
- September 2019 118
- October 2019 165
- November 2019 97
- December 2019 35
- January 2020 58
- February 2020 204
- March 2020 121
- April 2020 172
- May 2020 50
- June 2020 158
- July 2020 85
- August 2020 94
- September 2020 193
- October 2020 277
- November 2020 100
- December 2020 159
- January 2021 38
- February 2021 87
- March 2021 146
- April 2021 73
- May 2021 90
- June 2021 86
- July 2021 123
- August 2021 50
- September 2021 68
- October 2021 66
- November 2021 74
- December 2021 75
- January 2022 98
- February 2022 77
- March 2022 68
- April 2022 31
- May 2022 59
- June 2022 87
- July 2022 141
- August 2022 38
- September 2022 73
- October 2022 152
- November 2022 39
- December 2022 50
- January 2023 93
- February 2023 49
- March 2023 106
- April 2023 47
- May 2023 69
- June 2023 92
- July 2023 64
- August 2023 103
- September 2023 91
- October 2023 101
- November 2023 94
- December 2023 46
- January 2024 75
- February 2024 79
- March 2024 104
- April 2024 63
- May 2024 40
- June 2024 160
- July 2024 80
- August 2024 70
- September 2024 62
- October 2024 121
- November 2024 117
- December 2024 89
- January 2025 59
- February 2025 104
- March 2025 96
- April 2025 107
- May 2025 52
- June 2025 72
- July 2025 60
- August 2025 81
- September 2025 124
- October 2025 63
- November 2025 22
Contributors
Re: Odoo Test framework and rollback issue
Holger Brunn has pointed to me that there's a way to recover properly in SavepointCase tests after a raise:
You have to add the reset_on_failure to your `with` statement. And it seems that since v15, the savepoint is enough.
Anyway, think twice about piling a lot of asserts in the same test, being raise or not, as having an early failure makes you to iterate several times until having a pass one, although testing flows is usual to have that sequential asserts.
Thanks for the extra insights, Holger!
Regards.
by Pedro M. Baeza - 01:41 - 17 Nov 2023
Reference
-
Odoo Test framework and rollback issue
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.
by Yann Papouin - 06:16 - 15 Nov 2023-
Re: Odoo Test framework and rollback issue
Thank you all for your feedback.So I will (when possible) try to split my tests and re-arrange their content so that the exception assertions are (is) processed at the very end.Anyway, I will always add the self.cr.savepoint() in the with context (looking at the Odoo 14.0 code, clear_upon_failure is automatically called by assertRaises).--
Yann PAPOUINLe ven. 17 nov. 2023 à 13:42, Pedro M. Baeza <notifications@odoo-community.org> a écrit :Holger Brunn has pointed to me that there's a way to recover properly in SavepointCase tests after a raise:You have to add the reset_on_failure to your `with` statement. And it seems that since v15, the savepoint is enough.Anyway, think twice about piling a lot of asserts in the same test, being raise or not, as having an early failure makes you to iterate several times until having a pass one, although testing flows is usual to have that sequential asserts.Thanks for the extra insights, Holger!Regards._______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe
by Yann Papouin - 02:51 - 23 Nov 2023 -
Re: Odoo Test framework and rollback issue
Holger Brunn has pointed to me that there's a way to recover properly in SavepointCase tests after a raise:You have to add the reset_on_failure to your `with` statement. And it seems that since v15, the savepoint is enough.Anyway, think twice about piling a lot of asserts in the same test, being raise or not, as having an early failure makes you to iterate several times until having a pass one, although testing flows is usual to have that sequential asserts.Thanks for the extra insights, Holger!Regards.
by Pedro M. Baeza - 01:41 - 17 Nov 2023 -
Re: Odoo Test framework and rollback issue
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#L367Regards,lmiOn 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
by Laurent Mignon - 08:51 - 16 Nov 2023 -
Re: Odoo Test framework and rollback issue
It's just a problem with how Odoo handles rollbacks and savepoints in tests. The thumb rule is to not do anything more after testing a raise. Do it in another test method.Regards.
by Pedro M. Baeza - 07:47 - 15 Nov 2023
-