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

feat: Support to declare States using Enum. Closes #367 #375

Merged
merged 5 commits into from
Mar 19, 2023

Conversation

fgmacedo
Copy link
Owner

@fgmacedo fgmacedo commented Mar 15, 2023

Add support to declare States using Enum.

Given that you assign the response of States.from_enum to a class level
variable on your StateMachine you're good to go, you can use any name.
The variable will be inspected by the metaclass and the State instances
assigned to the state machine.

Enum campaign machine

A StateMachine that demonstrates using an Enum as source for States declaration.

from enum import Enum

from statemachine import StateMachine
from statemachine.states import States


class CampaignStatus(Enum):
    draft = 1
    producing = 2
    closed = 3


class CampaignMachine(StateMachine):
    "A workflow machine"

    _ = States.from_enum(
        CampaignStatus, initial=CampaignStatus.draft, final=CampaignStatus.closed
    )

    add_job = _.draft.to(_.draft) | _.producing.to(_.producing)
    produce = _.draft.to(_.producing)
    deliver = _.producing.to(_.closed)

Asserting campaign machine declaration

assert CampaignMachine.draft.initial
assert not CampaignMachine.draft.final

assert not CampaignMachine.producing.initial
assert not CampaignMachine.producing.final

assert not CampaignMachine.closed.initial
assert CampaignMachine.closed.final

Testing our campaign machine

sm = CampaignMachine()
res = sm.send("produce")

assert sm.draft.is_active is False
assert sm.producing.is_active is True
assert sm.closed.is_active is False
assert sm.current_state == sm.producing
assert sm.current_state_value == CampaignStatus.producing.value

@codecov
Copy link

codecov bot commented Mar 15, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (2420b18) 100.00% compared to head (743e8ff) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #375   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           19        20    +1     
  Lines          936       976   +40     
  Branches       154       161    +7     
=========================================
+ Hits           936       976   +40     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

Impacted Files Coverage Δ
statemachine/registry.py 100.00% <ø> (ø)
statemachine/state.py 100.00% <ø> (ø)
statemachine/factory.py 100.00% <100.00%> (ø)
statemachine/model.py 100.00% <100.00%> (ø)
statemachine/statemachine.py 100.00% <100.00%> (ø)
statemachine/states.py 100.00% <100.00%> (ø)
statemachine/transition_list.py 100.00% <100.00%> (ø)
statemachine/utils.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@fgmacedo fgmacedo marked this pull request as ready for review March 17, 2023 01:13
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@fgmacedo fgmacedo merged commit 0a15aa7 into develop Mar 19, 2023
@fgmacedo fgmacedo deleted the macedo/from-enum branch March 19, 2023 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant