Back to Repositories

Testing Google Places Search Integration in geocoder

This test suite validates the Google Places Search functionality within the Geocoder gem, focusing on place details retrieval and URL construction for API requests.

Test Coverage Overview

The test suite thoroughly examines the Google Places Search integration, covering essential place attributes and API request formatting. Key areas tested include:

  • Place details validation (ID, coordinates, rating, types)
  • URL parameter handling and construction
  • Field selection and configuration options
  • Location bias implementation
  • HTTPS protocol enforcement

Implementation Analysis

The testing approach employs Ruby’s unit testing framework with custom assertions for Google Places API responses. The implementation uses a structured setup with configuration management and isolated test cases. The test suite demonstrates proper separation of concerns with helper methods for common operations.

Technical Details

  • Testing Framework: Ruby Test::Unit
  • Configuration: Dynamic API key setup
  • Mocking: Custom test case inheritance
  • Helper Methods: Lookup and location search utilities
  • API Version: Google Places FindPlaceFromText API

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, comprehensive edge case coverage, and clear test naming conventions. Each test focuses on a single assertion, making failure diagnosis straightforward. The code organization separates setup, test cases, and helper methods effectively.

alexreisner/geocoder

test/unit/lookups/google_places_search_test.rb

            
# encoding: utf-8
require 'test_helper'

class GooglePlacesSearchTest < GeocoderTestCase

  def setup
    super
    Geocoder.configure(lookup: :google_places_search)
    set_api_key!(:google_places_search)
  end

  def test_google_places_search_result_contains_place_id
    assert_equal "ChIJhRwB-yFawokR5Phil-QQ3zM", madison_square_garden.place_id
  end

  def test_google_places_search_result_contains_latitude
    assert_equal madison_square_garden.latitude, 40.75050450000001
  end

  def test_google_places_search_result_contains_longitude
    assert_equal madison_square_garden.longitude, -73.9934387
  end

  def test_google_places_search_result_contains_rating
    assert_equal 4.5, madison_square_garden.rating
  end

  def test_google_places_search_result_contains_types
    assert_equal madison_square_garden.types, %w(stadium point_of_interest establishment)
  end

  def test_google_places_search_query_url_contains_language
    url = lookup.query_url(Geocoder::Query.new("some-address", language: "de"))
    assert_match(/language=de/, url)
  end

  def test_google_places_search_query_url_contains_input
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_match(/input=some-address/, url)
  end

  def test_google_places_search_query_url_contains_input_typer
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_match(/inputtype=textquery/, url)
  end

  def test_google_places_search_query_url_always_uses_https
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_match(%r{^https://}, url)
  end

  def test_google_places_search_query_url_contains_every_field_available_by_default
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    fields = %w[business_status formatted_address geometry icon name 
      photos place_id plus_code types opening_hours price_level rating 
      user_ratings_total]
    assert_match(/fields=#{fields.join('%2C')}/, url)
  end

  def test_google_places_search_query_url_contains_specific_fields_when_given
    fields = %w[formatted_address place_id]
    url = lookup.query_url(Geocoder::Query.new("some-address", fields: fields))
    
    assert_match(/fields=#{fields.join('%2C')}/, url)
  end

  def test_google_places_search_query_url_contains_specific_fields_when_configured
    fields = %w[business_status geometry photos]
    Geocoder.configure(google_places_search: {fields: fields})
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_match(/fields=#{fields.join('%2C')}/, url)
    Geocoder.configure(google_places_search: {})
  end

  def test_google_places_search_query_url_omits_fields_when_nil_given
    url = lookup.query_url(Geocoder::Query.new("some-address", fields: nil))
    assert_no_match(/fields=/, url)
  end

  def test_google_places_search_query_url_omits_fields_when_nil_configured
    Geocoder.configure(google_places_search: {fields: nil})
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_no_match(/fields=/, url)
    Geocoder.configure(google_places_search: {})
  end

  def test_google_places_search_query_url_omits_locationbias_by_default
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_no_match(/locationbias=/, url)
  end

  def test_google_places_search_query_url_contains_locationbias_when_configured
    Geocoder.configure(google_places_search: {locationbias: "point:-36.8509,174.7645"})
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_match(/locationbias=point%3A-36.8509%2C174.7645/, url)
    Geocoder.configure(google_places_search: {})
  end

  def test_google_places_search_query_url_contains_locationbias_when_given
    url = lookup.query_url(Geocoder::Query.new("some-address", locationbias: "point:-36.8509,174.7645"))
    assert_match(/locationbias=point%3A-36.8509%2C174.7645/, url)
  end

  def test_google_places_search_query_url_uses_given_locationbias_over_configured
    Geocoder.configure(google_places_search: {locationbias: "point:37.4275,-122.1697"})
    url = lookup.query_url(Geocoder::Query.new("some-address", locationbias: "point:-36.8509,174.7645"))
    assert_match(/locationbias=point%3A-36.8509%2C174.7645/, url)
    Geocoder.configure(google_places_search: {})
  end

  def test_google_places_search_query_url_omits_locationbias_when_nil_given
    Geocoder.configure(google_places_search: {locationbias: "point:37.4275,-122.1697"})
    url = lookup.query_url(Geocoder::Query.new("some-address", locationbias: nil))
    assert_no_match(/locationbias=/, url)
    Geocoder.configure(google_places_search: {})
  end

  def test_google_places_search_query_url_uses_find_place_service
    url = lookup.query_url(Geocoder::Query.new("some-address"))
    assert_match(%r{//maps.googleapis.com/maps/api/place/findplacefromtext/}, url)
  end

  private

  def lookup
    Geocoder::Lookup::GooglePlacesSearch.new
  end

  def madison_square_garden
    Geocoder.search("Madison Square Garden").first
  end
end