added random_company_sample endpoint
This commit is contained in:
@@ -40,6 +40,7 @@ urlpatterns = [
|
|||||||
path('api/v1/create_company_user/', core_views.create_company_user, name='create-company-user'),
|
path('api/v1/create_company_user/', core_views.create_company_user, name='create-company-user'),
|
||||||
path('api/v1/my_user/', core_views.my_user, name='my-user'),
|
path('api/v1/my_user/', core_views.my_user, name='my-user'),
|
||||||
path('api/v1/my_company/', company_views.my_company , name='my-company'),
|
path('api/v1/my_company/', company_views.my_company , name='my-company'),
|
||||||
|
path('api/v1/companies/sample/', company_views.random_company_sample , name='company-sample'),
|
||||||
path('api/v1/my_products/', product_views.my_products, name='my-products'),
|
path('api/v1/my_products/', product_views.my_products, name='my-products'),
|
||||||
path('api/v1/stats/me/', stat_views.track_user, name='user-tracker'),
|
path('api/v1/stats/me/', stat_views.track_user, name='user-tracker'),
|
||||||
path('api/v1/autocomplete/category-tag/', product_views.CategoryTagAutocomplete.as_view(), name='category-autocomplete'),
|
path('api/v1/autocomplete/category-tag/', product_views.CategoryTagAutocomplete.as_view(), name='category-autocomplete'),
|
||||||
|
|||||||
@@ -357,3 +357,53 @@ class MyCompanyViewTest(APITestCase):
|
|||||||
|
|
||||||
# check response
|
# check response
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
|
|
||||||
|
class RandomCompanySampleTest(APITestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""Tests setup
|
||||||
|
"""
|
||||||
|
self.endpoint = '/api/v1/companies/sample/'
|
||||||
|
self.factory = CompanyFactory
|
||||||
|
self.model = Company
|
||||||
|
# create user
|
||||||
|
self.email = f"user@mail.com"
|
||||||
|
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
|
||||||
|
self.user = CustomUserFactory(email=self.email, is_active=True)
|
||||||
|
self.user.set_password(self.password)
|
||||||
|
self.user.save()
|
||||||
|
|
||||||
|
def test_default_behavior(self):
|
||||||
|
"""Expect 6 instances as default
|
||||||
|
"""
|
||||||
|
# create instances
|
||||||
|
instances = [ self.factory() for i in range(20)]
|
||||||
|
|
||||||
|
# Query endpoint
|
||||||
|
response = self.client.get(self.endpoint)
|
||||||
|
payload = response.json()
|
||||||
|
|
||||||
|
# Assert forbidden code
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEquals(6, len(payload))
|
||||||
|
# test IDs not correlative (eventually it could be, because it's random)
|
||||||
|
self.assertTrue(payload[0]['id'] != (payload[1]['id'] + 1))
|
||||||
|
|
||||||
|
def test_custom_size_behavior(self):
|
||||||
|
"""Expect response size equal to parameter value
|
||||||
|
"""
|
||||||
|
# create instances
|
||||||
|
instances = [ self.factory() for i in range(20)]
|
||||||
|
|
||||||
|
# Query endpoint
|
||||||
|
size = 10
|
||||||
|
url = f"{self.endpoint}?size={size}"
|
||||||
|
response = self.client.get(url)
|
||||||
|
payload = response.json()
|
||||||
|
|
||||||
|
# Assert forbidden code
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEquals(size, len(payload))
|
||||||
|
# test IDs not correlative (eventually it could be, because it's random)
|
||||||
|
self.assertTrue(payload[0]['id'] != (payload[1]['id'] + 1))
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from django.template.loader import render_to_string
|
|||||||
from django.contrib.gis.geoip2 import GeoIP2
|
from django.contrib.gis.geoip2 import GeoIP2
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated
|
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated
|
||||||
from rest_framework.decorators import api_view, permission_classes, action
|
from rest_framework.decorators import api_view, permission_classes, action
|
||||||
@@ -172,3 +172,21 @@ def my_company(request):
|
|||||||
limit = int(limit)
|
limit = int(limit)
|
||||||
data = data[:limit]
|
data = data[:limit]
|
||||||
return Response(data=data)
|
return Response(data=data)
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['GET',])
|
||||||
|
@permission_classes([IsAuthenticatedOrReadOnly,])
|
||||||
|
def random_company_sample(request):
|
||||||
|
"""Return an slice of a randomized list of companies
|
||||||
|
|
||||||
|
Takes optional parameter size (int)
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
size = request.GET.get('size', 6)
|
||||||
|
size = int(size)
|
||||||
|
except:
|
||||||
|
return Response({"error": "Wrong value for size, must be a number"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
|
||||||
|
queryset = Company.objects.all().order_by('?')[:size]
|
||||||
|
serializer = CompanySerializer(queryset, many=True)
|
||||||
|
return Response(serializer.data)
|
||||||
|
|||||||
Reference in New Issue
Block a user