require 'spec_helper' require 'trip_advisor/translation_tool' describe TripAdvisor::TranslationTool do before(:each) do @translation_tool = TripAdvisor::TranslationTool.new(username: 'username', password: 'password') end describe '#search' do it 'returns a results page' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.should be_kind_of(TripAdvisor::TranslationTool::ResultsPaginator) end it 'retrieves the total number of results' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.total.should == 240 end it "returns the first page with an offset of zero" do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.offset.should == 0 end it 'calculates the page total' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.page_count.should == 1 end it 'builds the correct number of translations' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.translations.size.should == 240 end it 'populates the translation ids correctly' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.translations[0, 10].map { |t| t.id }.should == [293439, 314425, 303071, 290857, 292302, 290743, 290861, 290741, 290819, 292279] end it 'populates the translation keys correctly' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.translations[0, 10].map { |t| t.key }.should == ["TAFlights_adjustfilters_ffffef05", "TAFlights_Select_ffffdca8", "TAFlights_SeePrice_19ac", "TAFlights_DeselectAllFiltersAccessibilityLabel_ffffef05", "TAFlights_Departingfrom:_ffffef05", "TAFlights_ItineraryGroupPrice,Name,andItineraryCount_ffffef05", "TAFlights_FilterSelectionHeaderText_ffffef05", "TAFlights_AccessibilityLabeldescribingMarketingAirline,FlightNumber,Departure/ArrivalCity(AirportCode),Aircraft,SeatClass,FlightDuration&Amenities_ffffef05", "TAFlights_NearbyAirportsHeaderText_ffffef05", "TAFlights_Theflightsearchreturnedincompletesearchresults._ffffef05"] end it 'populates the translation notes correctly' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') paginator.translations[0, 10].map { |t| t.note }.should == ["Message asking the user to adjust the current set of overly restrictive filters so that the view will", "Select Button Title", "'See Price' call to action for an individual flight itinerary", "Deselect All Filters Accessibility Label", "'Departing From {Airport Name}' Header Text", "Itinerary Group Price, Name, and Itinerary Count", "Filter Selection Header Text", "Accessibility Label describing Marketing Airline, Flight Number, Departure/Arrival City (Airport Code), Aircraft, Seat Class, Flight Duration & Amenities", "Nearby Airports Header Text", "Invalid Flight Search Results Loaded Error Message"] end it 'raises an error if you try to load a negative offset' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') expect { paginator.load(offset: -1) }.to raise_error(ArgumentError) end it 'raises an error if you try to load an offset greater than the number of translations' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') expect { paginator.load(offset: 400) }.to raise_error(ArgumentError) end it 'raises an error if you try to load a page with a negative index' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') expect { paginator.load(page: -1) }.to raise_error(ArgumentError) end it 'raises an error if you try paginator to load a page beyond the page count' do fixture = fixture_content('search.json') stub_request(:post, "https://username:password@localization.tripadvisor.com/translations/search/keys").with(:body => "{\"lang\":\"en\",\"keyName\":\"TAFlights\"}").to_return(body: fixture, :status => 200) paginator = @translation_tool.search('TAFlights') expect { paginator.load(page: 6) }.to raise_error(ArgumentError) end end describe '#get_translation' do it "sets the translation id" do fixture = fixture_content('translation_details.json') stub_request(:get, "https://username:password@localization.tripadvisor.com/translations/keys/id/290759/translations").to_return(body: fixture, status: 200) translation = @translation_tool.get_translation(290759) translation.id.should == 290759 end it "loads the correct number of localizations" do fixture = fixture_content('translation_details.json') stub_request(:get, "https://username:password@localization.tripadvisor.com/translations/keys/id/290759/translations").to_return(body: fixture, status: 200) translation = @translation_tool.get_translation(290759) translation.localizations.size.should == 46 end it "loads the status for all localizations" do fixture = fixture_content('translation_details.json') stub_request(:get, "https://username:password@localization.tripadvisor.com/translations/keys/id/290759/translations").to_return(body: fixture, status: 200) translation = @translation_tool.get_translation(290759) translation.localizations.map(&:status).uniq.should == [false] end it "loads the locale_identifier for all localizations" do fixture = fixture_content('translation_details.json') stub_request(:get, "https://username:password@localization.tripadvisor.com/translations/keys/id/290759/translations").to_return(body: fixture, status: 200) translation = @translation_tool.get_translation(290759) translation.localizations.map(&:locale_identifier).size.should == 46 translation.localizations.map(&:locale_identifier).should == ["pl", "nb", "en_SG", "sk", "ru", "es_CL", "fi", "nl", "en-GB", "de_AT", "da", "sr", "en_CA", "zh", "en", "fr", "th", "ar", "vi", "el", "de", "en_ZA", "id", "en_MY", "ja", "es_PE", "zh-Hant", "pt", "cs", "en_IE", "en_IN", "es_AR", "es", "ko", "es_MX", "en_AU", "sv", "en_NZ", "iw", "tr", "es_CO", "en_PH", "es_VE", "hu", "it", "uk"] end it "loads the language for all localizations" do fixture = fixture_content('translation_details.json') stub_request(:get, "https://username:password@localization.tripadvisor.com/translations/keys/id/290759/translations").to_return(body: fixture, status: 200) translation = @translation_tool.get_translation(290759) translation.localizations.map(&:language_name).should == ["Polish", "Norwegian", "English (Singapore)", "Slovak", "Russian", "Spanish (Chile)", "Finnish", "Dutch", "English (UK)", "German (Austria)", "Danish", "Serbian", "English (Canada)", "Chinese (China)", "English", "French", "Thai", "Arabic", "Vietnamese", "Greek", "German", "English (South Africa)", "Indonesian", "English (Malaysia)", "Japanese", "Spanish (Peru)", "Chinese (Taiwan)", "Portuguese", "Czech", "English (Ireland)", "English (India)", "Spanish (Argentina)", "Spanish", "Korean", "Spanish (Mexico)", "English (Australia)", "Swedish", "English (New Zealand)", "Hebrew", "Turkish", "Spanish (Colombia)", "English (Philippines)", "Spanish (Venezuela)", "Hungarian", "Italian", "Ukrainian"] end end end