Skip to content

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

Merged
fgmacedo merged 1 commit into
developfrom
macedo/fix-deepcopy
Apr 18, 2024
Merged

Reconstructing callable references when using deepcopy over a SM.#425
fgmacedo merged 1 commit into
developfrom
macedo/fix-deepcopy

Conversation

@fgmacedo

@fgmacedo fgmacedo commented Apr 16, 2024

Copy link
Copy Markdown
Owner

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
@codecov

codecov Bot commented Apr 16, 2024

Copy link
Copy Markdown

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.

@fgmacedo
fgmacedo force-pushed the macedo/fix-deepcopy branch from 971db43 to 24f8254 Compare April 17, 2024 15:34
@sonarqubecloud

Copy link
Copy Markdown

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
@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