Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Test the search function for ingredient units

Roland Geider01307eda3adb
diff --git a/wger/nutrition/tests/ingredient.py b/wger/nutrition/tests/ingredient.py
index f1d45da03..7c7e9072e 100644
--- a/wger/nutrition/tests/ingredient.py
+++ b/wger/nutrition/tests/ingredient.py
@@ -171,3 +171,47 @@ class IngredientSearchTestCase(WorkoutManagerTestCase):
self.user_login('test')
self.search_ingredient()
+
+
+class IngredientUnitSearchTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the ingredient unit search functions
+ '''
+
+ def search_ingredient_unit(self, fail=True):
+ '''
+ Helper function
+ '''
+
+ # Search for an ingredient that has units
+ response = self.client.get(reverse('ingredient-get-units', kwargs={'pk': 1}))
+ if fail:
+ self.assertEqual(response.status_code, 302)
+ else:
+ self.assertEqual(response.status_code, 200)
+ result = json.loads(response.content)
+ self.assertEqual(len(result), 3)
+
+ # Search for an ingredient that has no units
+ response = self.client.get(reverse('ingredient-get-units', kwargs={'pk': 2}))
+ if fail:
+ self.assertEqual(response.status_code, 302)
+ else:
+ self.assertEqual(response.status_code, 200)
+ result = json.loads(response.content)
+ self.assertEqual(len(result), 0)
+
+ def test_search_ingredient_unit_anonymous(self):
+ '''
+ Test searching for an ingredient by an anonymous user
+ '''
+
+ self.search_ingredient_unit(fail=True)
+
+ def test_search_ingredient__unitlogged_in(self):
+ '''
+ Test searching for an ingredient by a logged in user
+ '''
+
+ self.user_login('test')
+ self.search_ingredient_unit(fail=False)
diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.py
index 448c4d1bf..4fbe96b8f 100644
--- a/wger/nutrition/views/ingredient.py
+++ b/wger/nutrition/views/ingredient.py
@@ -305,6 +305,7 @@ def ingredient_search(request):
context_instance=RequestContext(request))
+@login_required
def ajax_get_ingredient_units(request, pk):
'''
Fetches the available ingredient units