add simple API test

This commit is contained in:
Petr Dlouhý 2022-09-23 19:52:35 +02:00
parent 79405d4a2d
commit a5a0975c3f
7 changed files with 75 additions and 2 deletions

Binary file not shown.

View file

View file

@ -0,0 +1,62 @@
# import get_user_model
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.urls import reverse
from model_bakery import baker
from rest_framework import status
from rest_framework.test import APITestCase
from categories.models import Category
User = get_user_model()
class MeViewTests(APITestCase):
def setUp(self):
self.user = User.objects.create_superuser("test-user", "foo@bar.baz", "test-password")
self.client.force_authenticate(user=self.user)
category = Category.tree.create(name="Foo category", slug="foo_category", active=True, order=0, parent=None)
baker.make(
"SimpleText",
primary_category=category,
)
cache.clear()
def test_list(self):
"""
Test list
"""
url = reverse("categories-list")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertListEqual(
response.json(),
[
{
"name": "Foo category",
"slug": "foo_category",
"active": True,
"thumbnail": None,
"thumbnail_width": None,
"thumbnail_height": None,
"order": 0,
"alternate_title": "",
"alternate_url": "",
"description": None,
"meta_keywords": "",
"meta_extra": "",
"children": [],
"flatpage_count": 0,
"flatpage_count_cumulative": 0,
"other_cats_count": 0,
"other_cats_count_cumulative": 0,
"more_cats_count": 0,
"more_cats_count_cumulative": 0,
"simpletext_count": 1,
"simpletext_count_cumulative": 1,
"simpletext_sec_cat_count": 0,
"simpletext_sec_cat_count_cumulative": 0,
}
],
)

8
example/rest_urls.py Normal file
View file

@ -0,0 +1,8 @@
from rest_framework.routers import DefaultRouter
from categories.api.urls import router as category_router
router = DefaultRouter()
router.registry.extend(category_router.registry)
urlpatterns = router.urls

View file

@ -28,4 +28,5 @@ urlpatterns = (
# {'document_root': ROOT_PATH + '/editor/media/editor/',
# 'show_indexes':True}),
re_path(r"^static/(?P<path>.*)$", serve, {"document_root": os.path.join(ROOT_PATH, "example", "static")}),
path("api/", include("example.rest_urls"), name="api"),
)

View file

@ -1,7 +1,9 @@
-r prod.txt
django<4.0.0
django
django-rest-framework
ghp-import
linkify-it-py
model-bakery
myst-parser
pydata-sphinx-theme
Sphinx>=4.3.0

View file

@ -32,7 +32,7 @@ deps=
pillow
ipdb
codecov
-r{toxinidir}/requirements.txt
-r{toxinidir}/requirements/test.txt
commands=
coverage erase