Skip to content
Snippets Groups Projects
Commit 1806cfcf authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #690 from mtmail/opencagedata-overlimittest

Opencagedata: additional tests
parents 162097c9 a60ffe00
No related branches found
No related tags found
No related merge requests found
...@@ -18,14 +18,27 @@ module Geocoder::Lookup ...@@ -18,14 +18,27 @@ module Geocoder::Lookup
private private
def valid_response?(response)
status = parse_json(response.body)["status"]
super(response) and status['message'] == 'OK'
end
def results(query) def results(query)
data = fetch_data(query) return [] unless doc = fetch_data(query)
(data && data['results']) || [] # return doc["results"]
messages = doc['status']['message']
case doc['status']['code']
when 400 # Error with input
raise_error(Geocoder::InvalidRequest, messages) ||
warn("Opencagedata Geocoding API error: #{messages}")
when 403 # Key related error
raise_error(Geocoder::InvalidApiKey, messages) ||
warn("Opencagedata Geocoding API error: #{messages}")
when 402 # Quata Exceeded
raise_error(Geocoder::OverQueryLimitError, messages) ||
warn("Opencagedata Geocoding API error: #{messages}")
when 500 # Unknown error
raise_error(Geocoder::Error, messages) ||
warn("Opencagedata Geocoding API error: #{messages}")
end
return doc["results"]
end end
def query_url_params(query) def query_url_params(query)
......
{
"licenses": [
{
"name": "CC-BY-SA",
"url": "http://creativecommons.org/licenses/by-sa/3.0/"
},
{
"name": "ODbL",
"url": "http://opendatacommons.org/licenses/odbl/summary/"
}
],
"results": [ ],
"status": {
"code": 403,
"message": "invalid API key"
},
"thanks": "For using an OpenCage Data API",
"timestamp": {
"created_http": "Thu, 07 Aug 2014 14:26:28 GMT",
"created_unix": 1407421588
},
"total_results": 0,
"we_are_hiring": "http://lokku.com/#jobs"
}
\ No newline at end of file
{
"licenses": [
{
"name": "CC-BY-SA",
"url": "http://creativecommons.org/licenses/by-sa/3.0/"
},
{
"name": "ODbL",
"url": "http://opendatacommons.org/licenses/odbl/summary/"
}
],
"results": [ ],
"status": {
"code": 400,
"message": "Attribute (format) does not pass the type constraint because: not a valid format"
},
"thanks": "For using an OpenCage Data API",
"timestamp": {
"created_http": "Thu, 07 Aug 2014 14:27:29 GMT",
"created_unix": 1407421649
},
"total_results": 0,
"we_are_hiring": "http://lokku.com/#jobs"
}
\ No newline at end of file
{
"licenses": [
{
"name": "CC-BY-SA",
"url": "http://creativecommons.org/licenses/by-sa/3.0/"
},
{
"name": "ODbL",
"url": "http://opendatacommons.org/licenses/odbl/summary/"
}
],
"rate": {
"limit": 1,
"remaining": 0,
"reset": null
},
"results": [ ],
"status": {
"code": 402,
"message": "quota exceeded"
},
"thanks": "For using an OpenCage Data API",
"timestamp": {
"created_http": "Thu, 07 Aug 2014 13:59:19 GMT",
"created_unix": 1407419959
},
"total_results": 0,
"we_are_hiring": "http://lokku.com/#jobs"
}
\ No newline at end of file
...@@ -39,12 +39,26 @@ class OpencagedataTest < GeocoderTestCase ...@@ -39,12 +39,26 @@ class OpencagedataTest < GeocoderTestCase
def test_raises_exception_when_invalid_request
Geocoder.configure(always_raise: [Geocoder::InvalidRequest])
assert_raises Geocoder::InvalidRequest do
Geocoder.search("invalid request")
end
end
def test_raises_exception_when_invalid_api_key
Geocoder.configure(always_raise: [Geocoder::InvalidApiKey])
assert_raises Geocoder::InvalidApiKey do
Geocoder.search("invalid api key")
end
end
# def test_raises_exception_when_over_query_limit
# Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError]) def test_raises_exception_when_over_query_limit
# l = Geocoder::Lookup.get(:opencagedata) Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
# assert_raises Geocoder::OverQueryLimitError do l = Geocoder::Lookup.get(:opencagedata)
# l.send(:results, Geocoder::Query.new("over limit")) assert_raises Geocoder::OverQueryLimitError do
# end l.send(:results, Geocoder::Query.new("over limit"))
# end end
end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment