Skip to content

Commit bd23cec

Browse files
committed
Fix CI pipeline by adding conditional imports for async tests
- Added try/except blocks around httpx and async module imports - Added @unittest.skipIf decorators to skip tests when httpx unavailable - Ensures tests can be discovered by pytest without failing on import - Fixes ImportError in CI when httpx extra dependencies not installed
1 parent 7e48111 commit bd23cec

4 files changed

Lines changed: 76 additions & 38 deletions

File tree

tests/amazon_creatorsapi/aio/api_test.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,34 @@
33
import unittest
44
from unittest.mock import AsyncMock, MagicMock, patch
55

6-
from amazon_creatorsapi.aio import (
7-
AsyncAmazonCreatorsApi,
8-
)
9-
from amazon_creatorsapi.errors import (
10-
AssociateValidationError,
11-
InvalidArgumentError,
12-
ItemsNotFoundError,
13-
RequestError,
14-
TooManyRequestsError,
15-
)
16-
from creatorsapi_python_sdk.models.condition import Condition
17-
from creatorsapi_python_sdk.models.get_browse_nodes_resource import (
18-
GetBrowseNodesResource,
19-
)
20-
from creatorsapi_python_sdk.models.get_items_resource import GetItemsResource
21-
from creatorsapi_python_sdk.models.get_variations_resource import GetVariationsResource
22-
from creatorsapi_python_sdk.models.search_items_resource import SearchItemsResource
23-
from creatorsapi_python_sdk.models.sort_by import SortBy
24-
25-
6+
try:
7+
from amazon_creatorsapi.aio import (
8+
AsyncAmazonCreatorsApi,
9+
)
10+
from amazon_creatorsapi.errors import (
11+
AssociateValidationError,
12+
InvalidArgumentError,
13+
ItemsNotFoundError,
14+
RequestError,
15+
TooManyRequestsError,
16+
)
17+
from creatorsapi_python_sdk.models.condition import Condition
18+
from creatorsapi_python_sdk.models.get_browse_nodes_resource import (
19+
GetBrowseNodesResource,
20+
)
21+
from creatorsapi_python_sdk.models.get_items_resource import GetItemsResource
22+
from creatorsapi_python_sdk.models.get_variations_resource import (
23+
GetVariationsResource,
24+
)
25+
from creatorsapi_python_sdk.models.search_items_resource import SearchItemsResource
26+
from creatorsapi_python_sdk.models.sort_by import SortBy
27+
28+
HTTPX_AVAILABLE = True
29+
except ImportError:
30+
HTTPX_AVAILABLE = False
31+
32+
33+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
2634
class TestAsyncAmazonCreatorsApiInit(unittest.TestCase):
2735
"""Tests for AsyncAmazonCreatorsApi initialization."""
2836

@@ -117,6 +125,7 @@ def test_raises_error_for_invalid_version(
117125
self.assertIn("Supported versions are:", str(context.exception))
118126

119127

128+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
120129
class TestAsyncAmazonCreatorsApiContextManager(unittest.IsolatedAsyncioTestCase):
121130
"""Tests for AsyncAmazonCreatorsApi async context manager."""
122131

@@ -160,6 +169,7 @@ async def test_context_manager_exit_without_client(
160169
await api.__aexit__(None, None, None)
161170

162171

172+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
163173
class TestAsyncAmazonCreatorsApiGetItems(unittest.IsolatedAsyncioTestCase):
164174
"""Tests for get_items() method."""
165175

@@ -317,6 +327,7 @@ async def test_get_items_with_optional_params(
317327
self.assertEqual(len(items), 1)
318328

319329

330+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
320331
class TestAsyncAmazonCreatorsApiSearchItems(unittest.IsolatedAsyncioTestCase):
321332
"""Tests for search_items() method."""
322333

@@ -425,6 +436,7 @@ async def test_search_items_without_keywords(
425436
self.assertIn("browseNodeId", str(call_args))
426437

427438

439+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
428440
class TestAsyncAmazonCreatorsApiErrorHandling(unittest.IsolatedAsyncioTestCase):
429441
"""Tests for error handling."""
430442

@@ -525,6 +537,7 @@ async def test_handles_invalid_associate_error(
525537
await api.get_items(["B0DLFMFBJW"])
526538

527539

540+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
528541
class TestAsyncAmazonCreatorsApiThrottling(unittest.IsolatedAsyncioTestCase):
529542
"""Tests for throttling mechanism."""
530543

@@ -570,6 +583,7 @@ async def test_throttling_waits_between_requests(
570583
self.assertTrue(mock_sleep.called)
571584

572585

586+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
573587
class TestAsyncAmazonCreatorsApiGetVariations(unittest.IsolatedAsyncioTestCase):
574588
"""Tests for get_variations() method."""
575589

@@ -721,6 +735,7 @@ async def test_get_variations_not_found(
721735
await api.get_variations("B0DLFMFBJV")
722736

723737

738+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
724739
class TestAsyncAmazonCreatorsApiGetBrowseNodes(unittest.IsolatedAsyncioTestCase):
725740
"""Tests for get_browse_nodes() method."""
726741

tests/amazon_creatorsapi/aio/auth_test.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44
import unittest
55
from unittest.mock import AsyncMock, MagicMock, patch
66

7-
import httpx
7+
try:
8+
import httpx
89

9-
from amazon_creatorsapi.aio.auth import (
10-
GRANT_TYPE,
11-
SCOPE,
12-
TOKEN_EXPIRATION_BUFFER,
13-
VERSION_ENDPOINTS,
14-
AsyncOAuth2TokenManager,
15-
)
16-
from amazon_creatorsapi.errors import AuthenticationError
10+
from amazon_creatorsapi.aio.auth import (
11+
GRANT_TYPE,
12+
SCOPE,
13+
TOKEN_EXPIRATION_BUFFER,
14+
VERSION_ENDPOINTS,
15+
AsyncOAuth2TokenManager,
16+
)
17+
from amazon_creatorsapi.errors import AuthenticationError
1718

19+
HTTPX_AVAILABLE = True
20+
except ImportError:
21+
HTTPX_AVAILABLE = False
1822

23+
24+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
1925
class TestAsyncOAuth2TokenManagerInit(unittest.TestCase):
2026
"""Tests for AsyncOAuth2TokenManager initialization."""
2127

@@ -76,6 +82,7 @@ def test_with_invalid_version(self) -> None:
7682
self.assertIn("Unsupported version", str(context.exception))
7783

7884

85+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
7986
class TestAsyncOAuth2TokenManagerIsTokenValid(unittest.TestCase):
8087
"""Tests for is_token_valid() method."""
8188

@@ -102,6 +109,7 @@ def test_returns_true_when_token_valid(self) -> None:
102109
self.assertTrue(manager.is_token_valid())
103110

104111

112+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
105113
class TestAsyncOAuth2TokenManagerClearToken(unittest.TestCase):
106114
"""Tests for clear_token() method."""
107115

@@ -117,6 +125,7 @@ def test_clears_token_and_expiration(self) -> None:
117125
self.assertIsNone(manager._expires_at)
118126

119127

128+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
120129
class TestAsyncOAuth2TokenManagerGetToken(unittest.IsolatedAsyncioTestCase):
121130
"""Tests for get_token() method."""
122131

@@ -188,6 +197,7 @@ async def test_refreshes_token_when_expired(
188197
self.assertIsNotNone(manager._expires_at)
189198

190199

200+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
191201
class TestAsyncOAuth2TokenManagerRefreshToken(unittest.IsolatedAsyncioTestCase):
192202
"""Tests for refresh_token() method."""
193203

tests/amazon_creatorsapi/aio/client_test.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
import unittest
66
from unittest.mock import AsyncMock, MagicMock, patch
77

8-
from amazon_creatorsapi.aio.client import (
9-
DEFAULT_HOST,
10-
DEFAULT_TIMEOUT,
11-
AsyncHttpClient,
12-
AsyncHttpResponse,
13-
)
8+
try:
9+
from amazon_creatorsapi.aio.client import (
10+
DEFAULT_HOST,
11+
DEFAULT_TIMEOUT,
12+
AsyncHttpClient,
13+
AsyncHttpResponse,
14+
)
15+
16+
HTTPX_AVAILABLE = True
17+
except ImportError:
18+
HTTPX_AVAILABLE = False
1419

1520

21+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
1622
class TestAsyncHttpClient(unittest.IsolatedAsyncioTestCase):
1723
"""Tests for AsyncHttpClient."""
1824

@@ -112,6 +118,7 @@ async def test_post_with_context_manager(self, mock_client_cls: MagicMock) -> No
112118
mock_client_instance.post.assert_called_once()
113119

114120

121+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
115122
class TestAsyncHttpResponse(unittest.TestCase):
116123
"""Tests for AsyncHttpResponse."""
117124

@@ -123,6 +130,7 @@ def test_json_parsing(self) -> None:
123130
self.assertEqual(response.json(), {"foo": "bar"})
124131

125132

133+
@unittest.skipIf(not HTTPX_AVAILABLE, "httpx not installed")
126134
class TestAsyncModuleInit(unittest.TestCase):
127135
"""Test async module initialization logic."""
128136

tests/amazon_creatorsapi/aio/integration_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111

1212
from dotenv import load_dotenv
1313

14-
from amazon_creatorsapi.aio import AsyncAmazonCreatorsApi
15-
from amazon_creatorsapi.errors import ItemsNotFoundError
14+
try:
15+
from amazon_creatorsapi.aio import AsyncAmazonCreatorsApi
16+
from amazon_creatorsapi.errors import ItemsNotFoundError
17+
18+
HTTPX_AVAILABLE = True
19+
except ImportError:
20+
HTTPX_AVAILABLE = False
1621

1722
if TYPE_CHECKING:
1823
from creatorsapi_python_sdk.models.browse_node import BrowseNode
@@ -180,7 +185,7 @@ async def _run_api_setup() -> dict[str, object]:
180185

181186
# Module-level cache - run setup once when module is loaded (only if credentials exist)
182187
_cached_data: dict[str, object] = {}
183-
if has_api_credentials():
188+
if has_api_credentials() and HTTPX_AVAILABLE:
184189
_cached_data = asyncio.run(_run_api_setup())
185190

186191

0 commit comments

Comments
 (0)