require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/spec_helper_tables' unless metaclass.class_variable_defined? :@@set_up_demo_tables describe "Sequel::Model extentions" do describe "fill_hash method" do it "works with attributes" do Dude.first.should respond_to(:fill_hash) Dude.first.fill_hash([:'kid - date']).should == {:'kid - date'=>1} end it "works with methods" do Dude.column_alias :doo, :'kid - date' Dude.first.fill_hash([:doo]).should == {:doo=>1} end describe "method missing recognizes __ in method name and creates delegators only once" do it "for one level deep" do mock.proxy(Dude).delegate(:name, :to => :my_friend, :prefix => true, :allow_nil => true) Dude.first.fill_hash([:my_friend__name,:my_friend__name]).should == {:my_friend_name=>'dans buddy'} Dude.send(:remove_method, :my_friend_name) end it "for two levels deep" do MyFriend.column_alias :kid_date, :'kid - date' mock.proxy(Horse).delegate(:my_friend, :to => :dude, :prefix => true, :allow_nil => true) mock.proxy(Horse).delegate(:name, :to => :dude_my_friend, :prefix => true, :allow_nil => true) mock.proxy(Horse).delegate(:kid_date, :to => :dude_my_friend, :prefix => true, :allow_nil => true) Horse.first Horse.first.fill_hash([:dude__my_friend__kid_date,:dude__my_friend__name]).should == {:dude_my_friend_name=>'dans buddy',:dude_my_friend_kid_date=>1} Horse.send(:remove_method,:dude_my_friend) Horse.send(:remove_method,:dude_my_friend_name) Horse.send(:remove_method,:dude_my_friend_kid_date) end end end it "can update and save" do add_test_table_data({:date_col => 123, :time_col=>60}) record = TestTable.first record[:'goofy name col'] = 124 record.save_changes TestTable.first[:'goofy name col'].should ==124 end describe "select_fields method" do it "works on model" do dude = Dude.select_fields(:self => ['kid - date']).first dude[:'kid - date'].should == 1 end it "works on dataset" do dude = Dude.filter(:'kid - date'=>1).select_fields(:self => ['kid - time']).first dude[:'kid - time'].should == 1 end it "selects column alias" do Dude.column_alias :kid_date, :'kid - date' Dude.select_fields(:self => [:kid_date]).first.kid_date.should == 1 end it "can select more than one table with columns of the same name" do # dude = Dude.eager_graph(:horses).select(*(Dude.pk_for_select + Horse.pk_for_select(:table=>:horses,:alias=>true) + [:'dudes__name', :'horses__name___horses_name'])).all.first dude = Dude.eager_graph(:horses).select_fields(:self => [:name], :horses => [:name]).all.first dude.name.should == "dano" dude.horses.first.name.should == 'pinto' end it "selects column alias from correct table" do Dude.column_alias :doo, :'kid - date' Horse.column_alias :hoo, :'kid - time' dude = Dude.eager_graph(:horses).select_fields(:self => [:doo], :horses => [:hoo]).all.first dude.doo.should == 1 dude.horses.first.hoo.should == 1 end it "selects particular columns from joined tables" do dude = Dude.eager_graph(:ranch_code, :my_friend, :horses).select_fields(:self => [:name], :ranch_code => [:code], :my_friend => [:name], :horses => [:name]).all.first dude.ranch_code_code.should == "men" dude.name.should == "dano" dude.horses.first.name.should == 'pinto' dude.my_friend.name.should == 'dans buddy' end end describe "column_view" do after :each do TestTable.clear_schema end before do add_test_table_data({:date_col => 123, :time_col=>60, :price=>1215, :paid=>1}) TestTable.column_view :date_col, :date TestTable.column_view :time_col, :time TestTable.column_view :time_col, :time, :my_special_name TestTable.column_view :price, :currency TestTable.column_view :price, :precision TestTable.column_view :paid, :boolean @record = TestTable.first end it "creates method to display value" do @record.should respond_to(:date_col_date) @record.should respond_to(:date_col_date_view) @record.should respond_to(:date_col_date_view_mdy) @record.should respond_to(:date_col_date_view_mdy_ipc) @record.should respond_to(:time_col_time) @record.should respond_to(:time_col_time_view) @record.should respond_to(:price_currency) @record.should respond_to(:price_precision) @record.should respond_to(:paid_boolean) @record.should respond_to(:my_special_name) end it "shows correct date view" do @record.date_col_date_view.should == "1900-05-03" @record.date_col_date_view_mdy.should == "05/03/1900" # ruby 1.9.1 not making date as 5/3/1900, so this just makes it # pass .. but eventually we drop that ipc method anyway, so I dont care. @record.date_col_date_view_mdy_ipc.should =~ /[0]*5\/[0]*3\/1900/ end it "shows correct time view" do @record.time_col_time_view.should == "01:00" @record.my_special_name_view.should == "01:00" end it "shows correct currency view" do @record.price_currency.should == 12.15 @record.price_precision.should == 12.15 end it "shows correct currency view" do @record.paid_boolean.should == "True" end end describe "column_alias" do before :each do add_test_table_data({:id=>1, :'goofy name col' => 123}) end after :each do TestTable.clear_schema end it "makes alias" do TestTable.column_alias :'nice_name', :'goofy name col' TestTable.first.should respond_to(:nice_name) TestTable.first.nice_name.should == 123 end it "alias name also had entry in datatypes" do TestTable.column_alias :'nice_name', :'goofy name col' TestTable.db_schema[:nice_name].should_not be_nil end it "sets column type if param exists" do TestTable.column_alias :'new_date_col', :'goofy name col', :type => :date TestTable.first.new_date_col.should be_a_kind_of(Date) TestTable.first.new_date_col.should == Date.from_fos_days(123) end it "column alias expands to real column name in filtered search" do TestTable.column_alias :'goofy_col', :'goofy name col' TestTable.filter(:goofy_col=>123).first.goofy_col.should == 123 end it "column alias creates setter to real column name" do TestTable.column_alias :'goofy_col', :'goofy name col' testrow = TestTable.first testrow.goofy_col = 124 testrow.goofy_col.should == 124 end # seems like ridiculous case, but it is a case in use it "can makes an alias with same name and then an alias to that still works with existing record" do add_test_table_data({:id=>2, :'date_col' => 10}) TestTable.column_alias :date_col, :'date_col' # our autogenerate script sometimes makes same name alias TestTable.column_alias :save_date_col, :date_col, :type => :integer testrow = TestTable.first testrow.save_date_col = 11 testrow.date_col.should == 11 end it "can makes an alias with same name and then an alias to that still works with new record" do TestTable.column_alias :date_col, :'date_col' # our autogenerate script sometimes makes same name alias TestTable.column_alias :save_date_col, :date_col, :type => :integer testrow = TestTable.new({:save_date_col=>10}) testrow.save_date_col = 10 testrow.date_col.should == 10 end end describe "column_type" do before :each do add_test_table_data({:date_col => 123, :time_col=>60}) end after :each do TestTable.clear_schema end it "sets a date type" do TestTable.column_type :date_col, :date TestTable.first[:date_col].should be_a_kind_of(Date) TestTable.first[:date_col].should == Date.from_fos_days(123) end it "sets a time type" do TestTable.column_type :time_col, :time TestTable.first[:time_col].should be_a_kind_of(DateTime) TestTable.first[:time_col].should == Date.from_fos_days(0).to_time.utc + 60.minutes end it "should find rows" do TestTable.filter('date_col < ?', Date.today).all.size.should == 1 end end describe "column_def" do before do TestTable.column_def_datetime :datetime_col, :date_col, :time_col end it "datetime makes method to represent datetime" do add_test_table_data({:date_col => 39992, :time_col => 60}) TestTable.first.should respond_to(:datetime_col) end it "datetime can parse valid dates" do add_test_table_data({:date_col => 39992, :time_col => 60}) TestTable.first.datetime_col.should == DateTime.new(2009,6,29,1,0,0) end it "datetime can handle nil date or time" do add_test_table_data({:date_col => nil, :time_col => nil}) TestTable.first.datetime_col.should == DateTime.new(1899,12,31,0,0,0) end end describe "association methods" do describe "code association" do before(:each) do set_demo_db([Code,Dude]) end it "finds the associated code" do Dude.first.ranch_code.description.should == 'manly ranch' end it "finds two associated codes" do Dude.first.ranch_code.description.should == 'manly ranch' Dude.first.pool_type.description.should == 'big round pool' end it "creates delegators for code and description" do Dude.first.ranch_code_code.should == "men" Dude.first.ranch_code_description.should == "manly ranch" end it "should create a class method to get all codes for that type" do Dude.should respond_to(:get_all_ranch_codes) Dude.get_all_ranch_codes.should have(1).things Dude.get_all_ranch_codes.first.should == 'manly ranch' end end describe "o_to_n" do it "retrieves the association" do Dude.first.horses.first.name.should == 'pinto' end it "retrieves association eagerly with eager graph" do ds = Dude.eager_graph(:horses) mock.proxy(ds).fetch_rows(is_a(String)).times(1) ds.all.first.horses.first end end describe "n_to_o" do it "retrieves the association" do Horse.first.dude.name.should == 'dano' end it "retrieves association eagerly" do ds = Horse.eager_graph(:dude).select() mock.proxy(ds).fetch_rows(is_a(String)).times(1) ds.all.first.dude end end describe "work with real fos data" do before :all do set_fos_db([TripLeg, Trip, TripPassenger, CrewLeg, Aircraft, Code]) @trip_leg = TripLeg.first(:trip_number=>67506, :leg_number=>4) end it "many_to_one aircraft" do @trip_leg.aircraft__tail_number.should == "N779XJ" end it "o_to_n crew_legs association works" do [@trip_leg.pic,@trip_leg.sic].should == ['FORS','ALEB'] end it "n_to_o trip association works" do @trip_leg.trip.departure_date_gmt.should == 39873 end it "trip to passenger_list association works" do @trip_leg.passenger_list.should == "Mendelson, Victor (lead pax) : Mendelson, Laurans : Irwin, Thomas" end end end end