Skip to content
Open
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
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ classifiers = [
"Topic :: Scientific/Engineering :: GIS",
]
dependencies = [
"httpx>=0.24.0",
"httpx2>=2.0.0",
]

[project.optional-dependencies]
dev = [
"pytest>=7.0.0",
"pytest-cov>=4.0.0",
"pytest-mock>=3.10.0",
"pytest-httpx>=0.27.0",
"black>=23.0.0",
"isort>=5.12.0",
"flake8>=6.0.0",
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pytest>=7.0.0
pytest-cov>=4.0.0
pytest-mock>=3.10.0
pytest-httpx>=0.27.0
black>=23.0.0
isort>=5.12.0
flake8>=6.0.0
Expand Down
2 changes: 1 addition & 1 deletion src/geocodio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
from typing import Dict, List, Optional, Tuple, Union

import httpx
import httpx2 as httpx

from geocodio._version import __version__

Expand Down
2 changes: 1 addition & 1 deletion src/geocodio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar

import httpx
import httpx2 as httpx

T = TypeVar("T", bound="ExtrasMixin")

Expand Down
72 changes: 72 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,82 @@
import os

import pytest
import httpx2 as httpx
from dotenv import load_dotenv

from geocodio import Geocodio


class _HttpxMock:
"""Simple compatibility replacement for pytest-httpx's httpx_mock fixture."""

def __init__(self):
self._callbacks = []

def add_callback(self, callback=None, **kwargs):
if callback is None:
raise ValueError("callback is required")
self._callbacks.append(callback)

def add_response(
self,
*,
url=None,
match_headers=None,
status_code=200,
headers=None,
content=None,
json=None,
text=None,
):
self._callbacks.append(
lambda request: httpx.Response(
status_code,
headers=headers,
content=content,
json=json,
text=text,
)
)

def _assert_options(self):
assert not self._callbacks, (
"The following responses are mocked but not requested: "
f"{self._callbacks}"
)


@pytest.fixture
def httpx_mock(monkeypatch):
"""Patch httpx2 transport methods to satisfy the tests' httpx_mock API."""
mock = _HttpxMock()
real_handle_request = httpx.HTTPTransport.handle_request

def mocked_handle_request(transport, request):
if not mock._callbacks:
return real_handle_request(transport, request)
callback = mock._callbacks.pop(0)
return callback(request)

monkeypatch.setattr(httpx.HTTPTransport, "handle_request", mocked_handle_request)

real_handle_async_request = httpx.AsyncHTTPTransport.handle_async_request

async def mocked_handle_async_request(transport, request):
if not mock._callbacks:
return await real_handle_async_request(transport, request)
callback = mock._callbacks.pop(0)
return callback(request)

monkeypatch.setattr(
httpx.AsyncHTTPTransport,
"handle_async_request",
mocked_handle_async_request,
)

yield mock
mock._assert_options()

# Load environment variables from .env file
load_dotenv()

Expand Down
18 changes: 9 additions & 9 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tests for the Geocodio class
"""

import httpx
import httpx2 as httpx
import pytest

from geocodio import Geocodio
Expand Down Expand Up @@ -231,8 +231,8 @@ def test_user_agent_header_in_request(mocker):
"""Test that the User-Agent header is included in all requests."""
from geocodio import __version__

# Mock the httpx.Client.request method to capture headers
mock_httpx_request = mocker.patch("httpx.Client.request")
# Mock the httpx2.Client.request method to capture headers
mock_httpx_request = mocker.patch("httpx2.Client.request")
mock_httpx_request.return_value = httpx.Response(
200,
json={
Expand Down Expand Up @@ -281,8 +281,8 @@ def test_user_agent_header_in_batch_request(mocker):
"""Test that the User-Agent header is included in batch requests."""
from geocodio import __version__

# Mock the httpx.Client.request method
mock_httpx_request = mocker.patch("httpx.Client.request")
# Mock the httpx2.Client.request method
mock_httpx_request = mocker.patch("httpx2.Client.request")
mock_httpx_request.return_value = httpx.Response(200, json={"results": []})

client = Geocodio("test-api-key")
Expand All @@ -300,8 +300,8 @@ def test_user_agent_header_in_reverse_geocode(mocker):
"""Test that the User-Agent header is included in reverse geocoding requests."""
from geocodio import __version__

# Mock the httpx.Client.request method
mock_httpx_request = mocker.patch("httpx.Client.request")
# Mock the httpx2.Client.request method
mock_httpx_request = mocker.patch("httpx2.Client.request")
mock_httpx_request.return_value = httpx.Response(
200,
json={
Expand Down Expand Up @@ -338,8 +338,8 @@ def test_user_agent_header_in_list_api(mocker):
"""Test that the User-Agent header is included in List API requests."""
from geocodio import __version__

# Mock the httpx.Client.request method
mock_httpx_request = mocker.patch("httpx.Client.request")
# Mock the httpx2.Client.request method
mock_httpx_request = mocker.patch("httpx2.Client.request")
mock_httpx_request.return_value = httpx.Response(
200,
json={
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import json

import httpx
import httpx2 as httpx
import pytest

from geocodio import (
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import httpx
import httpx2 as httpx
import pytest

from geocodio.exceptions import (
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_geocode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from pathlib import Path

import httpx
import httpx2 as httpx

from geocodio.models import AddressComponents, GeocodingResponse

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_reverse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import httpx
import httpx2 as httpx
import pytest

from geocodio.models import GeocodingResponse, Location
Expand Down
Loading