File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Alert :
2
+ """Maps a Rule to an Action, and triggers the action if the rule
3
+ matches on any stock update"""
4
+
5
+ def __init__ (self , description , rule , action ):
6
+ self .description = description
7
+ self .rule = rule
8
+ self .action = action
9
+
10
+ def connect (self , exchange ):
11
+ self .exchange = exchange
12
+ dependent_stocks = self .rule .depends_on ()
13
+ for stock in dependent_stocks :
14
+ exchange [stock ].updated .connect (self .check_rule )
15
+
16
+ def check_rule (self , stock ):
17
+ if self .rule .matches (self .exchange ):
18
+ self .action .execute (self .description )
Original file line number Diff line number Diff line change
1
+ import unittest
2
+ from datetime import datetime
3
+
4
+ from ..alert import Alert
5
+ from ..rule import PriceRule
6
+ from ..stock import Stock
7
+
8
+
9
+ class TestAction :
10
+ executed = False
11
+
12
+ def execute (self , description ):
13
+ self .executed = True
14
+
15
+
16
+ class AlertTest (unittest .TestCase ):
17
+ def test_action_is_executed_when_rule_matches (self ):
18
+ exchange = {"GOOG" : Stock ("GOOG" )}
19
+ rule = PriceRule ("GOOG" , lambda stock : stock .price > 10 )
20
+ action = TestAction ()
21
+ alert = Alert ("sample alert" , rule , action )
22
+ alert .connect (exchange )
23
+ exchange ["GOOG" ].update (datetime (2014 , 2 , 10 ), 11 )
24
+ self .assertTrue (action .executed )
You can’t perform that action at this time.
0 commit comments