Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 18, 2024

Conversation

fgmacedo
Copy link
Owner

@fgmacedo fgmacedo commented Apr 16, 2024

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

  • Test with simple deep copy.
  • Test with observers added to the state machine.

@fgmacedo fgmacedo added the bug label Apr 16, 2024
Copy link

codecov bot commented Apr 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #425   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           20        20           
  Lines         1131      1144   +13     
  Branches       164       164           
=========================================
+ Hits          1131      1144   +13     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

sonarcloud bot commented Apr 17, 2024

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

@fgmacedo fgmacedo merged commit 16f2a73 into develop Apr 18, 2024
14 checks passed
@fgmacedo fgmacedo deleted the macedo/fix-deepcopy branch April 18, 2024 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Models instantiated in Djangos setUpTestData seem to skip conditions
1 participant