Add food api test

This commit is contained in:
Michael Götz 2018-02-03 11:52:44 +01:00
parent 831991518f
commit d300ead723
2 changed files with 91 additions and 13 deletions

View File

@ -112,6 +112,63 @@ class FoodViewSetV1_1(viewsets.ModelViewSet, ):
return queryset
# @api_view(['GET'])
@permission_classes((AllowAny,))
class FoodViewSetV1_1_Defaults(viewsets.ModelViewSet, ):
"""
API endpoint that allows users to be viewed or edited.
"""
# queryset = Menu.objects.all()
serializer_class = MenuSerializer
def get_queryset(self):
queryset = Menu.objects.all()
location = None
if 'location' in self.kwargs:
location = self.kwargs['location']
year = None
if 'year' in self.kwargs:
year = self.kwargs['year']
month = None
if 'month' in self.kwargs:
month = self.kwargs['month']
day = None
if 'day' in self.kwargs:
day = self.kwargs['day']
if location:
if str(location).upper() == Menu.ERBA.upper():
queryset = queryset.filter(location__contains=Menu.ERBA)
elif str(location).upper() == Menu.FEKI.upper():
queryset = queryset.filter(location__contains=Menu.FEKI)
elif str(location).upper() == Menu.AUSTRASSE.upper():
print("Before: " + str(queryset))
queryset = queryset.filter(location__contains=Menu.AUSTRASSE)
elif str(location).upper() == Menu.MARKUSPLATZ.upper():
queryset = queryset.filter(location__contains=Menu.MARKUSPLATZ)
print(queryset)
if year and month and day:
date = '%s-%s-%s' % (year, month, day)
queryset = queryset.filter(date=datetime.strptime(date, '%Y-%m-%d'))
# if date == "week":
# today = datetime.now()
# weekday = today.weekday()
# monday = today - timedelta(weekday)
# sunday = today + (timedelta(6 - weekday))
# print("Monday: " + str(monday))
# print("Sunday: " + str(sunday))
# queryset = queryset.filter(date__gte=monday, date__lte=sunday)
# else:
# queryset = queryset.filter(date=datetime.strptime(date, "%Y-%m-%d"))
print("LOCATION: %s" % str(location))
print(str(queryset))
return queryset
# @api_view(['GET'])
@permission_classes((AllowAny,))
class HappyHourViewSet(viewsets.ModelViewSet):

View File

@ -21,10 +21,10 @@ class SingleFood_Scope(TestCase):
self.singlefood_4 = SingleFood.objects.create(name="testfood4")
self.singlefood_5 = SingleFood.objects.create(name="testfood5")
self.singlefood_6 = SingleFood.objects.create(name="testfood6")
self.menu_1 = Menu.objects.create(date='2017-01-15', location=Menu.ERBA)
self.menu_1.menu.add(self.singlefood_1)
self.menu_1.menu.add(self.singlefood_2)
self.menu_1.menu.add(self.singlefood_3)
self.today_menu = Menu.objects.create(date='2017-01-15', location=Menu.ERBA)
self.today_menu.menu.add(self.singlefood_1)
self.today_menu.menu.add(self.singlefood_2)
self.today_menu.menu.add(self.singlefood_3)
self.menu_2 = Menu.objects.create(date='2017-01-10', location=Menu.FEKI)
self.menu_2.menu.add(self.singlefood_4)
self.menu_2.menu.add(self.singlefood_5)
@ -32,7 +32,7 @@ class SingleFood_Scope(TestCase):
def test_get_food_root(self):
"""
Right response on /food/api/v1.1/food
All menus in response
"""
menus = Menu.objects.all()
serializer = MenuSerializer(menus, many=True)
@ -42,21 +42,21 @@ class SingleFood_Scope(TestCase):
def test_get_food_date_param(self):
"""
Right response on /food/api/v1.1/food/2017/02/15/
Menu for a given date is in the response
"""
serializer = MenuSerializer(self.menu_2, many=False)
response_1 = self.client.get(reverse('api-v1_1-food-date', kwargs={'year': '2017', 'month': '01', 'day': '10'}))
self.assertEqual(response_1.status_code, 200)
self.assertEqual(response_1.data, [serializer.data])
serializer_2 = MenuSerializer(self.menu_1, many=False)
serializer_2 = MenuSerializer(self.today_menu, many=False)
response_2 = self.client.get(reverse('api-v1_1-food-date', kwargs={'year': '2017', 'month': '01', 'day': '15'}))
self.assertEqual(response_2.status_code, 200)
self.assertEqual(response_2.data, [serializer_2.data])
def test_get_food_date_param_not_in_set(self):
"""
Right response on /food/api/v1.1/food/2017/02/15/
Empty response if no object with request date in set
"""
response_1 = self.client.get(reverse('api-v1_1-food-date', kwargs={'year': '2017', 'month': '01', 'day': '11'}))
self.assertEqual(response_1.status_code, 200)
@ -64,9 +64,9 @@ class SingleFood_Scope(TestCase):
def test_get_food_location_param(self):
"""
Right response on /food/api/v1.1/food/2017/02/15/
Menu for a given location is in the response
"""
serializer_1 = MenuSerializer(self.menu_1, many=False)
serializer_1 = MenuSerializer(self.today_menu, many=False)
response_1 = self.client.get(reverse('api-v1_1-food-location', kwargs={'location': Menu.ERBA}))
self.assertEqual(response_1.status_code, 200)
self.assertEqual(response_1.data, [serializer_1.data])
@ -78,7 +78,7 @@ class SingleFood_Scope(TestCase):
def test_get_food_location_param_not_in_set(self):
"""
Right response on /food/api/v1.1/food/2017/02/15/
Empty response if no object with request location in set
"""
response_1 = self.client.get(reverse('api-v1_1-food-location', kwargs={'location': Menu.AUSTRASSE}))
self.assertEqual(response_1.status_code, 200)
@ -86,7 +86,7 @@ class SingleFood_Scope(TestCase):
def test_get_food_location_date_param(self):
"""
Right response on /food/api/v1.1/food/2017/02/15/
Menu for request location and date is in the response
"""
serializer = MenuSerializer(self.menu_2, many=False)
@ -98,7 +98,7 @@ class SingleFood_Scope(TestCase):
def test_get_food_location_date_param_not_in_set(self):
"""
Right response on /food/api/v1.1/food/2017/02/15/
Empty response if no object with request date and location in set
"""
response_1 = self.client.get(reverse('api-v1_1-food-location-date',
kwargs={'year': '2017', 'month': '01', 'day': '11',
@ -111,3 +111,24 @@ class SingleFood_Scope(TestCase):
'location': Menu.AUSTRASSE}))
self.assertEqual(response_1.status_code, 200)
self.assertEqual(response_1.data, [])
def test_get_food_today_param(self):
"""
Menu for request today is in the response
"""
self.singlefood = SingleFood.objects.create(name="testfood")
self.today_menu = Menu.objects.create(date=datetime.today().strftime('%Y-%m-%d'), location=Menu.ERBA)
self.today_menu.menu.add(self.singlefood)
serializer = MenuSerializer(self.today_menu, many=False)
response = self.client.get(reverse('api-v1_1-food-today'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [serializer.data])
def test_get_food_today_param_not_in_set(self):
"""
Empty response if no object with date 'today' is in set
"""
response = self.client.get(reverse('api-v1_1-food-today'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [])