Skip to content

Commit

Permalink
AddedL comments API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Aug 2, 2023
1 parent 6644c94 commit 19f08b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
33 changes: 30 additions & 3 deletions api/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
from django.test import TestCase
from django import urls
from rest_framework import test

from recipes import models


# Create your tests here.
class ApiTestCase(TestCase):
class ApiTestCase(test.APITestCase):
fixtures = ["categories.json", "programs.json", "recipes.json", "comments.json"]
pass

def test_recipe_comments_get(self):
url = urls.reverse("api:recipe-comments-list", kwargs={"pk": 1})
response = self.client.get(url)

self.assertEqual(response.status_code, 200)

data = response.json()
self.assertIsInstance(data, dict)
self.assertEqual(len(data["results"]), 1)
self.assertEqual(data["results"][0]["text"], "Опубликованный комментарий")

def test_recipe_comments_post(self):
url = urls.reverse("api:recipe-comments-list", kwargs={"pk": 1})
count_before = models.Comment.objects.count()

response = self.client.post(url, {"text": "test"})

self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()["text"], "test")

self.assertEqual(models.Comment.objects.count(), count_before + 1)
self.assertEqual(
models.Comment.objects.get(text="test").state, models.Comment.STATE_NEW
)
2 changes: 1 addition & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
router.register("categories", views.CategoriesViewSet)
router.register("programs", views.ProgramsViewSet)


app_name = "api"
urlpatterns = [
path("", include(router.urls)),
path("recipes/", views.RecipesListView.as_view(), name="recipe-list"),
Expand Down
1 change: 1 addition & 0 deletions bread/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("recipes.urls")),
path("api/v1/", include("api.urls")),
]

if settings.DEBUG:
Expand Down
1 change: 0 additions & 1 deletion recipes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
views.CommentAddView.as_view(),
name="comment_add",
),
path("api/v1/", include("api.urls")),
]

if settings.DEBUG:
Expand Down

0 comments on commit 19f08b4

Please sign in to comment.