Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
],
test_suite='pytest',
tests_require=[
'httpretty>=1.1.4',
'responses>=0.25.7',
'pylint==2.17.4',
'pytest>=7.4.0'
],
Expand Down
42 changes: 22 additions & 20 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from pathlib import Path

import os
import httpretty
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode

# reduce maximum backoff retry time from 120s to 1s
Expand All @@ -22,51 +21,54 @@ def _any_result_around(results, lat=None, lon=None):
return True
return False

@httprettified
@responses.activate
def test_gb_postcode():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8")
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8"),
status=200
)

results = geocoder.geocode("EC1M 5RF")
assert _any_result_around(results, lat=51.5201666, lon=-0.0985142)


@httprettified
@responses.activate
def test_australia():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/mudgee_australia.json').read_text(encoding="utf-8")
body=Path('test/fixtures/mudgee_australia.json').read_text(encoding="utf-8"),
status=200
)

results = geocoder.geocode("Mudgee, Australia")
assert _any_result_around(results, lat=-32.5980702, lon=149.5886383)


@httprettified
@responses.activate
def test_munster():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/muenster.json').read_text(encoding="utf-8")
body=Path('test/fixtures/muenster.json').read_text(encoding="utf-8"),
status=200
)

results = geocoder.geocode("Münster")
assert _any_result_around(results, lat=51.9625101, lon=7.6251879)

@httprettified
@responses.activate
def test_donostia():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/donostia.json').read_text(encoding="utf-8")

body=Path('test/fixtures/donostia.json').read_text(encoding="utf-8"),
status=200
)

results =geocoder.geocode("Donostia")
results = geocoder.geocode("Donostia")
assert _any_result_around(results, lat=43.300836, lon=-1.9809529)

# test that the results are in unicode
Expand Down
9 changes: 4 additions & 5 deletions test/test_error_blocked.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from pathlib import Path

import pytest
import httpretty
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode
from opencage.geocoder import ForbiddenError


geocoder = OpenCageGeocode('2e10e5e828262eb243ec0b54681d699a') # will always return 403

@httprettified
@responses.activate
def test_api_key_blocked():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/403_apikey_disabled.json').read_text(encoding="utf-8"),
status=403,
Expand Down
12 changes: 6 additions & 6 deletions test/test_error_invalid_input.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import pytest

import httpretty
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode
from opencage.geocoder import InvalidInputError

geocoder = OpenCageGeocode('abcde')

@httprettified
@responses.activate
def test_must_be_unicode_string():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body='{"results":{}}'
body='{"results":{}}',
status=200
)

# Should not give errors
Expand Down
9 changes: 4 additions & 5 deletions test/test_error_not_authorized.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from pathlib import Path

import httpretty
import pytest
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode
from opencage.geocoder import NotAuthorizedError

geocoder = OpenCageGeocode('unauthorized-key')

@httprettified
@responses.activate
def test_api_key_not_authorized():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/401_not_authorized.json').read_text(encoding="utf-8"),
status=401,
Expand Down
20 changes: 10 additions & 10 deletions test/test_error_ratelimit_exceeded.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
from pathlib import Path

import httpretty
import pytest
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode
from opencage.geocoder import RateLimitExceededError

geocoder = OpenCageGeocode('abcde')

@httprettified
@responses.activate
def test_no_rate_limit():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/no_ratelimit.json').read_text(encoding="utf-8")
body=Path('test/fixtures/no_ratelimit.json').read_text(encoding="utf-8"),
status=200
)
# shouldn't raise an exception
geocoder.geocode("whatever")


@httprettified
@responses.activate
def test_rate_limit_exceeded():
# 4372eff77b8343cebfc843eb4da4ddc4 will always return 402
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/402_rate_limit_exceeded.json').read_text(encoding="utf-8"),
status=402,
adding_headers={
headers={
'X-RateLimit-Limit': '2500',
'X-RateLimit-Remaining': '0',
'X-RateLimit-Reset': '1402185600'
Expand Down
25 changes: 13 additions & 12 deletions test/test_error_unknown.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import pytest

import httpretty
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode
from opencage.geocoder import UnknownError

geocoder = OpenCageGeocode('abcde')

@httprettified
@responses.activate
def test_http_500_status():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body='{}',
status=500,
)

Expand All @@ -21,14 +21,14 @@ def test_http_500_status():

assert str(excinfo.value) == '500 status code from API'

@httprettified
@responses.activate
def test_non_json():
"These kinds of errors come from webserver and may not be JSON"
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body='<html><body><h1>503 Service Unavailable</h1></body></html>',
forcing_headers={
headers={
'Content-Type': 'text/html',
},
status=503
Expand All @@ -39,12 +39,13 @@ def test_non_json():

assert str(excinfo.value) == 'Non-JSON result from server'

@httprettified
@responses.activate
def test_no_results_key():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body='{"spam": "eggs"}',
status=200, # Need to specify status code with responses
)

with pytest.raises(UnknownError) as excinfo:
Expand Down
17 changes: 10 additions & 7 deletions test/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import os
import re
import httpretty
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode

# reduce maximum backoff retry time from 120s to 1s
Expand All @@ -16,15 +15,19 @@

user_agent_format = re.compile(r'^opencage-python/[\d\.]+ Python/[\d\.]+ (requests|aiohttp)/[\d\.]+ \(OpenCage Test\)$')

@httprettified
@responses.activate
def test_sync():
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8")
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8"),
status=200
)

geocoder.geocode("EC1M 5RF")
user_agent = httpretty.last_request().headers['User-Agent']

# Check the User-Agent header in the most recent request
request = responses.calls[-1].request
user_agent = request.headers['User-Agent']

assert user_agent_format.match(user_agent) is not None
18 changes: 9 additions & 9 deletions test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from pathlib import Path

import httpretty
import pytest
import responses

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode
from opencage.geocoder import NotAuthorizedError

Expand All @@ -16,23 +15,24 @@ def _any_result_around(results, lat=None, lon=None):
return True
return False

@httprettified
@responses.activate
def test_success():
with OpenCageGeocode('abcde') as geocoder:
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8")
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8"),
status=200
)

results = geocoder.geocode("EC1M 5RF")
assert _any_result_around(results, lat=51.5201666, lon=-0.0985142)

@httprettified
@responses.activate
def test_failure():
with OpenCageGeocode('unauthorized-key') as geocoder:
httpretty.register_uri(
httpretty.GET,
responses.add(
responses.GET,
geocoder.url,
body=Path('test/fixtures/401_not_authorized.json').read_text(encoding="utf-8"),
status=401,
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ python =

[testenv]
deps =
httpretty
responses
pytest
pytest-aiohttp
pytest-asyncio
Expand All @@ -22,7 +22,7 @@ commands =
[testenv:lint]
usedevelop = True
deps =
httpretty
responses
pylint==2.17.4
pytest
commands =
Expand Down