spec/ib-ruby/models/order_spec.rb in ib-ruby-0.7.4 vs spec/ib-ruby/models/order_spec.rb in ib-ruby-0.7.6

- old
+ new

@@ -1,117 +1,138 @@ -require 'spec_helper' +require 'model_helper' describe IB::Models::Order do - let(:properties) do - {:outside_rth => true, - :open_close => 'C', - :origin => IB::Order::Origin_Firm, - :transmit => false, + let(:props) do + {:order_id => 23, + :order_ref => 'Test', + :client_id => 1111, + :perm_id => 173276893, + :parent_id => 0, + :side => :buy, + :order_type => :market_if_touched, + :limit_price => 0.01, + :total_quantity => 100, + :tif => :good_till_cancelled, + :open_close => :close, + :oca_group => '', + :oca_type => :reduce_no_block, + :origin => :firm, :designated_location => "WHATEVER", :exempt_code => 123, - :delta_neutral_order_type => "HACK", + :delta_neutral_order_type => :market, + #:commission_currency => "USD", + #:status => 'PreSubmitted', + :transmit => false, + :outside_rth => true, :what_if => true, :not_held => true} end - context "instantiation" do - context 'empty without properties' do - subject { IB::Order.new } + # TODO: :presents => { Object => "Formatted"} + let(:human) do + "<Order: Test MIT GTC buy 100 New 0.01 #23/173276893 from 1111>" + end - it { should_not be_nil } - its(:outside_rth) { should == false } - its(:open_close) { should == "O" } - its(:origin) { should == IB::Order::Origin_Customer } - its(:transmit) { should == true } - its(:designated_location) { should == '' } - its(:exempt_code) { should == -1 } - its(:delta_neutral_order_type) { should == '' } - its(:what_if) { should == false } - its(:not_held) { should == false } - its(:created_at) { should be_a Time } - end + let(:defaults) do + {:outside_rth => false, + :open_close => :open, + :short_sale_slot => :default, + :tif => :day, + :order_type => :limit, + :origin => :customer, + :transmit => true, + :designated_location => '', + :exempt_code => -1, + :what_if => false, + :not_held => false, + :status => 'New', + :created_at => Time, + } + end - context 'with properties' do - subject { IB::Order.new properties } + let(:errors) do + {:side =>["should be buy/sell/short"], + :order_id => ["is not a number"], } + end - it 'sets properties right' do - properties.each do |name, value| - subject.send(name).should == value - end - end + let(:assigns) do + {[:order_type, :delta_neutral_order_type] => codes_and_values_for(:order_type), + :open_close => + {['SAME', 'same', 'S', 's', :same, 0, '0'] => :same, + ['OPEN', 'open', 'O', 'o', :open, 1, '1'] => :open, + ['CLOSE', 'close', 'C', 'c', :close, 2, '2'] => :close, + ['UNKNOWN', 'unknown', 'U', 'u', :unknown, 3, '3'] => :unknown, + [42, nil, 'Foo', :bar] => /should be same.open.close.unknown/}, - context 'essential properties are still set, even if not given explicitely' do - its(:created_at) { should be_a Time } - end - end + :side => + {['BOT', 'BUY', 'Buy', 'buy', :BUY, :BOT, :Buy, :buy, 'B', :b] => :buy, + ['SELL', 'SLD', 'Sel', 'sell', :SELL, :SLD, :Sell, :sell, 'S', :S] => :sell, + ['SSHORT', 'Short', 'short', :SHORT, :short, 'T', :T] => :short, + ['SSHORTX', 'Shortextemt', 'shortx', :short_exempt, 'X', :X] => :short_exempt, + [1, nil, 'ASK', :foo] => /should be buy.sell.short/, }, - it 'allows setting attributes' do - x = IB::Order.new - properties.each do |name, value| - subject.send("#{name}=", value) - subject.send(name).should == value - end - end - end #instantiation + [:what_if, :not_held, :outside_rth, :hidden, :transmit, :block_order, :sweep_to_fill, + :override_percentage_constraints, :all_or_none, :etrade_only, :firm_quote_only, + :opt_out_smart_routing, :scale_auto_reset, :scale_random_percent + ] => {[1, true] => true, [0, false] => false}, + } + end + it_behaves_like 'Model' + it_behaves_like 'Self-equal Model' + context 'equality' do - subject { IB::Order.new properties } + subject { IB::Order.new props } - it 'is self-equal ' do - should == subject - end + it_behaves_like 'Self-equal Model' - it 'is equal to Order with the same properties' do - should == IB::Order.new(properties) - end - it 'is not equal for Orders with different limit price' do order1 = IB::Order.new :total_quantity => 100, - :limit_price => 1, - :action => 'BUY' + :limit_price => 1, + :action => 'BUY' order2 = IB::Order.new :total_quantity => 100, - :limit_price => 2, - :action => 'BUY' + :limit_price => 2, + :action => 'BUY' order1.should_not == order2 order2.should_not == order1 end it 'is not equal for Orders with different total_quantity' do order1 = IB::Order.new :total_quantity => 20000, - :limit_price => 1, - :action => 'BUY' + :limit_price => 1, + :action => 'BUY' order2 = IB::Order.new :total_quantity => 100, - :action => 'BUY', - :limit_price => 1 + :action => 'BUY', + :limit_price => 1 order1.should_not == order2 order2.should_not == order1 end it 'is not equal for Orders with different action/side' do order1 = IB::Order.new :total_quantity => 100, - :limit_price => 1, - :action => 'SELL' + :limit_price => 1, + :action => 'SELL' order2 = IB::Order.new :total_quantity => 100, - :action => 'BUY', - :limit_price => 1 + :action => 'BUY', + :limit_price => 1 order1.should_not == order2 order2.should_not == order1 end it 'is not equal for Orders with different order_type' do order1 = IB::Order.new :total_quantity => 100, - :limit_price => 1, - :action => 'BUY', - :order_type => 'LMT' + :limit_price => 1, + :action => 'BUY', + :order_type => 'LMT' order2 = IB::Order.new :total_quantity => 100, - :action => 'BUY', - :limit_price => 1, - :order_type => 'MKT' + :action => 'BUY', + :limit_price => 1, + :order_type => 'MKT' order1.should_not == order2 order2.should_not == order1 end end