Skip to content

Commit

Permalink
fix: making import of ninja optional
Browse files Browse the repository at this point in the history
  • Loading branch information
maticardenas committed Jun 17, 2024
1 parent a2d6691 commit 48d708b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
20 changes: 15 additions & 5 deletions openapi_tester/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
from __future__ import annotations

import http
import logging
from typing import TYPE_CHECKING

# pylint: disable=import-error
from ninja import NinjaAPI, Router
from ninja.testing import TestClient
try:
from ninja import NinjaAPI, Router
from ninja.testing import TestClient
except ImportError:
NinjaAPI = Router = TestClient = object
logging.info("Django-Ninja is not installed.")


from rest_framework.test import APIClient

from .exceptions import APIFrameworkNotInstalledError
from .response_handler_factory import ResponseHandlerFactory
from .schema_tester import SchemaTester
from .utils import serialize_json
Expand Down Expand Up @@ -129,8 +136,11 @@ def __init__(
schema_tester: SchemaTester | None = None,
**kwargs,
) -> None:
"""Initialize ``OpenAPIClient`` instance."""
super().__init__(*args, router_or_app=router_or_app, **kwargs)
"""Initialize ``OpenAPINinjaClient`` instance."""
if not isinstance(object, TestClient):
super().__init__(*args, router_or_app=router_or_app, **kwargs)
else:
raise APIFrameworkNotInstalledError("Django-Ninja is not installed.")
self.schema_tester = schema_tester or self._schema_tester_factory()
self._ninja_path_prefix = path_prefix

Expand Down
8 changes: 8 additions & 0 deletions openapi_tester/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ class UndocumentedSchemaSectionError(OpenAPISchemaError):
"""

pass


class APIFrameworkNotInstalledError(Exception):
"""
Raised when a required API framework is not installed.
"""

pass
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
from rest_framework.response import Response

import openapi_tester
from openapi_tester.response_handler import GenericRequest
from tests.schema_converter import SchemaToPythonConverter
from tests.utils import TEST_ROOT
Expand Down Expand Up @@ -98,3 +99,11 @@ def response(
return response

return response


@pytest.fixture
def ninja_not_installed():
former_client = openapi_tester.clients.TestClient
openapi_tester.clients.TestClient = object
yield
openapi_tester.clients.TestClient = former_client
15 changes: 14 additions & 1 deletion tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from rest_framework import status

from openapi_tester.clients import OpenAPIClient
from openapi_tester.exceptions import DocumentationError, UndocumentedSchemaSectionError
from openapi_tester.exceptions import (
APIFrameworkNotInstalledError,
DocumentationError,
UndocumentedSchemaSectionError,
)
from openapi_tester.schema_tester import SchemaTester

if TYPE_CHECKING:
Expand Down Expand Up @@ -162,3 +166,12 @@ class DummyTestCase(SimpleTestCase):
test_case._pre_setup()

assert isinstance(test_case.client, OpenAPIClient)


def test_ninja_not_installed(ninja_not_installed):
from openapi_tester.clients import OpenAPIClient, OpenAPINinjaClient

OpenAPIClient()

with pytest.raises(APIFrameworkNotInstalledError):
OpenAPINinjaClient(router_or_app=None)

0 comments on commit 48d708b

Please sign in to comment.