Skip to content

Commit

Permalink
Tests: add noextras test to the matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaEsmukov committed Dec 27, 2020
1 parent 10d6b28 commit fbf5ce5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 13 deletions.
4 changes: 3 additions & 1 deletion geopy/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
def __del__(self):
# Cleanup keepalive connections when Geocoder (and, thus, Adapter)
# instances are getting garbage-collected.
self.session.close()
session = getattr(self, "session", None)
if session is not None:
session.close()

def get_text(self, url, *, timeout, headers):
resp = self._request(url, timeout=timeout, headers=headers)
Expand Down
10 changes: 9 additions & 1 deletion test/geocoders/geonames.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import uuid

import pytest
import pytz

from geopy import Point
from geopy.exc import GeocoderAuthenticationFailure, GeocoderQueryError
from geopy.geocoders import GeoNames
from test.geocoders.util import BaseTestGeocoder, env

try:
import pytz
pytz_available = True
except ImportError:
pytz_available = False


class TestUnitGeoNames:

Expand Down Expand Up @@ -138,6 +143,7 @@ async def test_reverse_raises_for_unknown_find_nearby_type(self):
{},
)

@pytest.mark.skipif("not pytz_available")
async def test_reverse_timezone(self):
new_york_point = Point(40.75376406311989, -73.98489005863667)
america_new_york = pytz.timezone("America/New_York")
Expand All @@ -148,6 +154,7 @@ async def test_reverse_timezone(self):
)
assert timezone.raw['countryCode'] == 'US'

@pytest.mark.skipif("not pytz_available")
async def test_reverse_timezone_unknown(self):
await self.reverse_timezone_run(
# Geonames doesn't return `timezoneId` for Antarctica,
Expand Down Expand Up @@ -197,6 +204,7 @@ async def test_geocode(self):
expect_failure=True,
)

@pytest.mark.skipif("not pytz_available")
async def test_reverse_timezone(self):
with pytest.raises(GeocoderAuthenticationFailure):
await self.reverse_timezone_run(
Expand Down
22 changes: 16 additions & 6 deletions test/geocoders/googlev3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
from urllib.parse import parse_qs, urlparse

import pytest
from pytz import timezone

from geopy import exc
from geopy.geocoders import GoogleV3
from geopy.point import Point
from test.geocoders.util import BaseTestGeocoder, env

try:
import pytz
pytz_available = True
except ImportError:
pytz_available = False


class TestGoogleV3(BaseTestGeocoder):
new_york_point = Point(40.75376406311989, -73.98489005863667)
america_new_york = timezone("America/New_York")

@classmethod
def make_geocoder(cls, **kwargs):
Expand Down Expand Up @@ -203,13 +207,15 @@ async def test_zero_results(self):
expect_failure=True,
)

@pytest.mark.skipif("not pytz_available")
async def test_timezone_datetime(self):
await self.reverse_timezone_run(
{"query": self.new_york_point,
"at_time": datetime.utcfromtimestamp(0)},
self.america_new_york,
pytz.timezone("America/New_York"),
)

@pytest.mark.skipif("not pytz_available")
async def test_timezone_at_time_normalization(self):
utc_naive_dt = datetime(2010, 1, 1, 0, 0, 0)
utc_timestamp = 1262304000
Expand All @@ -221,31 +227,35 @@ async def test_timezone_at_time_normalization(self):
utc_timestamp < self.geocoder._normalize_timezone_at_time(None)
)

tz = timezone("Etc/GMT-2")
tz = pytz.timezone("Etc/GMT-2")
local_aware_dt = tz.localize(datetime(2010, 1, 1, 2, 0, 0))
assert(
utc_timestamp == self.geocoder._normalize_timezone_at_time(local_aware_dt)
)

@pytest.mark.skipif("not pytz_available")
async def test_timezone_integer_raises(self):
# In geopy 1.x `at_time` could be an integer -- a unix timestamp.
# This is an error since geopy 2.0.
with pytest.raises(exc.GeocoderQueryError):
await self.reverse_timezone_run(
{"query": self.new_york_point, "at_time": 0},
self.america_new_york,
pytz.timezone("America/New_York"),
)

@pytest.mark.skipif("not pytz_available")
async def test_timezone_no_date(self):
await self.reverse_timezone_run(
{"query": self.new_york_point},
self.america_new_york,
pytz.timezone("America/New_York"),
)

@pytest.mark.skipif("not pytz_available")
async def test_timezone_invalid_at_time(self):
with pytest.raises(exc.GeocoderQueryError):
self.geocoder.reverse_timezone(self.new_york_point, at_time="eek")

@pytest.mark.skipif("not pytz_available")
async def test_reverse_timezone_unknown(self):
await self.reverse_timezone_run(
# Google doesn't return a timezone for Antarctica.
Expand Down
30 changes: 28 additions & 2 deletions test/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@
# - Registry's Internet Settings section on Windows.
WITH_SYSTEM_PROXIES = bool(getproxies())

ADAPTERS = [RequestsAdapter, URLLibAdapter, AioHTTPAdapter]
AVAILABLE_ADAPTERS = [URLLibAdapter]
NOT_AVAILABLE_ADAPTERS = []

try:
import requests # noqa
except ImportError:
NOT_AVAILABLE_ADAPTERS.append(RequestsAdapter)
else:
AVAILABLE_ADAPTERS.append(RequestsAdapter)

try:
import aiohttp # noqa
except ImportError:
NOT_AVAILABLE_ADAPTERS.append(AioHTTPAdapter)
else:
AVAILABLE_ADAPTERS.append(AioHTTPAdapter)


class DummyGeocoder(Geocoder):
Expand Down Expand Up @@ -110,7 +125,7 @@ def remote_website_http_404(remote_website_http):
return urljoin(remote_website_http, "/404")


@pytest.fixture(params=ADAPTERS, autouse=True)
@pytest.fixture(params=AVAILABLE_ADAPTERS, autouse=True)
def adapter_factory(request):
adapter_factory = request.param
with patch.object(
Expand All @@ -137,6 +152,17 @@ async def geocode(*args, **kwargs):
await yield_(geocoder)


@pytest.mark.parametrize("adapter_cls", NOT_AVAILABLE_ADAPTERS)
async def test_not_available_adapters_raise(adapter_cls):
# Note: this test is uselessly parametrized with `adapter_factory`.
with patch.object(
geopy.geocoders.options, "default_adapter_factory", adapter_cls
):
with pytest.raises(ImportError):
async with make_dummy_async_geocoder():
pass


async def test_geocoder_constructor_uses_https_proxy(
timeout, proxy_server, proxy_url, remote_website_trusted_https
):
Expand Down
11 changes: 9 additions & 2 deletions test/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
import pickle
import unittest

import pytz
import pytz.tzinfo
import pytest

from geopy.timezone import Timezone, from_fixed_gmt_offset, from_timezone_name

try:
import pytz
import pytz.tzinfo
pytz_available = True
except ImportError:
pytz_available = False


@pytest.mark.skipif("not pytz_available")
class TimezoneTestCase(unittest.TestCase):

timezone_gmt_offset_hours = 3.0
Expand Down
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
envlist=
py{35,36,37,38,39,310,py3},
py35-async,
py35{-async,-noextras},
lint,
rst,

Expand All @@ -20,6 +20,11 @@ commands = make {env:GEOPY_TOX_TARGET:test}
# (not the whole matrix, to avoid spending extra quota)
setenv = GEOPY_TEST_ADAPTER=geopy.adapters.AioHTTPAdapter

[testenv:py35-noextras]
# Ensure `pip install geopy` without any non-test extras doesn't break.
extras =
dev-test

[gh-actions]
python =
3.5: py35
Expand Down

0 comments on commit fbf5ce5

Please sign in to comment.