-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZDL-47: Add categories model API endpoints
- Loading branch information
1 parent
6223dc9
commit e2b8b0f
Showing
21 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.contrib import admin | ||
|
||
from categories.models import Category | ||
|
||
admin.site.register(Category) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CategoryConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "categories" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 3.2.12 on 2022-08-08 16:29 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Category", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("name", models.TextField(unique=True)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.db import models | ||
|
||
|
||
class Category(models.Model): | ||
name = models.TextField(unique=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import serializers | ||
|
||
from categories.models import Category | ||
|
||
|
||
class CategorySerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Category | ||
fields = "__all__" |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import factory | ||
from factory.django import DjangoModelFactory | ||
|
||
from categories.models import Category | ||
|
||
|
||
class CategoryFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Category | ||
|
||
name = factory.Sequence(lambda n: f"Category {n}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import pytest | ||
from django.test.client import MULTIPART_CONTENT | ||
|
||
from categories.models import Category | ||
from categories.tests.factories.category_factory import CategoryFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_all_categories(logged_in_client): | ||
""" | ||
Test list all products categories with empty database | ||
""" | ||
response = logged_in_client.get("/v1/categories/") | ||
|
||
assert response.status_code == 200, response.json() | ||
assert len(response.json()["results"]) == 0 | ||
assert response.json()["results"] == [] | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_all_product_categories(logged_in_client): | ||
""" | ||
Test list all product categories | ||
""" | ||
CategoryFactory() | ||
response = logged_in_client.get("/v1/categories/") | ||
|
||
response_data = response.json()["results"] | ||
|
||
assert response.status_code == 200, response_data | ||
assert len(response_data) == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_create_product_category(admin_client): | ||
""" | ||
Test admin create product category | ||
""" | ||
category_data = {"name": "test-category-create"} | ||
response = admin_client.post("/v1/categories/", category_data, format="json") | ||
assert response.status_code == 201 and response.json() | ||
assert Category.objects.count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_retrieve_product_category(logged_in_client): | ||
""" | ||
Test retrieve specific product category | ||
""" | ||
category = CategoryFactory() | ||
response = logged_in_client.get(f"/v1/categories/{category.id}/") | ||
|
||
response_data = response.json() | ||
|
||
assert response.status_code == 200 and response_data | ||
assert response_data["id"] == category.id | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_update_product_category(admin_client): | ||
""" | ||
Test update a single product category | ||
""" | ||
content_type = MULTIPART_CONTENT | ||
category = CategoryFactory() | ||
data = { | ||
"name": "Category 1 edited", | ||
} | ||
data = admin_client._encode_json({} if not data else data, content_type) | ||
encoded_data = admin_client._encode_data(data, content_type) | ||
response = admin_client.generic( | ||
"PUT", | ||
f"/v1/categories/{category.id}/", | ||
encoded_data, | ||
content_type=content_type, | ||
secure=False, | ||
enctype="multipart/form-data", | ||
) | ||
|
||
response_data = response.json() | ||
assert response.status_code == 200, response.data | ||
assert data["name"] == response_data["name"] | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_patch_product_category(admin_client): | ||
""" | ||
Test patch a single product property | ||
""" | ||
content_type = MULTIPART_CONTENT | ||
category = CategoryFactory() | ||
data = { | ||
"name": "Category 1 patch edited", | ||
} | ||
data = admin_client._encode_json({} if not data else data, content_type) | ||
encoded_data = admin_client._encode_data(data, content_type) | ||
response = admin_client.generic( | ||
"PATCH", | ||
f"/v1/categories/{category.id}/", | ||
encoded_data, | ||
content_type=content_type, | ||
secure=False, | ||
enctype="multipart/form-data", | ||
) | ||
|
||
response_data = response.json() | ||
assert response.status_code == 200, response.data | ||
assert data["name"] == response_data["name"] | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete_product_category(admin_client): | ||
""" | ||
Test delete specific product category | ||
""" | ||
category = CategoryFactory() | ||
response = admin_client.delete(f"/v1/categories/{category.id}/") | ||
assert response.status_code == 204 | ||
assert Category.objects.count() == 0 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_non_safe_permission_product_category_should_raise_403(logged_in_client): | ||
""" | ||
Test non safe method permission access on product category | ||
""" | ||
category = CategoryFactory() | ||
response = logged_in_client.delete(f"/v1/categories/{category.id}/") | ||
assert response.status_code == 403 and response.json() == { | ||
"detail": "You do not have permission to perform this action." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import include, path | ||
from rest_framework import routers | ||
|
||
from categories.views import CategoryViewSet | ||
|
||
router = routers.DefaultRouter() | ||
router.register("", CategoryViewSet) | ||
urlpatterns = [path("", include(router.urls))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from rest_framework.viewsets import ModelViewSet | ||
|
||
from authentication.permissions import AdminAccessPermission | ||
from categories.models import Category | ||
from categories.serializers import CategorySerializer | ||
|
||
|
||
class CategoryViewSet(ModelViewSet): | ||
permission_classes = [AdminAccessPermission] | ||
queryset = Category.objects.all().order_by("id") | ||
serializer_class = CategorySerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 3.2.12 on 2022-08-08 16:29 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("categories", "0001_initial"), | ||
("products", "0005_alter_product_price"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="product", | ||
name="category", | ||
field=models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.SET_NULL, | ||
to="categories.category", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ | |
"corsheaders", | ||
"drf_yasg", | ||
"products", | ||
"categories", | ||
"orders", | ||
"authentication", | ||
"rest_framework", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters