From cfdac4998c646891c58caa22bf4c0f2674a9bccd Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Wed, 7 Nov 2012 20:20:03 -0500
Subject: [PATCH] Add test for caching.

This requires changing the way stubs work, so that they only simulate
actual HTTP calls, not cache hits.
---
 test/cache_test.rb          | 19 +++++++++++++++++
 test/error_handling_test.rb |  3 +++
 test/lookup_test.rb         |  1 +
 test/result_test.rb         |  1 +
 test/services_test.rb       | 20 +-----------------
 test/test_helper.rb         | 42 ++++++++++++++++++++++++++++---------
 6 files changed, 57 insertions(+), 29 deletions(-)
 create mode 100644 test/cache_test.rb

diff --git a/test/cache_test.rb b/test/cache_test.rb
new file mode 100644
index 00000000..f505b805
--- /dev/null
+++ b/test/cache_test.rb
@@ -0,0 +1,19 @@
+# encoding: utf-8
+require 'test_helper'
+
+class CacheTest < Test::Unit::TestCase
+
+  def test_second_occurrence_of_request_is_cache_hit
+    Geocoder::Configuration.cache = {}
+    Geocoder::Lookup.all_services_except_test.each do |l|
+      Geocoder::Configuration.lookup = l
+      set_api_key!(l)
+      results = Geocoder.search("Madison Square Garden")
+      assert !results.first.cache_hit,
+        "Lookup #{l} returned erroneously cached result."
+      results = Geocoder.search("Madison Square Garden")
+      assert results.first.cache_hit,
+        "Lookup #{l} did not return cached result."
+    end
+  end
+end
diff --git a/test/error_handling_test.rb b/test/error_handling_test.rb
index 002425b6..01f5f733 100644
--- a/test/error_handling_test.rb
+++ b/test/error_handling_test.rb
@@ -12,6 +12,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
     orig = $VERBOSE; $VERBOSE = nil
     Geocoder::Lookup.all_services_except_test.each do |l|
       Geocoder::Configuration.lookup = l
+      set_api_key!(l)
       assert_nothing_raised { Geocoder.search("timeout") }
     end
   ensure
@@ -22,6 +23,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
     Geocoder::Configuration.always_raise = [TimeoutError]
     Geocoder::Lookup.all_services_except_test.each do |l|
       lookup = Geocoder::Lookup.get(l)
+      set_api_key!(l)
       assert_raises TimeoutError do
         lookup.send(:results, Geocoder::Query.new("timeout"))
       end
@@ -32,6 +34,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
     Geocoder::Configuration.always_raise = [SocketError]
     Geocoder::Lookup.all_services_except_test.each do |l|
       lookup = Geocoder::Lookup.get(l)
+      set_api_key!(l)
       assert_raises SocketError do
         lookup.send(:results, Geocoder::Query.new("socket_error"))
       end
diff --git a/test/lookup_test.rb b/test/lookup_test.rb
index a0c8cd22..b721e4a1 100644
--- a/test/lookup_test.rb
+++ b/test/lookup_test.rb
@@ -6,6 +6,7 @@ class LookupTest < Test::Unit::TestCase
   def test_search_returns_empty_array_when_no_results
     Geocoder::Lookup.all_services_except_test.each do |l|
       lookup = Geocoder::Lookup.get(l)
+      set_api_key!(l)
       assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")),
         "Lookup #{l} does not return empty array when no results."
     end
diff --git a/test/result_test.rb b/test/result_test.rb
index ab207c5f..f826648d 100644
--- a/test/result_test.rb
+++ b/test/result_test.rb
@@ -6,6 +6,7 @@ class ResultTest < Test::Unit::TestCase
   def test_result_has_required_attributes
     Geocoder::Lookup.all_services_except_test.each do |l|
       Geocoder::Configuration.lookup = l
+      set_api_key!(l)
       result = Geocoder.search([45.423733, -75.676333]).first
       assert_result_has_required_attributes(result)
     end
diff --git a/test/services_test.rb b/test/services_test.rb
index 2e6cd3af..540f71c3 100644
--- a/test/services_test.rb
+++ b/test/services_test.rb
@@ -54,6 +54,7 @@ class ServicesTest < Test::Unit::TestCase
 
   def test_google_premier_result_components
     Geocoder::Configuration.lookup = :google_premier
+    set_api_key!(:google_premier)
     result = Geocoder.search("Madison Square Garden, New York, NY").first
     assert_equal "Manhattan",
       result.address_components_of_type(:sublocality).first['long_name']
@@ -185,23 +186,4 @@ class ServicesTest < Test::Unit::TestCase
     assert_equal "46 West 31st Street, New York, NY, 10001, US",
       result.address
   end
-
-  private # ------------------------------------------------------------------
-
-  def set_api_key!(lookup_name)
-    if lookup_name == :google_premier
-      Geocoder::Configuration.api_key = [
-        'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
-        'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
-        'cccccccccccccccccccccccccccccc'
-      ]
-    elsif lookup_name == :yahoo
-      Geocoder::Configuration.api_key = [
-        'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
-        'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
-      ]
-    else
-      Geocoder::Configuration.api_key = nil
-    end
-  end
 end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 8285233e..81deb6f3 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -75,13 +75,19 @@ module Geocoder
     class Base
       private #-----------------------------------------------------------------
       def read_fixture(file)
-        File.read(File.join("test", "fixtures", file)).strip.gsub(/\n\s*/, "")
+        filepath = File.join("test", "fixtures", file)
+        s = File.read(filepath).strip.gsub(/\n\s*/, "")
+        s.instance_eval do
+          def body; self; end
+          def code; "200"; end
+        end
+        s
       end
     end
 
     class Google < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         file = case query.text
@@ -99,7 +105,7 @@ module Geocoder
 
     class Yahoo < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         file = case query.text
@@ -113,7 +119,7 @@ module Geocoder
 
     class Yandex < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         file = case query.text
@@ -127,7 +133,7 @@ module Geocoder
 
     class GeocoderCa < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         if query.reverse_geocode?
@@ -144,7 +150,7 @@ module Geocoder
 
     class Freegeoip < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         file = case query.text
@@ -157,7 +163,7 @@ module Geocoder
 
     class Bing < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         if query.reverse_geocode?
@@ -174,7 +180,7 @@ module Geocoder
 
     class Nominatim < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         file = case query.text
@@ -187,7 +193,7 @@ module Geocoder
 
     class Mapquest < Base
       private #-----------------------------------------------------------------
-      def fetch_raw_data(query)
+      def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
         file = case query.text
@@ -318,5 +324,21 @@ class Test::Unit::TestCase
     return false unless coordinates.size == 2 # Should have dimension 2
     coordinates[0].nan? && coordinates[1].nan? # Both coordinates should be NaN
   end
-end
 
+  def set_api_key!(lookup_name)
+    if lookup_name == :google_premier
+      Geocoder::Configuration.api_key = [
+        'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
+        'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
+        'cccccccccccccccccccccccccccccc'
+      ]
+    elsif lookup_name == :yahoo
+      Geocoder::Configuration.api_key = [
+        'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
+        'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
+      ]
+    else
+      Geocoder::Configuration.api_key = nil
+    end
+  end
+end
-- 
GitLab