|
2 | 2 | import collections
|
3 | 3 | from datetime import datetime, timedelta
|
4 | 4 |
|
5 |
| -from nose2.tools.params import params |
6 |
| -from nose2.tools import such |
7 |
| - |
8 | 5 | from ..stock import Stock, StockSignal
|
9 | 6 |
|
10 | 7 |
|
11 |
| -def setup_test(): |
12 |
| - global goog |
13 |
| - goog = Stock("GOOG") |
14 |
| - |
15 |
| - |
16 |
| -def teardown_test(): |
17 |
| - global goog |
18 |
| - goog = None |
19 |
| - |
20 |
| - |
21 |
| -def test_price_of_a_new_stock_class_should_be_None(): |
22 |
| - assert goog.price is None |
23 |
| -test_price_of_a_new_stock_class_should_be_None.setup = setup_test |
24 |
| -test_price_of_a_new_stock_class_should_be_None.teardown = teardown_test |
25 |
| - |
26 |
| - |
27 |
| -def given_a_series_of_prices(stock, prices): |
28 |
| - timestamps = [datetime(2014, 2, 10), datetime(2014, 2, 11), |
29 |
| - datetime(2014, 2, 12), datetime(2014, 2, 13)] |
30 |
| - for timestamp, price in zip(timestamps, prices): |
31 |
| - stock.update(timestamp, price) |
32 |
| - |
33 |
| - |
34 |
| -@params( |
35 |
| - ([8, 10, 12], True), |
36 |
| - ([8, 12, 10], False), |
37 |
| - ([8, 10, 10], False) |
38 |
| -) |
39 |
| -def test_stock_trends(prices, expected_output): |
40 |
| - goog = Stock("GOOG") |
41 |
| - given_a_series_of_prices(goog, prices) |
42 |
| - assert goog.is_increasing_trend() == expected_output |
43 |
| - |
44 |
| - |
45 |
| -def test_trend_with_all_consecutive_values_upto_100(): |
46 |
| - for i in range(100): |
47 |
| - yield stock_trends_with_consecutive_prices, [i, i+1, i+2] |
48 |
| - |
49 |
| - |
50 |
| -def stock_trends_with_consecutive_prices(prices): |
51 |
| - goog = Stock("GOOG") |
52 |
| - given_a_series_of_prices(goog, prices) |
53 |
| - assert goog.is_increasing_trend() |
54 |
| - |
55 |
| - |
56 |
| -with such.A("Stock class") as it: |
57 |
| - |
58 |
| - @it.has_setup |
59 |
| - def setup(): |
60 |
| - it.goog = Stock("GOOG") |
61 |
| - |
62 |
| - with it.having("a price method"): |
63 |
| - @it.has_setup |
64 |
| - def setup(): |
65 |
| - it.goog.update(datetime(2014, 2, 12), price=10) |
66 |
| - |
67 |
| - @it.should("return the price") |
68 |
| - def test(case): |
69 |
| - assert it.goog.price == 10 |
70 |
| - |
71 |
| - @it.should("return the latest price") |
72 |
| - def test(case): |
73 |
| - it.goog.update(datetime(2014, 2, 11), price=15) |
74 |
| - assert it.goog.price == 10 |
75 |
| - |
76 |
| - with it.having("a trend method"): |
77 |
| - @it.should("return True if the last three updates were increasing") |
78 |
| - def test(case): |
79 |
| - it.goog.update(datetime(2014, 2, 11), price=12) |
80 |
| - it.goog.update(datetime(2014, 2, 12), price=13) |
81 |
| - it.goog.update(datetime(2014, 2, 13), price=14) |
82 |
| - assert it.goog.is_increasing_trend() |
83 |
| - |
84 |
| - it.createTests(globals()) |
85 |
| - |
86 |
| - |
87 | 8 | class StockTest(unittest.TestCase):
|
88 | 9 | def setUp(self):
|
89 | 10 | self.goog = Stock("GOOG")
|
|
0 commit comments