Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module CreateItinerary
- class BuildHiddenCityEventsMap < UseCaseBase
- DEPARTURE_EVENT_INDEX_OFFSETS = [0, 1, 2].freeze
- ARRIVAL_EVENT_INDEX_OFFSETS = [-2, -1, 0].freeze
- SELF_INDEX_OFFSET = [0].freeze
- def before
- @calendar_entries = context.calendar_entries
- @hidden_cities = context.hidden_cities
- end
- def perform_core
- result = {}
- hidden_city_ids = @hidden_cities.map(&:city_id)
- @calendar_entries.each_with_index do |entry, index|
- next unless hidden_city_ids.include?(entry.city_id)
- affected_entries = index_offsets(entry).map { |offset| @calendar_entries[index + offset] }
- affected_entries.each { |affected_entry| result[affected_entry.uuid] = affected_entry }
- end
- context.hidden_city_events_map = result
- end
- private
- def index_offsets(event)
- departure_event_indexes(event) || arrival_event_indexes(event) || SELF_INDEX_OFFSET
- end
- def departure_event_indexes(event)
- DEPARTURE_EVENT_INDEX_OFFSETS if event.uuid.end_with?(Calendar::DEPARTURE_EVENT_SUFFIX)
- end
- def arrival_event_indexes(event)
- ARRIVAL_EVENT_INDEX_OFFSETS if event.uuid.end_with?(Calendar::ARRIVAL_EVENT_SUFFIX)
- end
- end
- end
- require "test_helper"
- describe CreateItinerary::BuildHiddenCityEventsMap do
- describe "perform" do
- let(:hidden_cities) { [] }
- let(:calendar_entries) { [] }
- subject do
- CreateItinerary::BuildHiddenCityEventsMap.perform(
- hidden_cities: hidden_cities,
- calendar_entries: calendar_entries
- )
- end
- describe "hidden_cities is empty" do
- it "sets hidden_city_events_map to an empty hash" do
- result = subject
- assert_equal({}, result.hidden_city_events_map)
- end
- end
- describe "hidden cities is not empty" do
- let(:hidden_city) { FactoryGirl.create(:hidden_city) }
- before { hidden_cities.push(hidden_city) }
- describe "calendar entries is empty" do
- it "sets hidden_city_events_map to an empty hash" do
- result = subject
- assert_equal({}, result.hidden_city_events_map)
- end
- end
- describe "calendar_entries is not empty" do
- let(:calendar_entry1) { FactoryGirl.build(:calendar_entry, city: hidden_city.city) }
- let(:calendar_entry2) { FactoryGirl.build(:calendar_entry) }
- let(:calendar_entry3) { FactoryGirl.build(:calendar_entry, city: hidden_city.city) }
- before { calendar_entries.push(calendar_entry1, calendar_entry2, calendar_entry3) }
- it "adds the events from hidden cities to the hidden_city_events_map" do
- result = subject
- expected_map = { calendar_entry1.uuid => calendar_entry1, calendar_entry3.uuid => calendar_entry3 }
- assert_equal expected_map, result.hidden_city_events_map
- end
- describe "calendar_entries has expanded flight events" do
- let(:in_flight_event) do
- calendar_entry = FactoryGirl.build(:calendar_entry)
- calendar_entry.origin = FactoryGirl.build(:event_origin, calendar: calendar_entry)
- calendar_entry
- end
- let(:departure_event) { FactoryGirl.build(:calendar_entry, uuid: "#{in_flight_event.uuid}_departure") }
- let(:arrival_event) { FactoryGirl.build(:calendar_entry, uuid: "#{in_flight_event.uuid}_arrival") }
- before { calendar_entries.push(departure_event, in_flight_event, arrival_event) }
- describe "the departure event is from the same city as the hidden cities" do
- before { departure_event.city = hidden_city.city }
- it "adds the related events to the hidden_city_events_map" do
- result = subject
- assert_equal in_flight_event, result.hidden_city_events_map[in_flight_event.uuid]
- assert_equal departure_event, result.hidden_city_events_map[departure_event.uuid]
- assert_equal arrival_event, result.hidden_city_events_map[arrival_event.uuid]
- end
- end
- describe "the arrival event is from the same city as the hidden cities" do
- before { arrival_event.city = hidden_city.city }
- it "adds the related events to the hidden_city_events_map" do
- result = subject
- assert_equal in_flight_event, result.hidden_city_events_map[in_flight_event.uuid]
- assert_equal departure_event, result.hidden_city_events_map[departure_event.uuid]
- assert_equal arrival_event, result.hidden_city_events_map[arrival_event.uuid]
- end
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement