/
trade_spec.rb
160 lines (143 loc) · 4.36 KB
/
trade_spec.rb
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'spec_helper'
describe Trade do
before(:each) do
@portfolio = FactoryGirl.create(:portfolio)
@stock = FactoryGirl.create(:stock)
@attr = { :state => 1,
:buy => true,
:trade_date => DateTime.parse("2011-07-25"),
:clear_date => DateTime.parse("2011-07-25"),
:vol => 100.00,
:price => 20.00,
#:amount => 100*20,
:fee => 1.5,
:security_id => @stock.id
}
end
it "should create a new instance given valid attributes" do
@portfolio.trades.create!(@attr)
end
it "should allow Trade.new" do
Trade.new
end
describe "sell_type" do
it "should allow 'AVG' sell_type" do
@portfolio.trades.create!(@attr.merge(:sell_type => "AVG"))
end
it "should allow 'FIFO' sell_type" do
@portfolio.trades.create!(@attr.merge(:sell_type => "FIFO"))
end
it "should allow 'LIFO' sell_type" do
@portfolio.trades.create!(@attr.merge(:sell_type => "LIFO"))
end
it "should allow 'SPECIFIC' sell_type" do
@portfolio.trades.create!(@attr.merge(:sell_type => "SPECIFIC"))
end
it "should allow nil sell_type" do
@portfolio.trades.create!(@attr.merge(:sell_type => nil))
end
it "should NOT allow sell_types other than 'AVG' 'FIFO' 'LIFO' 'SPECIFIC' or nil " do
@portfolio.trades.build(@attr.merge(:sell_type => "aaa")).should_not be_valid
end
end
describe "portfolio associations" do
before(:each) do
@trade = @portfolio.trades.create(@attr)
end
it "should have a portfolio attribute" do
@trade.should respond_to(:portfolio)
end
it "should have the right associated portfolio" do
@trade.portfolio_id.should == @portfolio.id
@trade.portfolio.should == @portfolio
end
end
describe "security association" do
before(:each) do
@trade = @portfolio.trades.create(@attr)
end
it "should have a security attribute" do
@trade.should respond_to(:security)
end
it "should have the right associated security" do
@trade.security_id.should == @stock.id
@trade.security.should == @stock
end
end
describe "validations" do
before(:each) do
@trade = @portfolio.trades.create(@attr)
end
it "is valid with valid attributes" do
@trade.should be_valid
end
it "should require a portfolio id" do
@trade.portfolio_id = nil
@trade.should_not be_valid
end
it "should require a security id" do
@trade.security_id = nil
@trade.should_not be_valid
end
it "should belong to a portfolio that exists" do
@trade.portfolio_id = 999999999999
@trade.should_not be_valid
end
it "should belong to a security that exists" do
@trade.security_id = 999999999999
@trade.should_not be_valid
end
it "should require a trade_date" do
@trade.trade_date = nil
@trade.should_not be_valid
end
it "should have a numerical amount, which equals to vol * price" do
@trade.amount.should == @trade.vol * @trade.price
@trade.amount = "nill"
@trade.should_not be_valid
end
it "should set correct amount value even if amount is nil" do
@trade.amount = nil
@trade.should be_valid
@trade.amount.should == @trade.vol * @trade.price
end
it "should have a numercial price" do
@trade.price = "nill"
@trade.should_not be_valid
@trade.price = nil
@trade.should_not be_valid
end
it "should have a trade_date no later than clear_date" do
early_date = DateTime.parse("2011-7-29")
late_date = DateTime.parse("2011-7-30")
@trade.trade_date = late_date
@trade.clear_date = early_date
@trade.should_not be_valid
end
it "is not valid if VOL < 0" do
@trade.vol = -1
@trade.should_not be_valid
end
it "is not valid if price < 0 " do
@trade.price = -1
@trade.should_not be_valid
end
end
end
# == Schema Information
#
# Table name: trades
#
# id :integer(4) not null, primary key
# portfolio_id :integer(4)
# state :integer(4)
# buy :boolean(1)
# sell_type :string(255)
# trade_date :datetime
# clear_date :datetime
# vol :decimal(10, 3)
# price :decimal(8, 3)
# amount :decimal(12, 3)
# fee :decimal(8, 3)
# security_id :integer(4)
#