Merge pull request 'Document store list query params for swagger; add filter test coverage' (#3) from feature/filter-stores into master

Reviewed-on: #3
This commit is contained in:
Ghasemi 2026-08-19 07:05:21 -04:00
commit 2b3fd0fc28
5 changed files with 93 additions and 1 deletions

11
CHANGELOG.md Normal file
View file

@ -0,0 +1,11 @@
# Changelog
## [Unreleased]
### Fixed
- `GET /api/v1/stores/` — documented the `category`, `neighborhood`, `search`, `lat`, and `lng` query params for drf-spectacular so they render in swagger. The filtering itself already worked; only the OpenAPI schema was missing them (`apps/stores/views.py`).
- `GET /api/v1/products/` — same issue: documented the existing `store`, `category`, and `search` query params for swagger (`apps/catalog/views.py`).
### Added
- Test coverage for `StoreViewSet` list filtering (`apps/stores/tests/test_store_list.py`) — previously untested.
- Test coverage for `ProductViewSet` list filtering (`apps/catalog/tests/test_product_list.py`) — previously untested.

View file

@ -0,0 +1,37 @@
from apps.catalog.models import ProductCategory
from apps.orders.tests.base import OrdersTestCase
class ProductListFilterTests(OrdersTestCase):
def test_filter_by_store(self):
response = self.client.get('/api/v1/products/', {'store': str(self.store1.uuid)})
self.assertEqual(response.status_code, 200)
uuids = {product['uuid'] for product in response.data['results']}
self.assertEqual(uuids, {str(self.product1.uuid)})
def test_filter_by_category(self):
other_category = ProductCategory.objects.create(name='نوشیدنی')
self.product2.category = other_category
self.product2.save(update_fields=['category'])
response = self.client.get('/api/v1/products/', {'category': str(self.product1.category.uuid)})
self.assertEqual(response.status_code, 200)
uuids = {product['uuid'] for product in response.data['results']}
self.assertEqual(uuids, {str(self.product1.uuid)})
def test_filter_by_search(self):
response = self.client.get('/api/v1/products/', {'search': 'کاله'})
self.assertEqual(response.status_code, 200)
uuids = {product['uuid'] for product in response.data['results']}
self.assertEqual(uuids, {str(self.product1.uuid)})
def test_no_filter_returns_all_products(self):
response = self.client.get('/api/v1/products/')
self.assertEqual(response.status_code, 200)
uuids = {product['uuid'] for product in response.data['results']}
self.assertEqual(uuids, {str(self.product1.uuid), str(self.product2.uuid)})

View file

@ -1,4 +1,5 @@
from django.db.models import Q
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view
from rest_framework import mixins, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny, IsAuthenticated
@ -26,6 +27,15 @@ class ProductCategoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
queryset = ProductCategory.objects.all()
@extend_schema_view(
list=extend_schema(
parameters=[
OpenApiParameter('store', str, description='Filter by store UUID.'),
OpenApiParameter('category', str, description='Filter by product category UUID.'),
OpenApiParameter('search', str, description='Search by product name/description.'),
],
),
)
class ProductViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
"""Customer-facing product browsing (store page, product detail, search)."""

View file

@ -0,0 +1,23 @@
from apps.stores.models import StoreCategory
from apps.orders.tests.base import OrdersTestCase
class StoreListFilterTests(OrdersTestCase):
def test_filter_by_category(self):
other_category = StoreCategory.objects.create(name='رستوران')
self.store2.category = other_category
self.store2.save(update_fields=['category'])
response = self.client.get('/api/v1/stores/', {'category': str(self.store1.category.uuid)})
self.assertEqual(response.status_code, 200)
uuids = {store['uuid'] for store in response.data['results']}
self.assertEqual(uuids, {str(self.store1.uuid)})
def test_no_category_filter_returns_all_stores(self):
response = self.client.get('/api/v1/stores/')
self.assertEqual(response.status_code, 200)
uuids = {store['uuid'] for store in response.data['results']}
self.assertEqual(uuids, {str(self.store1.uuid), str(self.store2.uuid)})

View file

@ -3,7 +3,7 @@ from django.contrib.gis.geos import Point
from django.contrib.gis.measure import D
from django.db.models import Q
from django.shortcuts import get_object_or_404
from drf_spectacular.utils import extend_schema
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view
from rest_framework import mixins, status, viewsets
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
@ -30,6 +30,17 @@ class StoreCategoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
queryset = StoreCategory.objects.all()
@extend_schema_view(
list=extend_schema(
parameters=[
OpenApiParameter('category', str, description='Filter by store category UUID.'),
OpenApiParameter('neighborhood', str, description='Filter by service neighborhood UUID.'),
OpenApiParameter('search', str, description='Search by store name/description.'),
OpenApiParameter('lat', float, description='Latitude; used with `lng` to sort by distance (15km radius).'),
OpenApiParameter('lng', float, description='Longitude; used with `lat` to sort by distance (15km radius).'),
],
),
)
class StoreViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
"""Customer-facing store browsing (home feed, store page)."""