Skip to content

Commit

Permalink
Created model layer - added specs for FlightSelector and ReservationS…
Browse files Browse the repository at this point in the history
…ystem
  • Loading branch information
arunsark committed Jul 1, 2012
1 parent d33565a commit 1a7e804
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 49 deletions.
12 changes: 12 additions & 0 deletions app/models/flight.rb
@@ -0,0 +1,12 @@
class Flight
attr_accessor :flight_number, :itineraries

def initialize(flight_number)
@flight_number = flight_number
@itineraries = []
end

def add_itinerary(itinerary)
@itineraries << itinerary
end
end
11 changes: 11 additions & 0 deletions app/models/flight_selector.rb
@@ -0,0 +1,11 @@
class FlightSelector
attr_accessor :flight_list

def initialize(flight_list)
@flight_list = flight_list
end

def search(itinerary)
@flight_list.select { |flight| flight.itineraries.include?(itinerary) }
end
end
12 changes: 12 additions & 0 deletions app/models/itinerary.rb
@@ -0,0 +1,12 @@
class Itinerary
attr_accessor :from, :to, :date
def initialize(from, to, date)
@from = from
@to = to
@date = date
end

def ==(another_object)
self.from == another_object.from && self.to == another_object.to && self.date == another_object.date
end
end
9 changes: 9 additions & 0 deletions app/models/reservation_system.rb
@@ -0,0 +1,9 @@
class ReservationSystem
def self.book_ticket(flight, itinerary, passenger)
OpenStruct.new(flight_number: flight.flight_number,
from: itinerary.from,
to: itinerary.to,
date: itinerary.date,
passenger_name: passenger.name)
end
end
2 changes: 1 addition & 1 deletion features/booking_flight_tickets.feature
Expand Up @@ -8,5 +8,5 @@ Scenario: Book a ticket
Given there is a flight "6E-154" between "Bangalore" and "Hyderabad" on "01/12/2012" Given there is a flight "6E-154" between "Bangalore" and "Hyderabad" on "01/12/2012"
And I search for flights between "Bangalore" and "Hyderabad" on "01/12/2012" And I search for flights between "Bangalore" and "Hyderabad" on "01/12/2012"
And I select the flight "6E-154" from the search result And I select the flight "6E-154" from the search result
When I book the tickets When I select the seat "3A" and book the tickets
Then I should have my tickets booked Then I should have my tickets booked
50 changes: 3 additions & 47 deletions features/step_definitions/booking_flights_steps.rb
@@ -1,47 +1,3 @@
class Flight
attr_accessor :flight_number

def initialize(flight_number)
@flight_number = flight_number
@itineraries = []
end

def add_itinerary(itinerary)
@itineraries << itinerary
end
end

class Itinerary
attr_accessor :from, :to, :date
def initialize(from, to, date)
@from = from
@to = to
@date = date
end
end

class FlightSelector
attr_accessor :flight_list

def initialize(flight_list)
@flight_list = flight_list
end

def search(itinerary)
@flight_list
end
end

class ReservationSystem
def self.book_tickets(flight, itinerary, passenger)
OpenStruct.new(flight_number: flight.flight_number,
from: itinerary.from,
to: itinerary.to,
date: itinerary.date,
passenger_name: passenger.name)
end
end

Given /^there is a flight "(.*?)" between "(.*?)" and "(.*?)" on "(.*?)"$/ do |flight_number, from, to, on_date| Given /^there is a flight "(.*?)" between "(.*?)" and "(.*?)" on "(.*?)"$/ do |flight_number, from, to, on_date|
flight = Flight.new(flight_number) flight = Flight.new(flight_number)
flight.add_itinerary(Itinerary.new(from, to, on_date)) flight.add_itinerary(Itinerary.new(from, to, on_date))
Expand All @@ -57,9 +13,9 @@ def self.book_tickets(flight, itinerary, passenger)
choose_flight(flight_number) choose_flight(flight_number)
end end


When /^I book the tickets$/ do When /^I select the seat "(.*?)" and book the tickets$/ do |seat_number|
passenger = OpenStruct.new(name: "Arun") passenger_details = OpenStruct.new(name: "Arun", seat: seat_number)
book_tickets flight: selected_flight, itinerary: my_itinerary, passenger: passenger book_tickets flight: selected_flight, itinerary: my_itinerary, passenger_details: passenger_details
end end


Then /^I should have my tickets booked$/ do Then /^I should have my tickets booked$/ do
Expand Down
2 changes: 1 addition & 1 deletion features/support/booking_support.rb
Expand Up @@ -25,7 +25,7 @@ def selected_flight
end end


def book_tickets(params) def book_tickets(params)
@my_ticket = ReservationSystem.book_tickets(params[:flight], params[:itinerary], params[:passenger]) @my_ticket = ReservationSystem.book_ticket(params[:flight], params[:itinerary], params[:passenger_details])
end end


def my_ticket def my_ticket
Expand Down
4 changes: 4 additions & 0 deletions features/support/env.rb
@@ -0,0 +1,4 @@
# $LOAD_PATH << File.expand_path('../../../app/models', __FILE__)
Dir[File.dirname(__FILE__) + "/../../app/models/*.rb"].each do |file|
require file
end
16 changes: 16 additions & 0 deletions spec/models/flight_selector_spec.rb
@@ -0,0 +1,16 @@
require_relative '../../app/models/flight_selector'

describe FlightSelector do
let (:itinerary1) { stub(:itinerary1) }
let (:itinerary2) { stub(:itinerary2) }
let (:flight1) { stub(:flight1, itineraries: [itinerary1]) }
let (:flight2) { stub(:flight2, itineraries: [itinerary2]) }

before do
@it = FlightSelector.new([flight1, flight1])
end

it "should search for flights based on itinerary" do
@it.search(itinerary1).should include(flight1)
end
end
5 changes: 5 additions & 0 deletions spec/models/flight_spec.rb
@@ -0,0 +1,5 @@
require_relative '../../app/models/flight'

describe Flight do

end
12 changes: 12 additions & 0 deletions spec/models/itinerary_spec.rb
@@ -0,0 +1,12 @@
require_relative '../../app/models/itinerary'

describe Itinerary do

context "compare itineraries" do
it "should be same if from/to/date are same" do
it1 = Itinerary.new("Hyderabad", "Bangalore", "01/01/2012")
it2 = Itinerary.new("Hyderabad", "Bangalore", "01/01/2012")
it1.should eq(it2)
end
end
end
7 changes: 7 additions & 0 deletions spec/models/reservation_system_spec.rb
@@ -0,0 +1,7 @@
require_relative '../../app/models/reservation_system'

describe ReservationSystem do



end

0 comments on commit 1a7e804

Please sign in to comment.