Self hosted FLOSS fitness/workout, nutrition and weight tracker
Improve the coverage of exercise tests
Roland Geider4a100b1560f0
diff --git a/.hgignore b/.hgignoreindex f4c856607..d3418539a 100644--- a/.hgignore+++ b/.hgignore@@ -4,3 +4,4 @@ syntax: globsettings.py*.pyc*~+htmlcov/*\ No newline at end of filediff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.jsonindex 181c9730b..cfc99df9b 100644--- a/wger/exercises/fixtures/test-exercises.json+++ b/wger/exercises/fixtures/test-exercises.json@@ -80,6 +80,9 @@1,2],+ "muscles_secondary": [+ 3+ ],"description": "","name": "An exercise"}@@ -91,6 +94,10 @@"category": 2,"muscles": [2+ ],+ "muscles_secondary": [+ 1,+ 3],"description": "","name": "A very cool exercise"diff --git a/wger/exercises/tests/__init__.py b/wger/exercises/tests/__init__.pyindex a33e05ebc..61c0312c1 100644--- a/wger/exercises/tests/__init__.py+++ b/wger/exercises/tests/__init__.py@@ -1,2 +1,3 @@from wger.exercises.tests.categories import *from wger.exercises.tests.exercise import *+from wger.exercises.tests.exercise_comments import *diff --git a/wger/exercises/tests/exercise.py b/wger/exercises/tests/exercise.pyindex 5e5f2f01b..3e4b95ee1 100644--- a/wger/exercises/tests/exercise.py+++ b/wger/exercises/tests/exercise.py@@ -12,13 +12,7 @@## You should have received a copy of the GNU Affero General Public License--"""-This file demonstrates writing tests using the unittest module. These will pass-when you run "manage.py test".--Replace this with more appropriate tests for your application.-"""+import jsonfrom django.test import TestCasefrom django.core.urlresolvers import reverse@@ -32,7 +26,9 @@ from wger.manager.tests.testcase import WorkoutManagerTestCaseclass ExerciseIndexTestCase(WorkoutManagerTestCase):def test_exercise_index(self):- """Tests the exercise overview page"""+ '''+ Tests the exercise overview page+ '''response = self.client.get(reverse('wger.exercises.views.exercise_overview'))@@ -86,71 +82,7 @@ class ExerciseIndexTestCase(WorkoutManagerTestCase):# Ensure that non-existent exercises throw a 404.response = self.client.get(reverse('wger.exercises.views.exercise_view', kwargs={'id': 42}))self.assertEqual(response.status_code, 404)---class ExercisecommentsTestCase(WorkoutManagerTestCase):- def exercisecomment_fail(self):- """Tests the exercise comments (fails because of permissions)"""-- # Load the exercise- exercise_1 = Exercise.objects.get(pk=1)- comments = exercise_1.exercisecomment_set.all()- self.assertEqual(len(comments), 1)-- # Post a comment- response = self.client.post(reverse('wger.exercises.views.exercise_view', kwargs={'id': 1}),- {'comment': 'a new cool comment'})- self.assertEqual(response.status_code, 200)-- comments = exercise_1.exercisecomment_set.all()- self.assertEqual(len(comments), 1)--- def test_exercisecomment_no_authorized(self):- """Tests the exercise comments"""-- self.user_login('test')- self.exercisecomment_fail()- self.user_logout()-- def test_exercisecomment_not_logged_in(self):- """Tests the exercise comments"""-- self.user_logout()- self.exercisecomment_fail()--- def test_exercisecomment_authorized(self):- """Tests the exercise comments"""-- self.user_login()-- # Load the exercise- exercise_1 = Exercise.objects.get(pk=1)-- # Comments are loaded- comments = exercise_1.exercisecomment_set.all()- comment_1 = comments[0]- self.assertEqual(comment_1.id, 1)- self.assertEqual(comment_1.comment, "test 123")- self.assertEqual(len(comments), 1)-- # Post a comment- response = self.client.post(reverse('exercisecomment-add', kwargs={'exercise_pk': 1}),- {'comment': 'a new cool comment'})- comments = exercise_1.exercisecomment_set.all()-- self.assertEqual(response.status_code, 302)- self.assertEqual(len(comments), 2)-- # Post an empty comment and check it doesn't get added- response = self.client.post(reverse('exercisecomment-add', kwargs={'exercise_pk': 1}),- {'comment': ''})- comments = exercise_1.exercisecomment_set.all()-- self.assertEqual(len(comments), 2)- self.user_logout()class ExercisesTestCase(WorkoutManagerTestCase):"""Exercise test case"""@@ -257,10 +189,22 @@ class ExercisesTestCase(WorkoutManagerTestCase):# Delete the exercisecount_before = Exercise.objects.count()++ # GET request, doesn't delete anything+ response = self.client.get(reverse('exercise-delete', kwargs={'pk': 3}))+ count_after = Exercise.objects.count()+ self.assertEqual(count_before, count_after, 'Exercise was deleted')++ # Check the deletion+ if fail:+ self.assertEqual(response.status_code, 302)+ else:+ self.assertEqual(response.status_code, 200)++ # POST request, does delete the exerciseresponse = self.client.post(reverse('exercise-delete', kwargs={'pk': 3}))count_after = Exercise.objects.count()- # There is a redirectself.assertEqual(response.status_code, 302)# Check the deletion@@ -270,13 +214,14 @@ class ExercisesTestCase(WorkoutManagerTestCase):self.assertTrue(count_before > count_after, 'Exercise was not deleted')- def test_delete_exercise_anonymous(self):++ def test_delete_exercise_anonymous(self, fail=True):"""Test deleting an exercise by an anonymous user"""self.delete_exercise()- def test_delete_exercise_unauthorized(self):+ def test_delete_exercise_unauthorized(self, fail=True):"""Test deleting an exercise by an unauthorized user"""self.user_login('test')@@ -289,3 +234,48 @@ class ExercisesTestCase(WorkoutManagerTestCase):self.user_login()self.delete_exercise(fail=False)self.user_logout()+++ def search_exercise(self, fail=True):+ '''+ Helper function to test searching for exercises+ '''++ # Search for exercises (1 hit, "A very cool exercise")+ response = self.client.get(reverse('wger.exercises.views.exercise_search') + '?term=cool')+ self.assertEqual(response.status_code, 200)+ self.assertEqual(len(response.context['exercises']), 1)+ self.assertEqual(response.context['exercises'][0].name, 'A very cool exercise')+++ kwargs = {'HTTP_X_REQUESTED_WITH' : 'XMLHttpRequest'}++ # AJAX-Search for exercises (1 hit, "A very cool exercise")+ response = self.client.get(reverse('wger.exercises.views.exercise_search') + '?term=cool', **kwargs)+ self.assertEqual(response.status_code, 200)+ result = json.loads(response.content)+ self.assertEqual(len(result), 1)+ self.assertEqual(result[0]['value'], 'A very cool exercise')+++ def test_search_exercise_anonymous(self):+ """Test deleting an exercise by an anonymous user"""++ self.search_exercise()+++ def test_search_exercise_unauthorized(self):+ """Test deleting an exercise by an unauthorized user"""++ self.user_login('test')+ self.search_exercise()+ self.user_logout()++ def test_search_exercise_authorized(self):+ """Test deleting an exercise by an authorized user"""++ self.user_login()+ self.search_exercise(fail=False)+ self.user_logout()++diff --git a/wger/exercises/tests/exercise_comments.py b/wger/exercises/tests/exercise_comments.pynew file mode 100644index 000000000..1dde0ed6a--- /dev/null+++ b/wger/exercises/tests/exercise_comments.py@@ -0,0 +1,183 @@+# 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.test import TestCase+from django.core.urlresolvers import reverse++from wger.exercises.models import Exercise+from wger.exercises.models import ExerciseCategory++from wger.manager.tests.testcase import WorkoutManagerTestCase+++class ExercisecommentsTestCase(WorkoutManagerTestCase):++ def exercise_add_comment(self, fail=True):+ """Tests the exercise comments (fails because of permissions)"""++ # Load the exercise+ exercise_1 = Exercise.objects.get(pk=1)++ # Comments are loaded+ comments = exercise_1.exercisecomment_set.all()+ comment_1 = comments[0]+ self.assertEqual(comment_1.id, 1)+ self.assertEqual(comment_1.comment, "test 123")+ self.assertEqual(len(comments), 1)++ # Post a comment+ response = self.client.post(reverse('exercisecomment-add', kwargs={'exercise_pk': 1}),+ {'comment': 'a new cool comment'})+ comments = exercise_1.exercisecomment_set.all()++ self.assertEqual(response.status_code, 302)+ if fail:+ comments = exercise_1.exercisecomment_set.all()+ self.assertEqual(len(comments), 1)++ else:+ self.assertEqual(len(comments), 2)++ # Post an empty comment and check it doesn't get added+ response = self.client.post(reverse('exercisecomment-add', kwargs={'exercise_pk': 1}),+ {'comment': ''})+ comments = exercise_1.exercisecomment_set.all()++ if fail:+ self.assertEqual(len(comments), 1)+ else:+ self.assertEqual(response.status_code, 200)+ self.assertEqual(len(comments), 2)+++ def test_exercise_add_comment_no_authorized(self):+ """Tests the exercise comments"""++ self.user_login('test')+ self.exercise_add_comment(fail=True)+ self.user_logout()++ def test_exercise_add_comment_not_logged_in(self):+ """Tests the exercise comments"""++ self.exercise_add_comment(fail=True)+++ def test_exercise_add_comment_authorized(self):+ """Tests the exercise comments"""++ self.user_login()+ self.exercise_add_comment(fail=False)+++ def exercise_delete_comment(self, fail=True):+ '''+ Tests the deletion of exercise comments+ '''++ # Load the exercise+ exercise_1 = Exercise.objects.get(pk=1)++ # Comments are loaded+ comments = exercise_1.exercisecomment_set.all()+ comment_1 = comments[0]+ self.assertEqual(comment_1.id, 1)+ self.assertEqual(comment_1.comment, "test 123")+ self.assertEqual(len(comments), 1)++ # Delete the comment+ response = self.client.post(reverse('exercisecomment-delete', kwargs={'id': 1}))+ comments = exercise_1.exercisecomment_set.all()++ self.assertEqual(response.status_code, 302)+ if fail:+ comments = exercise_1.exercisecomment_set.all()+ self.assertEqual(len(comments), 1)++ else:+ self.assertEqual(len(comments), 0)+++ def test_exercise_delete_comment_no_authorized(self):+ """Tests the exercise comments"""++ self.user_login('test')+ self.exercise_delete_comment(fail=True)+ self.user_logout()++ def test_exercise_delete_comment_not_logged_in(self):+ """Tests the exercise comments"""++ self.exercise_delete_comment(fail=True)+++ def test_exercise_delete_comment_authorized(self):+ """Tests the exercise comments"""++ self.user_login()+ self.exercise_delete_comment(fail=False)+++ def exercise_edit_comment(self, fail=True):+ """Tests the exercise comments (fails because of permissions)"""++ # Load the exercise+ exercise_1 = Exercise.objects.get(pk=1)+ comment_before = 'test 123'+ comment_after = 'a new cool comment'++ # Comments are loaded+ comments = exercise_1.exercisecomment_set.all()+ comment_1 = comments[0]+ self.assertEqual(comment_1.id, 1)+ self.assertEqual(comment_1.comment, comment_before)+ self.assertEqual(len(comments), 1)++ # Open the edit page+ response = self.client.get(reverse('exercisecomment-edit', kwargs={'pk': 1}))+ if fail:+ self.assertEqual(response.status_code, 302)+ else:+ self.assertEqual(response.status_code, 200)++ # Edit the comment+ response = self.client.post(reverse('exercisecomment-edit', kwargs={'pk': 1}),+ {'comment': comment_after})+ comments = exercise_1.exercisecomment_set.all()+ comment_1 = comments[0]++ if fail:+ self.assertEqual(comment_1.comment, comment_before)+ else:+ self.assertEqual(comment_1.comment, comment_after)++++ def test_exercise_edit_comment_no_authorized(self):+ """Tests the exercise comments"""++ self.user_login('test')+ self.exercise_edit_comment(fail=True)++ def test_exercise_edit_comment_not_logged_in(self):+ """Tests the exercise comments"""++ self.exercise_edit_comment(fail=True)++ def test_exercise_edit_comment_authorized(self):+ """Tests the exercise comments"""++ self.user_login()+ self.exercise_edit_comment(fail=False)diff --git a/wger/exercises/urls.py b/wger/exercises/urls.pyindex 0695095ec..c36e240bf 100644--- a/wger/exercises/urls.py+++ b/wger/exercises/urls.py@@ -42,7 +42,9 @@ urlpatterns = patterns('wger.exercises.views',url(r'^comment/(?P<pk>\d+)/edit/$',permission_required('exercises.change_exercise')(ExerciseCommentEditView.as_view()),name='exercisecomment-edit'),- url(r'^comment/(?P<id>\d+)/delete/$', 'exercisecomment_delete'),+ url(r'^comment/(?P<id>\d+)/delete/$',+ 'exercisecomment_delete',+ name='exercisecomment-delete'),# Categoriesurl(r'^category/(?P<pk>\d+)/edit/$',