require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe TripLeg do describe "leg_status" do it "shows maint status" do trip_leg = TripLeg.new(:cancel_code=>0, :status=>21, :flight_time_actual=>0, :verify_date=>0) trip_leg.status= 21 trip_leg.leg_status.should == "MAINT" end end describe "arrival_airport assoc works" do before :all do set_fos_db([Airport]) end it "when there is an arrival_icao" do trip_leg = TripLeg.new(:arrival_icao=>'KSFO') trip_leg.arrival_airport.should_not == nil end it "when there is no arrival_icao but there is arrival_ap_prefix and arrival_airport_id" do trip_leg = TripLeg.new(:arrival_icao=>'', :arrival_ap_prefix=>'K', :arrival_airport_id=>'SFO') trip_leg.arrival_airport.should_not == nil end it "when there is no arrival_ap_prefix or arrival_airport_id" do trip_leg = TripLeg.new(:arrival_icao=>'', :arrival_ap_prefix=>'', :arrival_airport_id=>'') trip_leg.arrival_airport.should == nil end end describe "departure_airport assoc works" do before :all do set_fos_db([Airport]) end it "when there is an departure_icao" do trip_leg = TripLeg.new(:departure_icao=>'KSFO') trip_leg.departure_airport.should_not == nil end it "when there is no departure_icao but there is depart_ap_prefix and depart_airport_id" do trip_leg = TripLeg.new(:departure_icao=>'', :depart_ap_prefix=>'K', :depart_airport_id=>'SFO') trip_leg.departure_airport.should_not == nil end it "when there is no departure_icao or arrival_ap_prefix or arrival_airport_id" do trip_leg = TripLeg.new(:departure_icao=>'', :depart_ap_prefix=>'', :depart_airport_id=>'') trip_leg.departure_airport.should == nil end end it "ebt_time method produces correct Time with minutes" do dept_date_act_gmt = Date.new(2000, 1, 1) tl = TripLeg.new(:arrival_date_gmt=>dept_date_act_gmt.to_fos_days, :eta_gmt=>40, :dept_date_gmt=>dept_date_act_gmt.to_fos_days, :etd_gmt=>20) tl.ebt_time.should == Time.from_minutes(20) end it "ebt_time method produces correct Time with minutes" do dept_date_act_gmt = Date.new(2000, 1, 1) tl = TripLeg.new(:arrival_date_gmt=>dept_date_act_gmt.to_fos_days, :eta_gmt=>40, :dept_date_gmt=>dept_date_act_gmt.to_fos_days, :etd_gmt=>20) tl.ebt_time.should == Time.from_minutes(20) end it "actual take off date_time dates are after dept_date_act_gmt if take off time is less than depart time" do dept_date_act_gmt = Date.new(1900, 1, 1) tl = TripLeg.new(:dept_date_act_gmt=>dept_date_act_gmt.to_fos_days, :t_o_time_act_gmt=>20, :dept_time_act_gmt=>1420) tl.actual_takeoff_date_time_gmt.should == DateTime.new(1900, 1, 2, 0, 20, 0) end it "actual take off date_time dates are the same as dept_date_act_gmt if take off time is not less than depart time" do dept_date_act_gmt = Date.new(1900, 1, 1) tl = TripLeg.new(:dept_date_act_gmt=>dept_date_act_gmt.to_fos_days, :t_o_time_act_gmt=>40, :dept_time_act_gmt=>20) tl.actual_takeoff_date_time_gmt.should == DateTime.new(1900, 1, 1, 0, 40, 0) end describe 'actual_land_date_time_gmt' do it "actual_landing_date_time dates are before arr_date_act_gmt if on time time is greater than landing time" do tl = TripLeg.new(:arr_date_act_gmt=> Date.new(1900, 1, 2).to_fos_days, :land_time_act_gmt=> 1420, :arriv_time_act_gmt=> 20) tl.actual_land_date_time_gmt.should == DateTime.new(1900, 1, 1, 23, 40, 0) end it "actual landing date_time dates are the same as arr_date_act_gmt if on time time is NOT greater than landing time" do tl = TripLeg.new(:arr_date_act_gmt=> Date.new(1900, 1, 2).to_fos_days, :land_time_act_gmt=> 20, :arriv_time_act_gmt=> 40) tl.actual_land_date_time_gmt.should == DateTime.new(1900, 1, 2, 0, 20, 0) end end describe 'actual_arrival_date_time_gmt' do it "doesn't have the new 'arr date act gmt' field set" do tl = TripLeg.new(:arr_date_act_gmt=> 0, # new field not set :dept_date_act_gmt => 1, # should calculate arrival date based on departure datetime and block time :dept_time_act_gmt => 1340, :block_time_actual => 130, :arrival_date_gmt=> 2, :arriv_time_act_gmt=> 0) tl.actual_arrival_date_time_gmt.should == DateTime.new(1900, 1, 2, 0, 30, 0) end it "has the new 'arr date act gmt' field set" do tl = TripLeg.new(:arr_date_act_gmt=> 2, # new field is set, so use this one :arriv_time_act_gmt=> 10, :arrival_date_gmt=> 0) tl.actual_arrival_date_time_gmt.should == DateTime.new(1900, 1, 2, 0, 10, 0) end it "doesnt have any actual fields set" do # occurs when trip leg has not been flown tl = TripLeg.new(:arr_date_act_gmt => 0, :arriv_time_act_gmt => 0, :arrival_date_gmt => 2, :block_time_actual => 0, :dept_date_act_gmt => 0, :dept_time_act_gmt => 1440) tl.actual_arrival_date_time_gmt.should == DateTime.parse("1900-01-01T00:00:00+00:00") # we expect an invalid date here, accurate reflection of the data end end it "actual_arrival_date_time_base are correctly calculating the tz offset" do # this makes an actual_arrival_date_time_gmt date of: 1900-01-02 23:40:00 arr_date_act_gmt, arriv_time_act_gmt = Date.new(1900, 1, 2).to_fos_days, 1420 home_tz_gmt_offset = -70 # home_tz_gmt_offset should make actual_arrival_date_time_base of: 1900-01-02 16:40:00 tl = TripLeg.new(:arr_date_act_gmt => arr_date_act_gmt, :arriv_time_act_gmt => arriv_time_act_gmt, :home_tz_gmt_offset => home_tz_gmt_offset) tl.actual_arrival_date_time_base.should == DateTime.new(1900, 1, 2, 16, 40, 0) end it "actual_departure_date_time_base are correctly calculating the tz offset" do # this makes an actual_arrival_date_time_gmt date of: 1900-01-02 23:40:00 dept_date_act_gmt, dept_time_act_gmt = Date.new(1900, 1, 2).to_fos_days, 1420 home_tz_gmt_offset = -70 # home_tz_gmt_offset should make actual_arrival_date_time_base of: 1900-01-02 16:40:00 tl = TripLeg.new(:dept_date_act_gmt => dept_date_act_gmt, :dept_time_act_gmt => dept_time_act_gmt, :home_tz_gmt_offset=>home_tz_gmt_offset) tl.actual_departure_date_time_base.should == DateTime.new(1900, 1, 2, 16, 40, 0) end it "should properly convert nautical miles to statute miles with 1 significant digit" do tl = TripLeg.new(:nautical_miles => 285) tl.custom_statute_miles.should == 327.9 end it "should properly handle nautical miles to statute miles when nautical_miles is nil" do tl = TripLeg.new(:nautical_miles => nil) tl.custom_statute_miles.should == 0 end describe "should find correct passenger list" do before :each do @p1 = TripPassenger.new(:name=>"dan", :departure_leg_number=>0, :arrival_leg_number=>0, :lead_pax=>0) @p2 = TripPassenger.new(:name=>"rob", :departure_leg_number=>1, :arrival_leg_number=>1, :lead_pax=>0) @p3 = TripPassenger.new(:name=>"eric", :departure_leg_number=>1, :arrival_leg_number=>2, :lead_pax=>1) @t = Trip.new(:trip_number=>100) end it "when leg has no passengers" do mock(@t).passengers { [] } tl = TripLeg.new(:trip_number=>100, :leg_number=>1, :trip=>@t) tl.passenger_list.should == '' end it "when leg has some in range ( and put lead pax name first )" do stub(@t).passengers { [@p1, @p2, @p3] } tl = TripLeg.new(:trip_number=>100, :leg_number=>1, :verify_date=>0, :trip=>@t) tl.passenger_list.should == "eric (lead pax) : rob" end it "when leg has 1 in range" do stub(@t).passengers { [@p1, @p2, @p3] } tl = TripLeg.new(:trip_number=>100, :leg_number=>2, :verify_date=>0, :trip=>@t) tl.passenger_list.should == "eric (lead pax)" end it "when leg has none in range" do stub(@t).passengers { [@p1, @p2, @p3] } tl = TripLeg.new(:trip_number=>100, :leg_number=>3, :verify_date=>0, :trip=>@t) tl.passenger_list.should == "" end end it "method total_trip_statute_miles returns a value" do @t = Trip.new(:total_statute_miles=>100) tl = TripLeg.new(:trip_number=>100, :leg_number=>3, :trip=>@t) tl.total_trip_statute_miles.should == 10.0 end describe 'crew brief fields' do before :all do set_fos_db([TripLeg, Comment]) @trip_leg = TripLeg.first(:trip_number=>50885, :leg_number=>1) end it "catering departure value" do @trip_leg.catering_departure.should == 1 end it "departure fbo comment value" do @trip_leg.departure_fbo_comment_value.comment.should == "3028 Peacekeeper Way\r\nSacramento, CA 95652" end it "arrival fbo comment value" do @trip_leg.arrival_fbo_comment_value.comment.should == "Auto-selected FBO not preferred" end it "catering comment value" do @trip_leg.departure_catering_comment__comment.should == "Catering: Standard drinks and snacks for all live legs" end it "limo comment value" do @trip_leg.arrival_transport_comment__comment.should == "Transportation: Passenger arranged for all live legs" end it "crew fbo comment value" do @trip_leg.crew_fbo_comment.should == nil end end describe 'fuel expenses' do before :each do @tl = TripLeg.new end describe 'arrival fuel costs' do it 'should not have a cost if there are no fuel flight log expenses' do @tl.arrival_fuel_cost.should == 0.0 end it 'should have a cost if there is an arrival fuel flight log expense' do fe = FlightLogExpense.new(:type => 1, :arrival_airport => 1) stub(fe).fuel_cost { 300.0 } stub(@tl).fuel_expenses { [fe] } @tl.arrival_fuel_cost.should == 300.0 end it 'should not have a cost if the fuel flight log expense is not arrival' do fe = FlightLogExpense.new(:type => 1, :arrival_airport => 0) stub(fe).fuel_cost { 300.0 } stub(@tl).fuel_expenses { [fe] } @tl.arrival_fuel_cost.should == 0.0 end it 'should total the costs if there are more than one fuel flight log expenses' do fles = [] (1..4).each do |n| fe = FlightLogExpense.new(:type => 1, :arrival_airport => 1) stub(fe).fuel_cost { n * 100 } fles << fe end stub(@tl).fuel_expenses { fles } @tl.arrival_fuel_cost.should == 1000.0 end it 'should total the costs but only for fuel expenses that are arrival, ignoring departures' do fles = [] (1..4).each do |n| arrival = (n > 2 ? 1 : 0) fe = FlightLogExpense.new(:type => 1, :arrival_airport => arrival) stub(fe).fuel_cost { n * 100 } fles << fe end stub(@tl).fuel_expenses { fles } @tl.arrival_fuel_cost.should == 700.0 end end describe 'departure fuel costs' do it 'should not have a cost if there are no fuel flight log expenses' do @tl.departure_fuel_cost.should == 0.0 end it 'should have a cost if there is a departure fuel flight log expense' do fe = FlightLogExpense.new(:type => 1, :arrival_airport => 0) stub(fe).fuel_cost { 300.0 } stub(@tl).fuel_expenses { [fe] } @tl.departure_fuel_cost.should == 300.0 end it 'should not have a cost if the fuel flight log expense is not departure' do fe = FlightLogExpense.new(:type => 1, :arrival_airport => 1) stub(fe).fuel_cost { 300.0 } stub(@tl).fuel_expenses { [fe] } @tl.departure_fuel_cost.should == 0.0 end it 'should total the costs if there are more than one fuel flight log expenses' do fles = [] (1..4).each do |n| fe = FlightLogExpense.new(:type => 1, :arrival_airport => 0) stub(fe).fuel_cost { n * 100 } fles << fe end stub(@tl).fuel_expenses { fles } @tl.departure_fuel_cost.should == 1000.0 end it 'should total the costs but only for fuel expenses that are arrival, ignoring departures' do fles = [] (1..4).each do |n| arrival = (n <= 2 ? 1 : 0) fe = FlightLogExpense.new(:type => 1, :arrival_airport => arrival) stub(fe).fuel_cost { n * 100 } fles << fe end stub(@tl).fuel_expenses { fles } @tl.departure_fuel_cost.should == 700.0 end end describe 'total fuel costs' do it 'should not have a total cost if there are no fuel flight log expenses' do stub(@tl).departure_fuel_cost { 0.0 } stub(@tl).arrival_fuel_cost { 0.0 } @tl.total_fuel_cost.should == 0.0 end it 'should have a total cost if there are departure fuel expense costs' do stub(@tl).departure_fuel_cost { 100.0 } stub(@tl).arrival_fuel_cost { 0.0 } @tl.total_fuel_cost.should == 100.0 end it 'should have a total cost if there are arrival fuel expense costs' do stub(@tl).departure_fuel_cost { 0.0 } stub(@tl).arrival_fuel_cost { 100.0 } @tl.total_fuel_cost.should == 100.0 end it 'should have a total cost if there are both arrival and departure fuel expense costs' do stub(@tl).departure_fuel_cost { 100.0 } stub(@tl).arrival_fuel_cost { 100.0 } @tl.total_fuel_cost.should == 200.0 end end end end