From f13f3dc2bf16f8fe40d6bb10b89846ce8c8bb503 Mon Sep 17 00:00:00 2001 From: Pilhwan Kim Date: Tue, 28 Apr 2020 12:41:51 +0900 Subject: [PATCH] add use drf tests --- hooks/post_gen_project.py | 2 ++ .../users/tests/test_drf_urls.py | 24 +++++++++++++++ .../users/tests/test_drf_views.py | 30 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_urls.py create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_views.py diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index ad1db3ee..21d680e0 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -299,6 +299,8 @@ def remove_aws_dockerfile(): def remove_drf_starter_files(): os.remove(os.path.join("config", "api_router.py")) shutil.rmtree(os.path.join("{{cookiecutter.project_slug}}", "users", "api")) + os.remove(os.path.join("{{cookiecutter.project_slug}}", "users", "tests", "test_drf_urls.py")) + os.remove(os.path.join("{{cookiecutter.project_slug}}", "users", "tests", "test_drf_views.py")) def remove_storages_module(): diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_urls.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_urls.py new file mode 100644 index 00000000..83b623f4 --- /dev/null +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_urls.py @@ -0,0 +1,24 @@ +import pytest +from django.urls import resolve, reverse + +from {{ cookiecutter.project_slug }}.users.models import User + +pytestmark = pytest.mark.django_db + + +def test_user_detail(user: User): + assert ( + reverse("api:user-detail", kwargs={"username": user.username}) + == f"/api/users/{user.username}/" + ) + assert resolve(f"/api/users/{user.username}/").view_name == "api:user-detail" + + +def test_user_list(): + assert reverse("api:user-list") == "/api/users/" + assert resolve("/api/users/").view_name == "api:user-list" + + +def test_user_me(): + assert reverse("api:user-me") == "/api/users/me/" + assert resolve("/api/users/me/").view_name == "api:user-me" diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_views.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_views.py new file mode 100644 index 00000000..89c3c1a9 --- /dev/null +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_views.py @@ -0,0 +1,30 @@ +import pytest +from django.test import RequestFactory + +from {{ cookiecutter.project_slug }}.users.models import User +from {{ cookiecutter.project_slug }}.users.api.views import UserViewSet +from {{ cookiecutter.project_slug }}.users.api.serializers import UserSerializer + +pytestmark = pytest.mark.django_db + + +class TestUserViewSet: + def test_get_queryset(self, user: User, rf: RequestFactory): + view = UserViewSet() + request = rf.get("/fake-url/") + request.user = user + + view.request = request + + assert user in view.get_queryset() + + def test_me(self, user: User, rf: RequestFactory): + view = UserViewSet() + request = rf.get("/fake-url/") + request.user = user + + view.request = request + + response = view.me(request) + + assert response.data == UserSerializer(user, context={"request": request}).data