Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add simple testcase to test copying a nutritional plan

Roland Geiderf1da3576ef65
diff --git a/wger/nutrition/fixtures/test-nutrition-data.json b/wger/nutrition/fixtures/test-nutrition-data.json
index ba493f779..719346d70 100644
--- a/wger/nutrition/fixtures/test-nutrition-data.json
+++ b/wger/nutrition/fixtures/test-nutrition-data.json
@@ -1,6 +1,6 @@
[
{
- "pk": 9,
+ "pk": 5,
"model": "nutrition.nutritionplan",
"fields": {
"description": "Description 1",
@@ -10,27 +10,27 @@
}
},
{
- "pk": 8,
+ "pk": 4,
"model": "nutrition.nutritionplan",
"fields": {
"description": "My plan to do xyz",
- "user": 1,
+ "user": 2,
"language": 2,
"creation_date": "2012-11-12"
}
},
{
- "pk": 7,
+ "pk": 3,
"model": "nutrition.nutritionplan",
"fields": {
"description": "",
- "user": 2,
+ "user": 1,
"language": 2,
"creation_date": "2012-11-09"
}
},
{
- "pk": 6,
+ "pk": 2,
"model": "nutrition.nutritionplan",
"fields": {
"description": "",
@@ -40,7 +40,7 @@
}
},
{
- "pk": 5,
+ "pk": 1,
"model": "nutrition.nutritionplan",
"fields": {
"description": "",
@@ -54,7 +54,7 @@
"model": "nutrition.meal",
"fields": {
"time": null,
- "plan": 5,
+ "plan": 1,
"order": 1
}
},
@@ -63,7 +63,7 @@
"model": "nutrition.meal",
"fields": {
"time": "07:00:00",
- "plan": 8,
+ "plan": 4,
"order": 1
}
},
@@ -72,7 +72,7 @@
"model": "nutrition.meal",
"fields": {
"time": "08:00:00",
- "plan": 6,
+ "plan": 2,
"order": 1
}
},
@@ -81,7 +81,7 @@
"model": "nutrition.meal",
"fields": {
"time": "08:00:00",
- "plan": 7,
+ "plan": 3,
"order": 1
}
},
@@ -90,7 +90,7 @@
"model": "nutrition.meal",
"fields": {
"time": "08:00:00",
- "plan": 6,
+ "plan": 2,
"order": 1
}
},
@@ -99,7 +99,7 @@
"model": "nutrition.meal",
"fields": {
"time": "08:00:00",
- "plan": 9,
+ "plan": 5,
"order": 1
}
},
@@ -108,7 +108,7 @@
"model": "nutrition.meal",
"fields": {
"time": "08:02:00",
- "plan": 8,
+ "plan": 4,
"order": 1
}
},
@@ -117,7 +117,7 @@
"model": "nutrition.meal",
"fields": {
"time": "10:00:00",
- "plan": 6,
+ "plan": 2,
"order": 1
}
},
@@ -126,7 +126,7 @@
"model": "nutrition.meal",
"fields": {
"time": "10:00:00",
- "plan": 8,
+ "plan": 4,
"order": 1
}
},
@@ -135,7 +135,7 @@
"model": "nutrition.meal",
"fields": {
"time": "19:02:00",
- "plan": 8,
+ "plan": 4,
"order": 1
}
},
@@ -144,7 +144,7 @@
"model": "nutrition.meal",
"fields": {
"time": "22:00:00",
- "plan": 8,
+ "plan": 4,
"order": 1
}
},
diff --git a/wger/nutrition/tests/__init__.py b/wger/nutrition/tests/__init__.py
index b39e86d0e..b5c57c6ab 100644
--- a/wger/nutrition/tests/__init__.py
+++ b/wger/nutrition/tests/__init__.py
@@ -1,4 +1,6 @@
-from wger.nutrition.tests.ingredient import *
from wger.nutrition.tests.plan import *
-from wger.nutrition.tests.meal import *
+from wger.nutrition.tests.copy_plan import *
+
+from wger.nutrition.tests.ingredient import *
from wger.nutrition.tests.ingredient_overview import *
+from wger.nutrition.tests.meal import *
diff --git a/wger/nutrition/tests/copy_plan.py b/wger/nutrition/tests/copy_plan.py
new file mode 100644
index 000000000..ba9b8f685
--- /dev/null
+++ b/wger/nutrition/tests/copy_plan.py
@@ -0,0 +1,80 @@
+# This file is part of Workout Manager.
+#
+# Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+from django.core.urlresolvers import reverse
+
+from wger.nutrition.models import NutritionPlan
+
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+
+
+class CopyPlanTestCase(WorkoutManagerTestCase):
+ '''
+ Tests copying a nutritional plan
+ '''
+
+ def copy_plan(self, fail=False):
+ '''
+ Helper function to test copying nutrition plans
+ '''
+
+ # Open the copy nutritional plan form
+ response = self.client.get(reverse('nutrition-copy', kwargs={'pk': 4}))
+ if fail:
+ self.assertIn(response.status_code, (404, 302))
+ else:
+ self.assertEqual(response.status_code, 302)
+
+ # Copy the plan
+ count_before = NutritionPlan.objects.count()
+ response = self.client.post(reverse('nutrition-copy', kwargs={'pk': 4}),
+ {'comment': 'A copied plan'})
+ count_after = NutritionPlan.objects.count()
+
+ if fail:
+ self.assertEqual(count_before, count_after)
+ else:
+ self.assertGreater(count_after, count_before)
+ self.assertEqual(count_after, 7)
+
+ # Test accessing the copied workout
+ response = self.client.get(reverse('wger.nutrition.views.view', kwargs={'id': 4}))
+
+ if fail:
+ self.assertIn(response.status_code, (404, 302))
+ else:
+ self.assertEqual(response.status_code, 200)
+
+ def test_copy_plan_anonymous(self):
+ '''
+ Test copying a nutritional plan as an anonymous user
+ '''
+
+ self.copy_plan(fail=True)
+
+ def test_copy_plan_owner(self):
+ '''
+ Test copying a nutritional plan as the owner user
+ '''
+
+ self.user_login('test')
+ self.copy_plan(fail=False)
+
+ def test_copy_plan_other(self):
+ '''
+ Test copying a nutritional plan as a logged in user not owning the plan
+ '''
+
+ self.user_login('admin')
+ self.copy_plan(fail=True)
diff --git a/wger/nutrition/tests/meal.py b/wger/nutrition/tests/meal.py
index da831794d..f31ac4ae2 100644
--- a/wger/nutrition/tests/meal.py
+++ b/wger/nutrition/tests/meal.py
@@ -41,6 +41,8 @@ class AddMealTestCase(WorkoutManagerAddTestCase):
'''
object_class = Meal
- url = reverse('meal-add', kwargs={'plan_pk': 8})
+ url = reverse('meal-add', kwargs={'plan_pk': 4})
pk = 12
data = {'time': datetime.time(9, 2)}
+ user_success = 'test'
+ user_fail = 'admin'
diff --git a/wger/nutrition/tests/plan.py b/wger/nutrition/tests/plan.py
index 1eaf1aed4..87607daf3 100644
--- a/wger/nutrition/tests/plan.py
+++ b/wger/nutrition/tests/plan.py
@@ -27,7 +27,7 @@ class DeletePlanTestCase(WorkoutManagerDeleteTestCase):
delete_class = NutritionPlan
delete_url = 'nutrition-delete'
- pk = 6
+ pk = 2
class EditPlanTestCase(WorkoutManagerEditTestCase):
@@ -37,5 +37,5 @@ class EditPlanTestCase(WorkoutManagerEditTestCase):
object_class = NutritionPlan
edit_url = 'nutrition-edit'
- pk = 6
+ pk = 2
data_update = {'description': 'My new description'}