forked from fgmacedo/python-statemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_profiling.py
More file actions
49 lines (35 loc) · 1.37 KB
/
Copy pathtest_profiling.py
File metadata and controls
49 lines (35 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import weakref
from statemachine import State
from statemachine import StateMachine
class OrderControl(StateMachine):
waiting_for_payment = State(initial=True)
processing = State()
shipping = State()
completed = State(final=True)
add_to_order = waiting_for_payment.to(waiting_for_payment)
receive_payment = waiting_for_payment.to(
processing, cond="payments_enough"
) | waiting_for_payment.to(waiting_for_payment, unless="payments_enough")
process_order = processing.to(shipping, cond="payment_received")
ship_order = shipping.to(completed)
class Order:
def __init__(self):
self.order_total = 0
self.payments = []
self.payment_received = False
self.state_machine = OrderControl(model=weakref.proxy(self))
def payments_enough(self, amount):
return sum(self.payments) + amount >= self.order_total
def before_add_to_order(self, amount):
self.order_total += amount
return self.order_total
def on_receive_payment(self, amount):
self.payments.append(amount)
return self.payments
def after_receive_payment(self):
self.payment_received = True
def exercise_order():
order = Order()
assert order.state_machine.waiting_for_payment.is_active
def test_setup_performance(benchmark):
benchmark.pedantic(exercise_order, rounds=10, iterations=1000)