nas-yahoo_stock 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,317 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe YahooStock::Interface::History do
4
+
5
+ describe ".new" do
6
+
7
+ it "should raise error when no parameter is passed during object initialization" do
8
+ lambda { YahooStock::Interface::History.new
9
+ }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ it "should raise an error if stock symbol key is not present in parameter hash" do
13
+ lambda { YahooStock::Interface::History.new(:start_date => Date.today-7)
14
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol key is not present in the parameter hash')
15
+ end
16
+
17
+ it "should raise error when stock_symbol value in hash is nil" do
18
+ lambda { YahooStock::Interface::History.new(:stock_symbol => nil)
19
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
20
+ end
21
+
22
+ it "should raise error when stock_symbol value in hash is blank" do
23
+ lambda { YahooStock::Interface::History.new(:stock_symbol => '')
24
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
25
+ end
26
+
27
+ it "should raise error when start date key is not present in parameter hash" do
28
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd') }.should raise_error(YahooStock::Interface::History::HistoryError, ':start_date key is not present in the parameter hash')
29
+ end
30
+
31
+ it "should raise error when start_date value is nil" do
32
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => nil) }.should raise_error(YahooStock::Interface::History::HistoryError, ':start_date value cannot be blank')
33
+ end
34
+
35
+ it "should raise error when start_date value is blank" do
36
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => '') }.should raise_error(YahooStock::Interface::History::HistoryError, ':start_date value cannot be blank')
37
+ end
38
+
39
+ it "should raise error when start date value is not of type date" do
40
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => '23/3/2009') }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be of type Date')
41
+ end
42
+
43
+ it "should raise error when end date key is not present in parameter hash" do
44
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => Date.today-1) }.should raise_error(YahooStock::Interface::History::HistoryError, ':end_date key is not present in the parameter hash')
45
+ end
46
+
47
+ it "should raise error when end_date value is nil" do
48
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
49
+ :start_date => Date.today-1,
50
+ :end_date => nil) }.should raise_error(YahooStock::Interface::History::HistoryError, ':end_date value cannot be blank')
51
+ end
52
+
53
+ it "should raise error when end_date value is blank" do
54
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
55
+ :start_date => Date.today-1,
56
+ :end_date => '') }.should raise_error(YahooStock::Interface::History::HistoryError, ':end_date value cannot be blank')
57
+ end
58
+
59
+ it "should raise error when end date value is not of type date" do
60
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
61
+ :start_date => Date.today-1,
62
+ :end_date => 'sds') }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be of type Date')
63
+ end
64
+
65
+ it "should raise error when start date is not less than today" do
66
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
67
+ :start_date => Date.today,
68
+ :end_date => Date.today)
69
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be in the past')
70
+ end
71
+
72
+ it "should raise error when start date is greater than end date" do
73
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
74
+ :start_date => Date.today-7,
75
+ :end_date => Date.today-8)
76
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be greater than the start date')
77
+ end
78
+
79
+ it "should not raise error when start date is in the past, end date is greater than start date and all relevant keys are present with right type" do
80
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
81
+ :start_date => Date.today-7,
82
+ :end_date => Date.today-1)
83
+ }.should_not raise_error
84
+
85
+ end
86
+
87
+ it "should by default set interval to daily if no value provided for it" do
88
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'd',
89
+ :start_date => Date.today-7,
90
+ :end_date => Date.today-1)
91
+ @history.interval.should eql(:daily)
92
+ end
93
+
94
+ it "should raise invalid keys error when an invalid key is passed in the parameter" do
95
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
96
+ :start_date => Date.today-7,
97
+ :end_date => Date.today-1, :boom => 1) }.should raise_error(YahooStock::Interface::History::HistoryError, "An invalid key 'boom' is passed in the parameters. Allowed keys are stock_symbol, start_date, end_date, interval")
98
+ end
99
+
100
+ it "should raise error when interval is neither :daily, :weekly or :monthly" do
101
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
102
+ :start_date => Date.today-7,
103
+ :end_date => Date.today-1,
104
+ :interval => :yearly)
105
+ }.should raise_error(YahooStock::Interface::History::HistoryError, "Allowed values for interval are daily, weekly, monthly")
106
+
107
+ end
108
+
109
+ it "should not raise error when interval is :daily" do
110
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
111
+ :start_date => Date.today-7,
112
+ :end_date => Date.today-1,
113
+ :interval => :daily)
114
+ }.should_not raise_error
115
+
116
+ end
117
+ end
118
+
119
+ describe "setters" do
120
+ before(:each) do
121
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'd',
122
+ :start_date => Date.today-7,
123
+ :end_date => Date.today-1)
124
+ end
125
+
126
+ it "should raise error when stock symbol is being set to nil" do
127
+ lambda { @history.stock_symbol = nil
128
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
129
+ end
130
+
131
+ it "should raise error when stock symbol is being set to an empty string" do
132
+ lambda { @history.stock_symbol = ''
133
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
134
+ end
135
+
136
+ it "should set the new stock symbol value when it is valid" do
137
+ @history.stock_symbol = 'new_val'
138
+ @history.stock_symbol.should eql('new_val')
139
+ end
140
+
141
+ it "should raise error when start date type is not a date" do
142
+ lambda { @history.start_date = '21/3/2009'
143
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be of type Date')
144
+ end
145
+
146
+ it "should raise error when start date is not in the past" do
147
+ lambda { @history.start_date = Date.today
148
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be in the past')
149
+ end
150
+
151
+ it "should set the start date when start date is valid" do
152
+ s_date = Date.today-1
153
+ @history.start_date = s_date
154
+ @history.start_date.should eql(s_date)
155
+ end
156
+
157
+ it "should raise error when end date type is not a date" do
158
+ lambda { @history.end_date = '21/3/2009'
159
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be of type Date')
160
+ end
161
+
162
+ it "should raise error when end date type is not in the past" do
163
+ lambda { @history.end_date = Date.today
164
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be in the past')
165
+ end
166
+
167
+ it "should raise error when end date type is not in the past" do
168
+ e_date = Date.today-1
169
+ @history.end_date = e_date
170
+ @history.end_date.should eql(e_date)
171
+ end
172
+
173
+ it "should raise error when settin interval other than :daily, :monthly or :weekly" do
174
+ lambda { @history.interval = :yearly }.should raise_error(YahooStock::Interface::History::HistoryError)
175
+ end
176
+
177
+ it "should set the interval" do
178
+ @history.interval = :daily
179
+ @history.interval.should eql(:daily)
180
+ end
181
+
182
+ it "should set the interval default to daily if not set" do
183
+ @history.interval.should eql(:daily)
184
+ end
185
+ end
186
+
187
+ describe "uri" do
188
+ before(:each) do
189
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'symbol',
190
+ :start_date => Date.today-7,
191
+ :end_date => Date.today-1)
192
+ end
193
+
194
+ it "should get the interval to generate url" do
195
+ @history.should_receive(:interval)
196
+ @history.uri
197
+ end
198
+
199
+ it "should get the day of the start date" do
200
+ @history.start_date.should_receive(:day)
201
+ @history.uri
202
+ end
203
+
204
+ it "should get the day of the end date" do
205
+ @history.end_date.should_receive(:day)
206
+ @history.uri
207
+ end
208
+
209
+ it "should get the year of the start date" do
210
+ @history.start_date.should_receive(:year)
211
+ @history.uri
212
+ end
213
+
214
+ it "should get the day of the end date" do
215
+ @history.start_date.should_receive(:year)
216
+ @history.uri
217
+ end
218
+
219
+ it "should get the month of the start and end date" do
220
+ @history.should_receive(:sprintf).exactly(2)
221
+ @history.uri
222
+ end
223
+
224
+ it "should have the base url" do
225
+ @history.uri.should =~ /http:\/\/ichart.finance.yahoo.com\/table.csv?/
226
+ end
227
+
228
+ it "should have parameter 'a' with month -1 " do
229
+ month = sprintf("%02d", @history.start_date.month-1)
230
+ @history.uri.should =~ /a=#{month}/
231
+ end
232
+
233
+ it "should have parameter 'b' with start date day " do
234
+ @history.uri.should =~ /b=#{@history.start_date.day}/
235
+ end
236
+
237
+ it "should have parameter 'c' with start date year " do
238
+ @history.uri.should =~ /c=#{@history.start_date.year}/
239
+ end
240
+
241
+ it "should have parameter 'd' with end date month -1 " do
242
+ month = sprintf("%02d", @history.end_date.month-1)
243
+ @history.uri.should =~ /d=#{month}/
244
+ end
245
+
246
+ it "should have parameter 'e' with end date day " do
247
+ @history.uri.should =~ /e=#{@history.end_date.day}/
248
+ end
249
+
250
+ it "should have parameter 'f' with end date year " do
251
+ @history.uri.should =~ /c=#{@history.end_date.year}/
252
+ end
253
+
254
+ it "should have parameter 'g' with interval" do
255
+ @history.uri.should =~ /g=d/
256
+ end
257
+
258
+ it "should have the parameter 's' with stock symbol" do
259
+ @history.uri.should =~ /s=symbol/
260
+ end
261
+
262
+ end
263
+
264
+ describe "get" do
265
+ before(:each) do
266
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'd',
267
+ :start_date => Date.today-7,
268
+ :end_date => Date.today-1)
269
+ response = stub('HTTP')
270
+ Net::HTTP.stub!(:get_response).and_return(response)
271
+ response.stub!(:code).and_return('200')
272
+ response.stub!(:body)
273
+ end
274
+
275
+ it "should check the stock symbol" do
276
+ @history.should_receive(:stock_symbol).times.at_least(2).and_return('symbol')
277
+ @history.get
278
+ end
279
+
280
+ it "should check the end date" do
281
+ @history.should_receive(:end_date).times.at_least(1).and_return(Date.today-1)
282
+ @history.get
283
+ end
284
+
285
+ it "should check the start date" do
286
+ @history.should_receive(:start_date).times.at_least(1).and_return(Date.today-7)
287
+ @history.get
288
+ end
289
+
290
+ it "should raise error if start date is not present" do
291
+ @history.stub!(:start_date)
292
+ lambda { @history.get }.should raise_error('Cannot send request unless all parameters, ie stock_symbol, start and end date are present')
293
+ end
294
+
295
+ it "should raise error if end date is not present" do
296
+ @history.stub!(:end_date)
297
+ lambda { @history.get }.should raise_error('Cannot send request unless all parameters, ie stock_symbol, start and end date are present')
298
+ end
299
+
300
+ it "should raise error if stock symbol is not present" do
301
+ @history.stub!(:stock_symbol)
302
+ lambda { @history.get }.should raise_error('Cannot send request unless all parameters, ie stock_symbol, start and end date are present')
303
+ end
304
+
305
+ it "should send the http request to yahoo" do
306
+ Net::HTTP.should_receive(:get_response)
307
+ @history.get
308
+ end
309
+
310
+ it "should parse the url" do
311
+ URI.should_receive(:parse)
312
+ @history.get
313
+ end
314
+
315
+ end
316
+
317
+ end