SHARE
TWEET

Untitled

a guest Feb 17th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module CreateItinerary
  2.   class BuildHiddenCityEventsMap < UseCaseBase
  3.     DEPARTURE_EVENT_INDEX_OFFSETS = [0, 1, 2].freeze
  4.     ARRIVAL_EVENT_INDEX_OFFSETS = [-2, -1, 0].freeze
  5.     SELF_INDEX_OFFSET = [0].freeze
  6.  
  7.     def before
  8.       @calendar_entries = context.calendar_entries
  9.       @hidden_cities = context.hidden_cities
  10.     end
  11.  
  12.     def perform_core
  13.       result = {}
  14.       hidden_city_ids = @hidden_cities.map(&:city_id)
  15.       @calendar_entries.each_with_index do |entry, index|
  16.         next unless hidden_city_ids.include?(entry.city_id)
  17.         affected_entries = index_offsets(entry).map { |offset| @calendar_entries[index + offset] }
  18.         affected_entries.each { |affected_entry| result[affected_entry.uuid] = affected_entry }
  19.       end
  20.       context.hidden_city_events_map = result
  21.     end
  22.  
  23.     private
  24.  
  25.     def index_offsets(event)
  26.       departure_event_indexes(event) || arrival_event_indexes(event) || SELF_INDEX_OFFSET
  27.     end
  28.  
  29.     def departure_event_indexes(event)
  30.       DEPARTURE_EVENT_INDEX_OFFSETS if event.uuid.end_with?(Calendar::DEPARTURE_EVENT_SUFFIX)
  31.     end
  32.  
  33.     def arrival_event_indexes(event)
  34.       ARRIVAL_EVENT_INDEX_OFFSETS if event.uuid.end_with?(Calendar::ARRIVAL_EVENT_SUFFIX)
  35.     end
  36.   end
  37. end
  38.  
  39. require "test_helper"
  40.  
  41. describe CreateItinerary::BuildHiddenCityEventsMap do
  42.   describe "perform" do
  43.     let(:hidden_cities) { [] }
  44.     let(:calendar_entries) { [] }
  45.  
  46.     subject do
  47.       CreateItinerary::BuildHiddenCityEventsMap.perform(
  48.         hidden_cities: hidden_cities,
  49.         calendar_entries: calendar_entries
  50.       )
  51.     end
  52.  
  53.     describe "hidden_cities is empty" do
  54.       it "sets hidden_city_events_map to an empty hash" do
  55.         result = subject
  56.  
  57.         assert_equal({}, result.hidden_city_events_map)
  58.       end
  59.     end
  60.  
  61.     describe "hidden cities is not empty" do
  62.       let(:hidden_city) { FactoryGirl.create(:hidden_city) }
  63.  
  64.       before { hidden_cities.push(hidden_city) }
  65.  
  66.       describe "calendar entries is empty" do
  67.         it "sets hidden_city_events_map to an empty hash" do
  68.           result = subject
  69.  
  70.           assert_equal({}, result.hidden_city_events_map)
  71.         end
  72.       end
  73.  
  74.       describe "calendar_entries is not empty" do
  75.         let(:calendar_entry1) { FactoryGirl.build(:calendar_entry, city: hidden_city.city) }
  76.         let(:calendar_entry2) { FactoryGirl.build(:calendar_entry) }
  77.         let(:calendar_entry3) { FactoryGirl.build(:calendar_entry, city: hidden_city.city) }
  78.  
  79.         before { calendar_entries.push(calendar_entry1, calendar_entry2, calendar_entry3) }
  80.  
  81.         it "adds the events from hidden cities to the hidden_city_events_map" do
  82.           result = subject
  83.  
  84.           expected_map = { calendar_entry1.uuid => calendar_entry1, calendar_entry3.uuid => calendar_entry3 }
  85.           assert_equal expected_map, result.hidden_city_events_map
  86.         end
  87.  
  88.         describe "calendar_entries has expanded flight events" do
  89.           let(:in_flight_event) do
  90.             calendar_entry = FactoryGirl.build(:calendar_entry)
  91.             calendar_entry.origin = FactoryGirl.build(:event_origin, calendar: calendar_entry)
  92.             calendar_entry
  93.           end
  94.           let(:departure_event) { FactoryGirl.build(:calendar_entry, uuid: "#{in_flight_event.uuid}_departure") }
  95.           let(:arrival_event) { FactoryGirl.build(:calendar_entry, uuid: "#{in_flight_event.uuid}_arrival") }
  96.  
  97.           before { calendar_entries.push(departure_event, in_flight_event, arrival_event) }
  98.  
  99.           describe "the departure event is from the same city as the hidden cities" do
  100.             before { departure_event.city = hidden_city.city }
  101.  
  102.             it "adds the related events to the hidden_city_events_map" do
  103.               result = subject
  104.  
  105.               assert_equal in_flight_event, result.hidden_city_events_map[in_flight_event.uuid]
  106.               assert_equal departure_event, result.hidden_city_events_map[departure_event.uuid]
  107.               assert_equal arrival_event, result.hidden_city_events_map[arrival_event.uuid]
  108.             end
  109.           end
  110.  
  111.           describe "the arrival event is from the same city as the hidden cities" do
  112.             before { arrival_event.city = hidden_city.city }
  113.  
  114.             it "adds the related events to the hidden_city_events_map" do
  115.               result = subject
  116.  
  117.               assert_equal in_flight_event, result.hidden_city_events_map[in_flight_event.uuid]
  118.               assert_equal departure_event, result.hidden_city_events_map[departure_event.uuid]
  119.               assert_equal arrival_event, result.hidden_city_events_map[arrival_event.uuid]
  120.             end
  121.           end
  122.         end
  123.       end
  124.     end
  125.   end
  126. end
RAW Paste Data
Top