Skip to content

Commit

Permalink
finished test improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
enzofrnt committed Dec 8, 2024
1 parent 5339e9e commit d098e67
Show file tree
Hide file tree
Showing 12 changed files with 1,697 additions and 7 deletions.
4 changes: 1 addition & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.pytestArgs": [
"-v",
"-s",
"./hybridrouter/tests/"
"tests"
]
}
1 change: 1 addition & 0 deletions hybridrouter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .hybridrouter import HybridRouter, TreeNode
1,647 changes: 1,647 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

File renamed without changes.
4 changes: 2 additions & 2 deletions hybridrouter/tests/conftest.py → tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def pytest_configure():
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"hybridrouter.tests",
"tests",
],
REST_FRAMEWORK={
"DEFAULT_RENDERER_CLASSES": ("rest_framework.renderers.JSONRenderer",),
Expand All @@ -110,6 +110,6 @@ def pytest_configure():

@pytest.fixture
def hybrid_router():
from hybridrouter.hybridrouter import HybridRouter
from hybridrouter import HybridRouter

return HybridRouter()
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import types
from unittest.mock import MagicMock

import pytest
from django.test import override_settings
from django.urls import include, path, reverse
from django.urls.resolvers import get_resolver
from rest_framework import status
from rest_framework.routers import DefaultRouter
from rest_framework.test import APIClient
from rest_framework.test import APIClient, APIRequestFactory

from hybridrouter import TreeNode

from .conftest import recevoir_test_url_resolver
from .models import Item
from .views import ItemView, item_view
from .viewsets import ItemViewSet, SlugItemViewSet, EmptyViewSet
from .viewsets import EmptyViewSet, ItemViewSet, SlugItemViewSet


def create_urlconf(router):
Expand Down Expand Up @@ -616,3 +619,44 @@ def test_get_viewset_urls_with_empty_mapping(hybrid_router, db):
urls = hybrid_router.get_urls()

assert len(urls) == 0


def test_get_api_root_view_with_no_children(hybrid_router):
# Créer un TreeNode sans enfants
empty_node = TreeNode()

# Appeler la méthode _get_api_root_view avec ce node
result = hybrid_router._get_api_root_view(empty_node, prefix="")

# Vérifier que la méthode retourne None
assert (
result is None
), "La méthode _get_api_root_view devrait retourner None lorsqu'il n'y a pas d'enfants"


def test_namespace_in_get_api_root_view(hybrid_router):
# Création d'un TreeNode avec un enfant
root_node = TreeNode()
child_node = TreeNode(name="child")
child_node.basename = "child-api"
root_node.children["child"] = child_node

# Instancier la vue APIRoot
api_root_view = hybrid_router._get_api_root_view(root_node, "")

# Créer une fausse requête avec un namespace
factory = APIRequestFactory()
request = factory.get("/") # Cette requête aura method='GET'

# Mocker le resolver_match
resolver_match = MagicMock()
resolver_match.namespace = "testnamespace"
request.resolver_match = resolver_match

# Appeler la méthode get() de APIRoot
response = api_root_view(request)

# Vérifier le contenu de la réponse
expected_data = {"child": "http://testserver/child/"}
assert response.status_code == 200
assert response.data == expected_data
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d098e67

Please sign in to comment.