Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Rewrite the tests using the more compact base testcase

Roland Geider5b1a64026fa4
diff --git a/wger/exercises/tests/categories.py b/wger/exercises/tests/categories.py
index 7582f3c0b..d11c0aab9 100644
--- a/wger/exercises/tests/categories.py
+++ b/wger/exercises/tests/categories.py
@@ -18,6 +18,8 @@ from django.core.urlresolvers import reverse
from wger.exercises.models import ExerciseCategory
from wger.manager.tests.testcase import WorkoutManagerTestCase
from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
+from wger.manager.tests.testcase import WorkoutManagerEditTestCase
+from wger.manager.tests.testcase import WorkoutManagerAddTestCase
class DeleteExerciseCategoryTestCase(WorkoutManagerDeleteTestCase):
@@ -32,130 +34,23 @@ class DeleteExerciseCategoryTestCase(WorkoutManagerDeleteTestCase):
user_fail = 'test'
-class EditExerciseCategoryTestCase(WorkoutManagerTestCase):
+class EditExerciseCategoryTestCase(WorkoutManagerEditTestCase):
'''
- Exercise category test case
+ Tests editing an exercise category
'''
- def edit_category(self, fail=False):
- '''
- Helper function to test editing categories
- '''
-
- category = ExerciseCategory.objects.get(pk=3)
- old_name = category.name
-
- response = self.client.get(reverse('exercisecategory-edit', kwargs={'pk': 3}))
-
- # Did it work
- if fail:
- self.assertEqual(response.status_code, 302)
- else:
- self.assertEqual(response.status_code, 200)
-
- response = self.client.post(reverse('exercisecategory-edit',
- kwargs={'pk': 3}),
- {'name': 'A different name'})
-
- # There is a redirect
- self.assertEqual(response.status_code, 302)
-
- category = ExerciseCategory.objects.get(pk=3)
- new_name = category.name
-
- # Did it work
- if fail:
- self.assertEqual(old_name,
- new_name,
- 'Category was edited by unauthorzed user')
- else:
- self.assertTrue(old_name != new_name,
- 'Category wasnt deleted by unauthorzed user')
-
- # No name
- if not fail:
- response = self.client.post(reverse('exercisecategory-edit',
- kwargs={'pk': 3}),
- {'name': ''})
-
- self.assertTrue(response.context['form'].errors['name'])
-
- def test_edit_category_unauthorized(self):
- '''
- Test editing a category by an unauthorized user
- '''
-
- self.user_login('test')
- self.edit_category(fail=True)
-
- def test_edit_category_anonymous(self):
- '''
- Test editing a category by an anonymous user
- '''
-
- self.user_logout()
- self.edit_category(fail=True)
-
- def test_edit_category_authorized(self):
- '''
- Test editing a category by an authorized user
- '''
-
- self.user_login()
- self.edit_category()
+ object_class = ExerciseCategory
+ url = 'exercisecategory-edit'
+ pk = 3
+ data = {'name': 'A different name'}
-class AddExerciseCategoryTestCase(WorkoutManagerTestCase):
+class AddExerciseCategoryTestCase(WorkoutManagerAddTestCase):
'''
- Exercise category test case
+ Tests adding an exercise category
'''
- def add_category(self, fail=False):
- '''
- Helper function to test adding categories
- '''
-
- response = self.client.get(reverse('exercisecategory-add'))
-
- # Did it work?
- if fail:
- self.assertEqual(response.status_code, 302)
- else:
- self.assertEqual(response.status_code, 200)
-
- count_before = ExerciseCategory.objects.count()
- response = self.client.post(reverse('exercisecategory-add'),
- {'name': 'A new category'})
- count_after = ExerciseCategory.objects.count()
-
- # There is a redirect
- self.assertEqual(response.status_code, 302)
-
- # Did it work
- if fail:
- self.assertEqual(count_before, count_after)
- else:
- self.assertGreater(count_after, count_before)
-
- def test_add_category_unauthorized(self):
- '''
- Test adding a category by an unauthorized user
- '''
-
- self.user_login('test')
- self.add_category(fail=True)
-
- def test_add_category_anonymous(self):
- '''
- Test adding a category by an anonymous user
- '''
-
- self.add_category(fail=True)
-
- def test_add_category_authorized(self):
- '''
- Test adding a category by an authorized user
- '''
-
- self.user_login()
- self.add_category()
+ object_class = ExerciseCategory
+ url = 'exercisecategory-add'
+ pk = 5
+ data = {'name': 'A new category'}
diff --git a/wger/exercises/tests/exercise.py b/wger/exercises/tests/exercise.py
index f7762d01c..a99c8c0b8 100644
--- a/wger/exercises/tests/exercise.py
+++ b/wger/exercises/tests/exercise.py
@@ -295,7 +295,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):
self.user_login('test')
self.search_exercise()
-
+
class DeleteExercisesTestCase(WorkoutManagerDeleteTestCase):
'''
diff --git a/wger/exercises/tests/exercise_comments.py b/wger/exercises/tests/exercise_comments.py
index 82f00a468..369d84b90 100644
--- a/wger/exercises/tests/exercise_comments.py
+++ b/wger/exercises/tests/exercise_comments.py
@@ -16,74 +16,38 @@
from django.core.urlresolvers import reverse
from wger.exercises.models import Exercise
+from wger.exercises.models import ExerciseComment
from wger.manager.tests.testcase import WorkoutManagerTestCase
+from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
+from wger.manager.tests.testcase import WorkoutManagerEditTestCase
+from wger.manager.tests.testcase import WorkoutManagerAddTestCase
-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()
+class AddExerciseCommentTestCase(WorkoutManagerAddTestCase):
+ '''
+ Tests adding a comment to an exercise
+ '''
- 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
- '''
+ object_class = ExerciseComment
+ url = reverse('exercisecomment-add', kwargs={'exercise_pk': 1})
+ pk = 3
+ user_fail = False
+ data = {'comment': 'a new cool comment'}
- 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
- '''
+class EditExerciseCommentTestCase(WorkoutManagerEditTestCase):
+ '''
+ Tests editing a comment to an exercise
+ '''
- self.exercise_add_comment(fail=True)
+ object_class = ExerciseComment
+ url = 'exercisecomment-edit'
+ pk = 1
+ data = {'comment': 'an edited comment'}
- def test_exercise_add_comment_authorized(self):
- '''
- Tests the exercise comments
- '''
- self.user_login()
- self.exercise_add_comment(fail=False)
+class ExercisecommentsTestCase(WorkoutManagerTestCase):
def exercise_delete_comment(self, fail=True):
'''
@@ -135,61 +99,3 @@ class ExercisecommentsTestCase(WorkoutManagerTestCase):
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/weight/tests/entry.py b/wger/weight/tests/entry.py
index 5a4124812..43e6b17f4 100644
--- a/wger/weight/tests/entry.py
+++ b/wger/weight/tests/entry.py
@@ -43,7 +43,7 @@ class EditWeightEntryTestCase(WorkoutManagerEditTestCase):
'''
object_class = WeightEntry
- url = reverse('weight-edit', kwargs={'pk': 1})
+ url = 'weight-edit'
pk = 1
data = {'weight': 100,
'creation_date': datetime.date(2013, 2, 1)}