implemented pagination for my_products and my_company
This commit is contained in:
@@ -33,8 +33,6 @@ class CompanyViewSetTest(APITestCase):
|
|||||||
def test_anon_user_cannot_create_instance(self):
|
def test_anon_user_cannot_create_instance(self):
|
||||||
"""Not logged-in user cannot create new instance
|
"""Not logged-in user cannot create new instance
|
||||||
"""
|
"""
|
||||||
instances = [self.factory() for n in range(random.randint(1,5))]
|
|
||||||
|
|
||||||
# Query endpoint
|
# Query endpoint
|
||||||
response = self.client.post(self.endpoint, data={})
|
response = self.client.post(self.endpoint, data={})
|
||||||
# Assert access is forbidden
|
# Assert access is forbidden
|
||||||
@@ -76,6 +74,20 @@ class CompanyViewSetTest(APITestCase):
|
|||||||
# Assert access is forbidden
|
# Assert access is forbidden
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def test_anon_user_can_paginate_instances(self):
|
||||||
|
"""Not logged-in user can paginate instances
|
||||||
|
"""
|
||||||
|
instances = [self.factory() for n in range(12)]
|
||||||
|
# Request list
|
||||||
|
url = f"{self.endpoint}?limit=5&offset=10"
|
||||||
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
# Assert access is allowed
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
# assert only 2 instances in response
|
||||||
|
payload = response.json()
|
||||||
|
self.assertEquals(2, len(payload['results']))
|
||||||
|
|
||||||
def test_anon_user_can_filter_tags(self):
|
def test_anon_user_can_filter_tags(self):
|
||||||
# create instances
|
# create instances
|
||||||
expected_instance = [
|
expected_instance = [
|
||||||
@@ -299,6 +311,27 @@ class MyCompanyViewTest(APITestCase):
|
|||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEquals(len(user_instances), len(payload))
|
self.assertEquals(len(user_instances), len(payload))
|
||||||
|
|
||||||
|
def test_auth_user_can_paginate_instances(self):
|
||||||
|
"""authenticated user can paginate instances
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Authenticate
|
||||||
|
token = get_tokens_for_user(self.user)
|
||||||
|
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||||
|
|
||||||
|
# create instances
|
||||||
|
instances = [self.factory(creator=self.user) for n in range(12)]
|
||||||
|
|
||||||
|
# Request list
|
||||||
|
url = f"{self.endpoint}?limit=5&offset=10"
|
||||||
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
# Assert access is allowed
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
# assert only 2 instances in response
|
||||||
|
payload = response.json()
|
||||||
|
self.assertEquals(2, len(payload))
|
||||||
|
|
||||||
def test_anon_user_cannot_access(self):
|
def test_anon_user_cannot_access(self):
|
||||||
# send in request
|
# send in request
|
||||||
response = self.client.get(self.endpoint)
|
response = self.client.get(self.endpoint)
|
||||||
|
|||||||
@@ -158,6 +158,17 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
|||||||
@api_view(['GET',])
|
@api_view(['GET',])
|
||||||
@permission_classes([IsAuthenticated,])
|
@permission_classes([IsAuthenticated,])
|
||||||
def my_company(request):
|
def my_company(request):
|
||||||
qs = Company.objects.filter(creator=request.user)
|
limit = request.GET.get('limit')
|
||||||
company_serializer = CompanySerializer(qs, many=True)
|
offset = request.GET.get('offset')
|
||||||
return Response(data=company_serializer.data)
|
qs = Company.objects.filter(creator=request.user)
|
||||||
|
company_serializer = CompanySerializer(qs, many=True)
|
||||||
|
data = company_serializer.data
|
||||||
|
# RESULTS PAGINATION
|
||||||
|
if limit is not None and offset is not None:
|
||||||
|
limit = int(limit)
|
||||||
|
offset = int(offset)
|
||||||
|
data = data[offset:(limit+offset)]
|
||||||
|
elif limit is not None:
|
||||||
|
limit = int(limit)
|
||||||
|
data = data[:limit]
|
||||||
|
return Response(data=data)
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ class ProductViewSetTest(APITestCase):
|
|||||||
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
|
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
|
||||||
self.user = CustomUserFactory(email="test@mail.com", password=self.password, is_active=True)
|
self.user = CustomUserFactory(email="test@mail.com", password=self.password, is_active=True)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.model.objects.all().delete()
|
||||||
|
|
||||||
# anon user
|
# anon user
|
||||||
def test_anon_user_cannot_create_instance(self):
|
def test_anon_user_cannot_create_instance(self):
|
||||||
"""Not logged-in user cannot create new instance
|
"""Not logged-in user cannot create new instance
|
||||||
@@ -192,6 +195,26 @@ class ProductViewSetTest(APITestCase):
|
|||||||
self.assertEquals(len(expected_instance), len(payload))
|
self.assertEquals(len(expected_instance), len(payload))
|
||||||
|
|
||||||
# authenticated user
|
# authenticated user
|
||||||
|
def test_auth_user_can_paginate_instances(self):
|
||||||
|
"""authenticated user can paginate instances
|
||||||
|
"""
|
||||||
|
# Authenticate
|
||||||
|
token = get_tokens_for_user(self.user)
|
||||||
|
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||||
|
|
||||||
|
# create instances
|
||||||
|
instances = [self.factory(creator=self.user) for n in range(12)]
|
||||||
|
|
||||||
|
# Request list
|
||||||
|
url = f"{self.endpoint}?limit=5&offset=10"
|
||||||
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
# Assert access is allowed
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
# assert only 2 instances in response
|
||||||
|
payload = response.json()
|
||||||
|
self.assertEquals(2, len(payload['results']))
|
||||||
|
|
||||||
def test_auth_user_can_list_instances(self):
|
def test_auth_user_can_list_instances(self):
|
||||||
"""Regular logged-in user can list instance
|
"""Regular logged-in user can list instance
|
||||||
"""
|
"""
|
||||||
@@ -926,6 +949,26 @@ class MyProductsViewTest(APITestCase):
|
|||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEquals(len(user_instances), len(payload))
|
self.assertEquals(len(user_instances), len(payload))
|
||||||
|
|
||||||
|
def test_auth_user_can_paginate_instances(self):
|
||||||
|
"""authenticated user can paginate instances
|
||||||
|
"""
|
||||||
|
# Authenticate
|
||||||
|
token = get_tokens_for_user(self.user)
|
||||||
|
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||||
|
|
||||||
|
# create instances
|
||||||
|
instances = [self.factory(creator=self.user) for n in range(12)]
|
||||||
|
|
||||||
|
# Request list
|
||||||
|
url = f"{self.endpoint}?limit=5&offset=10"
|
||||||
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
# Assert access is allowed
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
# assert only 2 instances in response
|
||||||
|
payload = response.json()
|
||||||
|
self.assertEquals(2, len(payload))
|
||||||
|
|
||||||
def test_anon_user_cannot_access(self):
|
def test_anon_user_cannot_access(self):
|
||||||
# send in request
|
# send in request
|
||||||
response = self.client.get(self.endpoint)
|
response = self.client.get(self.endpoint)
|
||||||
|
|||||||
@@ -54,9 +54,20 @@ class ProductViewSet(viewsets.ModelViewSet):
|
|||||||
@api_view(['GET',])
|
@api_view(['GET',])
|
||||||
@permission_classes([IsAuthenticated,])
|
@permission_classes([IsAuthenticated,])
|
||||||
def my_products(request):
|
def my_products(request):
|
||||||
qs = Product.objects.filter(creator=request.user)
|
limit = request.GET.get('limit')
|
||||||
product_serializer = ProductSerializer(qs, many=True)
|
offset = request.GET.get('offset')
|
||||||
return Response(data=product_serializer.data)
|
qs = Product.objects.filter(creator=request.user)
|
||||||
|
product_serializer = ProductSerializer(qs, many=True)
|
||||||
|
data = product_serializer.data
|
||||||
|
# RESULTS PAGINATION
|
||||||
|
if limit is not None and offset is not None:
|
||||||
|
limit = int(limit)
|
||||||
|
offset = int(offset)
|
||||||
|
data = data[offset:(limit+offset)]
|
||||||
|
elif limit is not None:
|
||||||
|
limit = int(limit)
|
||||||
|
data = data[:limit]
|
||||||
|
return Response(data=data)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST',])
|
@api_view(['POST',])
|
||||||
|
|||||||
Reference in New Issue
Block a user