Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3853694

Browse files
author
Siddharta Govindaraj
committed
Testing interactions approach 1 - no mocking
1 parent 0854694 commit 3853694

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

stock_alerter/alert.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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)

stock_alerter/tests/test_alert.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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)

0 commit comments

Comments
 (0)