fgmacedo / python-statemachine

Python Finite State Machines made easy.
MIT License
854 stars 84 forks source link

Reconstructing callable references when using deepcopy over a SM. #425

Closed fgmacedo closed 5 months ago

fgmacedo commented 5 months ago

Fixes #424.

Issue

As declared on the related issue:

The MachineMixin for Django seems to behave differently when models are used within django tests. Oddly enough, any objects created during setUpTestData seem to cause transitions to bypass checks like conditions.

The root cause of the issue lies in the setUpTestData() method. Every cls attr created in this method is wrapped in a TestData class, which performs a deepcopy() for each test, so this is designed to allow safe alteration of objects between tests on the same TestCase instance.

However, this conflicts with the creation strategy of state machines. The references of properties/methods used as actions/conditions are computed at the time of creation. So, when the state machine is cloned, the references still point to the original instance.

Fix

We've added a hook on the deepcopy Python protocol, where we reconstruct the new instance callable references.

Tests

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (f236cad) to head (24f8254).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #425 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 20 20 Lines 1131 1144 +13 Branches 164 164 ========================================= + Hits 1131 1144 +13 ``` | [Flag](https://app.codecov.io/gh/fgmacedo/python-statemachine/pull/425/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fernando+Macedo) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/fgmacedo/python-statemachine/pull/425/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fernando+Macedo) | `100.00% <100.00%> (ø)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fernando+Macedo#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

sonarcloud[bot] commented 5 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud