Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add more tests for the ingredient model
Roland Geider0ca8ff5e32e2
diff --git a/wger/manager/tests/testcase.py b/wger/manager/tests/testcase.pyindex c1acd1bf0..30e3644c9 100644--- a/wger/manager/tests/testcase.py+++ b/wger/manager/tests/testcase.py@@ -29,6 +29,7 @@ class WorkoutManagerTestCase(TestCase):'test-weight-data','test-exercises','tests-ingredients',+ 'test-nutrition-data','days_of_week','tests-workout-data')@@ -83,8 +84,8 @@ class WorkoutManagerDeleteTestCase(WorkoutManagerTestCase):count_before = self.delete_class.objects.count()response = self.client.get(reverse(self.delete_url, kwargs={'pk': self.pk}))count_after = self.delete_class.objects.count()-self.assertEqual(count_before, count_after)+if fail:self.assertIn(response.status_code, (403, 302))else:@@ -126,3 +127,155 @@ class WorkoutManagerDeleteTestCase(WorkoutManagerTestCase):self.user_login(self.user_fail)self.delete_object(fail=True)+++class WorkoutManagerEditTestCase(WorkoutManagerTestCase):+ '''+ Tests editing an object as an authorized user, a different one and a logged out+ one.+ '''++ object_class = ''+ edit_url = ''+ pk = None+ user_success = 'admin'+ user_fail = 'test'+ data_update = {}++ def edit_object(self, fail=False):+ '''+ Helper function to test editing an object+ '''++ # Only perform the checks on derived classes+ if self.__class__.__name__ == 'WorkoutManagerEditTestCase':+ return++ # Fetch the edit page+ response = self.client.get(reverse(self.edit_url, kwargs={'pk': self.pk}))+ entry_before = self.object_class.objects.get(pk=self.pk)++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ self.assertEqual(response.status_code, 200)++ # Try to edit the object+ response = self.client.post(reverse(self.edit_url, kwargs={'pk': self.pk}),+ self.data_update)++ entry_after = self.object_class.objects.get(pk=1)++ # Check the results+ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')+ self.assertEqual(entry_before, entry_after)++ else:+ self.assertEqual(response.status_code, 302)+ for i in self.data_update:+ self.assertEqual(getattr(entry_after, i), self.data_update[i])++ def test_edit_object_anonymous(self):+ '''+ Tests editing the object as an anonymous user+ '''++ self.edit_object(fail=True)++ def test_edit_object_authorized(self):+ '''+ Tests editing the object as an authorized user+ '''++ self.user_login(self.user_success)+ self.edit_object(fail=False)++ def test_edit_object_other(self):+ '''+ Tests editing the object as an unauthorized, logged in user+ '''++ self.user_login(self.user_fail)+ self.edit_object(fail=True)+++class WorkoutManagerAddTestCase(WorkoutManagerTestCase):+ '''+ Tests adding an object as an authorized user, a different one and a logged out+ one.+ '''++ object_class = ''+ url = ''+ pk = None+ user_success = 'admin'+ user_fail = 'test'+ data = {}++ def add_object(self, fail=False):+ '''+ Helper function to test adding an object+ '''++ # Only perform the checks on derived classes+ if self.__class__.__name__ == 'WorkoutManagerAddTestCase':+ return++ # Fetch the add page+ response = self.client.get(reverse(self.url))++ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ else:+ self.assertEqual(response.status_code, 200)++ # Enter the data+ count_before = self.object_class.objects.count()+ response = self.client.post(reverse(self.url), self.data)+ count_after = self.object_class.objects.count()+ if fail:+ self.assertIn(response.status_code, (302, 403))+ self.assertTemplateUsed('login.html')++ self.assertRaises(self.object_class.DoesNotExist,+ self.object_class.objects.get,+ pk=self.pk)+ self.assertEqual(count_before, count_after)++ else:+ self.assertEqual(response.status_code, 302)+ entry = self.object_class.objects.get(pk=self.pk)+ for i in self.data:+ self.assertEqual(getattr(entry, i), self.data[i])++ self.assertEqual(count_before + 1, count_after)+++ def test_add_object_anonymous(self):+ '''+ Tests adding the object as an anonymous user+ '''++ self.add_object(fail=True)++ def test_add_object_authorized(self):+ '''+ Tests adding the object as an authorized user+ '''++ self.user_login(self.user_success)+ self.add_object(fail=False)++ def test_add_object_other(self):+ '''+ Tests adding the object as an unauthorized, logged in user+ '''++ self.user_login(self.user_fail)+ self.add_object(fail=True)diff --git a/wger/nutrition/fixtures/test-nutrition-data.json b/wger/nutrition/fixtures/test-nutrition-data.jsonnew file mode 100644index 000000000..c24447b1c--- /dev/null+++ b/wger/nutrition/fixtures/test-nutrition-data.json@@ -0,0 +1,331 @@+[+ {+ "pk": 9,+ "model": "nutrition.nutritionplan",+ "fields": {+ "description": "",+ "user": 1,+ "language": 2,+ "creation_date": "2012-11-19"+ }+ },+ {+ "pk": 8,+ "model": "nutrition.nutritionplan",+ "fields": {+ "description": "",+ "user": 1,+ "language": 2,+ "creation_date": "2012-11-12"+ }+ },+ {+ "pk": 7,+ "model": "nutrition.nutritionplan",+ "fields": {+ "description": "",+ "user": 2,+ "language": 2,+ "creation_date": "2012-11-09"+ }+ },+ {+ "pk": 6,+ "model": "nutrition.nutritionplan",+ "fields": {+ "description": "",+ "user": 1,+ "language": 2,+ "creation_date": "2012-10-29"+ }+ },+ {+ "pk": 5,+ "model": "nutrition.nutritionplan",+ "fields": {+ "description": "",+ "user": 2,+ "language": 2,+ "creation_date": "2012-09-22"+ }+ },+ {+ "pk": 13,+ "model": "nutrition.meal",+ "fields": {+ "time": null,+ "plan": 5,+ "order": 1+ }+ },+ {+ "pk": 20,+ "model": "nutrition.meal",+ "fields": {+ "time": "07:00:00",+ "plan": 8,+ "order": 1+ }+ },+ {+ "pk": 14,+ "model": "nutrition.meal",+ "fields": {+ "time": "08:00:00",+ "plan": 6,+ "order": 1+ }+ },+ {+ "pk": 17,+ "model": "nutrition.meal",+ "fields": {+ "time": "08:00:00",+ "plan": 7,+ "order": 1+ }+ },+ {+ "pk": 18,+ "model": "nutrition.meal",+ "fields": {+ "time": "08:00:00",+ "plan": 6,+ "order": 1+ }+ },+ {+ "pk": 24,+ "model": "nutrition.meal",+ "fields": {+ "time": "08:00:00",+ "plan": 9,+ "order": 1+ }+ },+ {+ "pk": 19,+ "model": "nutrition.meal",+ "fields": {+ "time": "08:02:00",+ "plan": 8,+ "order": 1+ }+ },+ {+ "pk": 15,+ "model": "nutrition.meal",+ "fields": {+ "time": "10:00:00",+ "plan": 6,+ "order": 1+ }+ },+ {+ "pk": 21,+ "model": "nutrition.meal",+ "fields": {+ "time": "10:00:00",+ "plan": 8,+ "order": 1+ }+ },+ {+ "pk": 22,+ "model": "nutrition.meal",+ "fields": {+ "time": "19:02:00",+ "plan": 8,+ "order": 1+ }+ },+ {+ "pk": 23,+ "model": "nutrition.meal",+ "fields": {+ "time": "22:00:00",+ "plan": 8,+ "order": 1+ }+ },+ {+ "pk": 3,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 1,+ "amount_gramm": 200,+ "meal": 13,+ "order": 1+ }+ },+ {+ "pk": 4,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 4,+ "amount_gramm": 300,+ "meal": 14,+ "order": 1+ }+ },+ {+ "pk": 5,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 2,+ "amount_gramm": 100,+ "meal": 15,+ "order": 1+ }+ },+ {+ "pk": 6,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 5,+ "amount_gramm": 100,+ "meal": 15,+ "order": 1+ }+ },+ {+ "pk": 7,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 3,+ "amount_gramm": 300,+ "meal": 19,+ "order": 1+ }+ },+ {+ "pk": 8,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 1,+ "amount_gramm": 101,+ "meal": 21,+ "order": 1+ }+ },+ {+ "pk": 9,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 2,+ "amount_gramm": 100,+ "meal": 21,+ "order": 1+ }+ },+ {+ "pk": 10,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 3,+ "amount_gramm": 100,+ "meal": 20,+ "order": 1+ }+ },+ {+ "pk": 11,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 4,+ "amount_gramm": 200,+ "meal": 22,+ "order": 1+ }+ },+ {+ "pk": 13,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 3,+ "amount_gramm": 100,+ "meal": 19,+ "order": 1+ }+ },+ {+ "pk": 14,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 6,+ "amount_gramm": 30,+ "meal": 19,+ "order": 1+ }+ },+ {+ "pk": 15,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 3,+ "amount_gramm": 150,+ "meal": 22,+ "order": 1+ }+ },+ {+ "pk": 16,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 1,+ "amount_gramm": 500,+ "meal": 23,+ "order": 1+ }+ },+ {+ "pk": 17,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 6,+ "amount_gramm": 100,+ "meal": 24,+ "order": 1+ }+ },+ {+ "pk": 18,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 4,+ "amount_gramm": 100,+ "meal": 24,+ "order": 1+ }+ },+ {+ "pk": 19,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 3,+ "amount_gramm": 100,+ "meal": 24,+ "order": 1+ }+ },+ {+ "pk": 20,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 2,+ "amount_gramm": 100,+ "meal": 24,+ "order": 1+ }+ },+ {+ "pk": 21,+ "model": "nutrition.mealitem",+ "fields": {+ "ingredient": 1,+ "amount_gramm": 100,+ "meal": 24,+ "order": 1+ }+ }+]diff --git a/wger/nutrition/fixtures/tests-ingredients.json b/wger/nutrition/fixtures/tests-ingredients.jsonindex 2f8bfadab..6e09b44ce 100644--- a/wger/nutrition/fixtures/tests-ingredients.json+++ b/wger/nutrition/fixtures/tests-ingredients.json@@ -30,5 +30,69 @@"protein": 25.63,"carbohydrates": 0.0}+ },+ {+ "pk": 3,+ "model": "nutrition.ingredient",+ "fields": {+ "name": "Bachelor chow, now with flavour!",+ "language": 2,+ "sodium": 0.1,+ "energy": 300,+ "fat": 9,+ "carbohydrates_sugar": 1,+ "fat_saturated": 3,+ "fibres": 5.0,+ "protein": 25.63,+ "carbohydrates": 0.0+ }+ },+ {+ "pk": 4,+ "model": "nutrition.ingredient",+ "fields": {+ "name": "Slurm",+ "language": 2,+ "sodium": 0.1,+ "energy": 500,+ "fat": 1,+ "carbohydrates_sugar": 99,+ "fat_saturated": 0,+ "fibres": 0,+ "protein": 0,+ "carbohydrates": 99.0+ }+ },+ {+ "pk": 5,+ "model": "nutrition.ingredient",+ "fields": {+ "name": "Another tasty ingredient",+ "language": 2,+ "sodium": 2.1,+ "energy": 10,+ "fat": 1,+ "carbohydrates_sugar": 4,+ "fat_saturated": 0,+ "fibres": 0,+ "protein": 0,+ "carbohydrates": 5.0+ }+ },+ {+ "pk": 6,+ "model": "nutrition.ingredient",+ "fields": {+ "name": "Raw ingredient",+ "language": 2,+ "sodium": 2.1,+ "energy": 180,+ "fat": 2,+ "carbohydrates_sugar": 4,+ "fat_saturated": 0,+ "fibres": 20,+ "protein": 10,+ "carbohydrates": 5.0+ }}]diff --git a/wger/nutrition/tests/ingredient.py b/wger/nutrition/tests/ingredient.pyindex 907bb1170..03541136c 100644--- a/wger/nutrition/tests/ingredient.py+++ b/wger/nutrition/tests/ingredient.py@@ -11,15 +11,13 @@# GNU General Public License for more details.## You should have received a copy of the GNU Affero General Public License--import datetime--from django.core.urlresolvers import reverse+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.from wger.nutrition.models import Ingredient-from wger.manager.tests.testcase import WorkoutManagerTestCasefrom wger.manager.tests.testcase import WorkoutManagerDeleteTestCase+from wger.manager.tests.testcase import WorkoutManagerEditTestCase+from wger.manager.tests.testcase import WorkoutManagerAddTestCaseclass DeleteIngredientTestCase(WorkoutManagerDeleteTestCase):@@ -30,5 +28,41 @@ class DeleteIngredientTestCase(WorkoutManagerDeleteTestCase):delete_class = Ingredientdelete_url = 'ingredient-delete'pk = 1- user_success = 'admin'- user_fail = 'test'+++class EditIngredientTestCase(WorkoutManagerEditTestCase):+ '''+ Tests editing an ingredient+ '''++ object_class = Ingredient+ edit_url = 'ingredient-edit'+ pk = 1+ data_update = {'name': 'A new name',+ 'sodium': 2,+ 'energy': 200,+ 'fat': 10,+ 'carbohydrates_sugar': 5,+ 'fat_saturated': 3.1415,+ 'fibres': 2.1,+ 'protein': 30,+ 'carbohydrates': 10}+++class AddIngredientTestCase(WorkoutManagerAddTestCase):+ '''+ Tests adding an ingredient+ '''++ object_class = Ingredient+ url = 'ingredient-add'+ pk = 7+ data = {'name': 'A new ingredient',+ 'sodium': 2,+ 'energy': 200,+ 'fat': 10,+ 'carbohydrates_sugar': 5,+ 'fat_saturated': 3.1415,+ 'fibres': 2.1,+ 'protein': 30,+ 'carbohydrates': 10}