From d9c2c7a52ff2bee5b6b5edeefa69a8be5f8ba498 Mon Sep 17 00:00:00 2001
From: Greg Signal <greg@cardboardstraw.com>
Date: Wed, 31 Aug 2016 15:44:28 +1200
Subject: [PATCH] Add test coverage for LocationIQ lookup

---
 lib/geocoder/lookups/locationiq.rb            |  12 ++
 test/fixtures/locationiq_invalid_api_key      |   3 +
 .../fixtures/locationiq_madison_square_garden | 150 ++++++++++++++++++
 test/fixtures/locationiq_no_results           |   1 +
 test/fixtures/locationiq_over_limit           |   1 +
 test/test_helper.rb                           |   8 +
 test/unit/lookups/locationiq_test.rb          |  24 +++
 7 files changed, 199 insertions(+)
 create mode 100644 test/fixtures/locationiq_invalid_api_key
 create mode 100644 test/fixtures/locationiq_madison_square_garden
 create mode 100644 test/fixtures/locationiq_no_results
 create mode 100644 test/fixtures/locationiq_over_limit
 create mode 100644 test/unit/lookups/locationiq_test.rb

diff --git a/lib/geocoder/lookups/locationiq.rb b/lib/geocoder/lookups/locationiq.rb
index 7f14d214..a29581c4 100644
--- a/lib/geocoder/lookups/locationiq.rb
+++ b/lib/geocoder/lookups/locationiq.rb
@@ -16,5 +16,17 @@ module Geocoder::Lookup
       host = configuration[:host] || "locationiq.org/v1"
       "#{protocol}://#{host}/#{method}?key=#{configuration.api_key}&" + url_query_string(query)
     end
+
+    private
+
+    def results(query)
+      return [] unless doc = fetch_data(query)
+
+      if !doc.is_a?(Array) && doc['error'] =~ /Invalid\skey/
+        raise_error(Geocoder::InvalidApiKey, doc['error'])
+      end
+
+      doc.is_a?(Array) ? doc : [doc]
+    end
   end
 end
diff --git a/test/fixtures/locationiq_invalid_api_key b/test/fixtures/locationiq_invalid_api_key
new file mode 100644
index 00000000..636cff25
--- /dev/null
+++ b/test/fixtures/locationiq_invalid_api_key
@@ -0,0 +1,3 @@
+{
+    "error": "Invalid key"
+}
diff --git a/test/fixtures/locationiq_madison_square_garden b/test/fixtures/locationiq_madison_square_garden
new file mode 100644
index 00000000..b1c03ffd
--- /dev/null
+++ b/test/fixtures/locationiq_madison_square_garden
@@ -0,0 +1,150 @@
+[
+
+    {
+        "place_id": "30632629",
+        "licence": "Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.",
+        "osm_type": "way",
+        "osm_id": "24801588",
+        "boundingbox": [
+            "40.749828338623",
+            "40.7511596679688",
+            "-73.9943389892578",
+            "-73.9926528930664"
+        ],
+        "polygonpoints": [
+            [
+                "-73.9943346",
+                "40.7503638"
+            ],
+            [
+                "-73.9942745",
+                "40.7504158"
+            ],
+            [
+                "-73.9942593",
+                "40.750629"
+            ],
+            [
+                "-73.9941343",
+                "40.7508432"
+            ],
+            [
+                "-73.9939794",
+                "40.7509703"
+            ],
+            [
+                "-73.9938042",
+                "40.7510532"
+            ],
+            [
+                "-73.9938025",
+                "40.7511311"
+            ],
+            [
+                "-73.9936051",
+                "40.7511571"
+            ],
+            [
+                "-73.9935673",
+                "40.751105"
+            ],
+            [
+                "-73.9934095",
+                "40.7511089"
+            ],
+            [
+                "-73.9931235",
+                "40.7510548"
+            ],
+            [
+                "-73.9928863",
+                "40.7509311"
+            ],
+            [
+                "-73.9928068",
+                "40.750949"
+            ],
+            [
+                "-73.992721",
+                "40.7508515"
+            ],
+            [
+                "-73.9927444",
+                "40.7507889"
+            ],
+            [
+                "-73.9926693",
+                "40.7506457"
+            ],
+            [
+                "-73.9926597",
+                "40.7503657"
+            ],
+            [
+                "-73.9928305",
+                "40.7500953"
+            ],
+            [
+                "-73.9929757",
+                "40.7499911"
+            ],
+            [
+                "-73.9931281",
+                "40.7499238"
+            ],
+            [
+                "-73.993133",
+                "40.7498631"
+            ],
+            [
+                "-73.9932961",
+                "40.7498306"
+            ],
+            [
+                "-73.9933664",
+                "40.7498742"
+            ],
+            [
+                "-73.993471",
+                "40.7498701"
+            ],
+            [
+                "-73.9938023",
+                "40.7499263"
+            ],
+            [
+                "-73.9940703",
+                "40.7500756"
+            ],
+            [
+                "-73.9941876",
+                "40.7502038"
+            ],
+            [
+                "-73.9942831",
+                "40.7502142"
+            ],
+            [
+                "-73.9943346",
+                "40.7503638"
+            ]
+        ],
+        "lat": "40.7504928941818",
+        "lon": "-73.993466492276",
+        "display_name": "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America",
+        "class": "leisure",
+        "type": "stadium",
+        "address": {
+            "stadium": "Madison Square Garden",
+            "road": "West 31st Street",
+            "suburb": "Long Island City",
+            "city": "New York City",
+            "county": "New York",
+            "state": "New York",
+            "postcode": "10001",
+            "country": "United States of America",
+            "country_code": "us"
+        }
+    }
+
+]
diff --git a/test/fixtures/locationiq_no_results b/test/fixtures/locationiq_no_results
new file mode 100644
index 00000000..1e3ec721
--- /dev/null
+++ b/test/fixtures/locationiq_no_results
@@ -0,0 +1 @@
+[ ]
diff --git a/test/fixtures/locationiq_over_limit b/test/fixtures/locationiq_over_limit
new file mode 100644
index 00000000..9b9f64d1
--- /dev/null
+++ b/test/fixtures/locationiq_over_limit
@@ -0,0 +1 @@
+<html>\n<head>\n<title>Bandwidth limit exceeded</title>\n</head>\n<body>\n<h1>Bandwidth limit exceeded</h1>\n\n<p>You have been temporarily blocked because you have been overusing OSM's geocoding service or because you have not provided sufficient identification of your application. This block will be automatically lifted after a while. Please take the time and adapt your scripts to reduce the number of requests and make sure that you send a valid UserAgent or Referer.</p>\n\n<p>For more information, consult the <a href=\"http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy\">usage policy</a> for the OSM Nominatim server.\n</body>\n</head>\n
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 89aa2bef..9051042b 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -176,6 +176,14 @@ module Geocoder
       end
     end
 
+    require 'geocoder/lookups/locationiq'
+    class Locationiq
+      private
+      def fixture_prefix
+        "locationiq"
+      end
+    end
+
     require 'geocoder/lookups/yandex'
     class Yandex
       private
diff --git a/test/unit/lookups/locationiq_test.rb b/test/unit/lookups/locationiq_test.rb
new file mode 100644
index 00000000..05fee245
--- /dev/null
+++ b/test/unit/lookups/locationiq_test.rb
@@ -0,0 +1,24 @@
+# encoding: utf-8
+require 'unit/lookups/nominatim_test'
+require 'test_helper'
+
+class LocationIq < NominatimTest
+
+  def setup
+    Geocoder.configure(lookup: :locationiq)
+    set_api_key!(:locationiq)
+  end
+
+  def test_url_contains_api_key
+    Geocoder.configure(locationiq: {api_key: "abc123"})
+    query = Geocoder::Query.new("Leadville, CO")
+    assert_equal "http://locationiq.org/v1/search.php?key=abc123&accept-language=en&addressdetails=1&format=json&q=Leadville%2C+CO", query.url
+  end
+
+  def test_raises_exception_with_invalid_api_key
+    Geocoder.configure(always_raise: [Geocoder::InvalidApiKey])
+    assert_raises Geocoder::InvalidApiKey do
+      Geocoder.search("invalid api key")
+    end
+  end
+end
-- 
GitLab