# 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

import datetime

from django.core.urlresolvers import reverse

from wger.weight.models import WeightEntry

from wger.manager.tests.testcase import WorkoutManagerTestCase


class AddWeightEntryTestCase(WorkoutManagerTestCase):
    '''
    Tests adding a weight entry
    '''

    def add_entry(self, fail=False):
        '''
        Helper function to test adding a weight entry
        '''

        # Fetch the add entry page
        response = self.client.get(reverse('weight-add'))

        if fail:
            self.assertIn(response.status_code, (302, 403))
            self.assertTemplateUsed('login.html')

        else:
            self.assertEqual(response.status_code, 200)

        # Enter the data
        response = self.client.post(reverse('weight-add'),
                                    {'weight': '81',
                                    'creation_date': '2013-02-01'})
        if fail:
            self.assertRaises(WeightEntry.DoesNotExist, WeightEntry.objects.get, pk=8)
            self.assertIn(response.status_code, (302, 403))
            self.assertTemplateUsed('login.html')

        else:
            entry = WeightEntry.objects.get(pk=8)
            self.assertEqual(response.status_code, 302)
            self.assertEqual(entry.weight, 81)
            self.assertEqual(entry.creation_date, datetime.date(2013, 2, 1))

    def test_add_entry_anonymous(self):
        '''
        Test adding a weight entry as an anonymous user
        '''

        self.add_entry(fail=True)

    def test_add_entry_logged_in(self):
        '''
        Test adding a weight entry as a logged in user
        '''

        self.user_login('test')
        self.add_entry(fail=False)


class EditWeightEntryTestCase(WorkoutManagerTestCase):
    '''
    Tests editing a weight entry
    '''

    def edit_entry(self, fail=False):
        '''
        Helper function
        '''

        # Fetch the day edit page
        response = self.client.get(reverse('weight-edit', kwargs={'pk': 1}))

        if fail:
            self.assertIn(response.status_code, (302, 403))
            self.assertTemplateUsed('login.html')

        else:
            self.assertEqual(response.status_code, 200)

        # Edit the day
        response = self.client.post(reverse('weight-edit', kwargs={'pk': 1}),
                                    {'weight': 100,
                                    'creation_date': '2005-01-01'})

        entry = WeightEntry.objects.get(pk=1)

        if fail:
            self.assertIn(response.status_code, (302, 403))
            self.assertTemplateUsed('login.html')
            self.assertEqual(entry.weight, 77)
            self.assertEqual(entry.creation_date, datetime.date(2012, 10, 1))

        else:
            self.assertEqual(response.status_code, 302)
            self.assertEqual(entry.weight, 100)
            self.assertEqual(entry.creation_date, datetime.date(2005, 1, 1))

    def test_edit_entry_anonymous(self):
        '''
        Test editing a weight entry as an anonymous user
        '''

        self.edit_entry(fail=True)

    def test_edit_entry_owner(self):
        '''
        Test editing a weight entry as the owner user
        '''

        self.user_login('test')
        self.edit_entry(fail=False)

    def test_edit_entry_other(self):
        '''
        Test editing a weight entry as a different user
        '''

        self.user_login('admin')
        self.edit_entry(fail=True)

@@ -792,3279 +792,822 @@
 ase%0A
-%0A%0Aclass AddWeightEntryTestCase(WorkoutManagerTestCase):%0A    '''%0A    Tests adding a weight entry%0A    '''%0A%0A    def add_entry(self, fail=False):%0A        '''%0A        Helper function to test adding a weight entry%0A        '''%0A%0A        # Fetch the add entry page%0A        response = self.client.get(reverse('weight-add'))%0A%0A        if fail:%0A            self.assertIn(response.status_code, (302, 403))%0A            self.assertTemplateUsed('login.html')%0A%0A        else:%0A            self.assertEqual(response.status_code, 200)%0A%0A        # Enter the data%0A        response = self.client.post(reverse('weight-add'),%0A                                    %7B'weight': '81',%0A                                    'creation_date': '2013-02-01'%7D)%0A        if fail:%0A            self.assertRaises(WeightEntry.DoesNotExist, WeightEntry.objects.get, pk=8)%0A            self.assertIn(response.status_code, (302, 403))%0A            self.assertTemplateUsed('login.html')%0A%0A        else:%0A            entry = WeightEntry.objects.get(pk=8)%0A            self.assertEqual(response.status_code, 302)%0A            self.assertEqual(entry.weight, 81)%0A            self.assertEqual(entry.creation_date, datetime.date(2013, 2, 1))%0A%0A    def test_add_entry_anonymous(self):%0A        '''%0A        Test adding a weight entry as an anonymous user%0A        '''%0A%0A        self.add_entry(fail=True)%0A%0A    def test_add_entry_logged_in(self):%0A        '''%0A        Test adding a weight entry as a logged in user%0A        '''%0A%0A        self.user_login('test')%0A        self.add_entry(fail=False)%0A%0A%0Aclass EditWeightEntryTestCase(WorkoutManagerTestCase):%0A    '''%0A    Tests editing a weight entry%0A    '''%0A%0A    def edit_entry(self, fail=False):%0A        '''%0A        Helper function%0A        '''%0A%0A        # Fetch the day edit page%0A        response = self.client.get(reverse('weight-edit', kwargs=%7B'pk': 1%7D))%0A%0A        if fail:%0A            self.assertIn(response.status_code, (302, 403))%0A            self.assertTemplateUsed('login.html')%0A%0A        else:%0A            self.assertEqual(response.status_code, 200)%0A%0A        # Edit the day%0A        response = self.client.post(reverse('weight-edit', kwargs=%7B'pk': 1%7D),%0A                                    %7B'weight': 100,%0A                                    'creation_date': '2005-01-01'%7D)%0A%0A        entry = WeightEntry.objects.get(pk=1)%0A%0A        if fail:%0A            self.assertIn(response.status_code, (302, 403))%0A            self.assertTemplateUsed('login.html')%0A            self.assertEqual(entry.weight, 77)%0A            self.assertEqual(entry.creation_date, datetime.date(2012, 10, 1))%0A%0A        else:%0A            self.assertEqual(response.status_code, 302)%0A            self.assertEqual(entry.weight, 100)%0A            self.assertEqual(entry.creation_date, datetime.date(2005, 1, 1))%0A%0A    def test_edit_entry_anonymous(self):%0A        '''%0A        Test editing a weight entry as an anonymous user%0A        '''%0A%0A        self.edit_entry(fail=True)%0A%0A    def test_edit_entry_owner(self):%0A        '''%0A        Test editing a weight entry as the owner user%0A        '''%0A%0A        self.user_login('test')%0A        self.edit_entry(fail=False)%0A%0A    def test_edit_entry_other(self):%0A        '''%0A        Test editing a weight entry as a different user%0A        '''%0A%0A        self.user_login('admin')%0A        self.edit_entry(fail=True)
+from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase%0Afrom wger.manager.tests.testcase import WorkoutManagerEditTestCase%0Afrom wger.manager.tests.testcase import WorkoutManagerAddTestCase%0A%0A%0Aclass AddWeightEntryTestCase(WorkoutManagerAddTestCase):%0A    '''%0A    Tests adding a weight entry%0A    '''%0A%0A    object_class = WeightEntry%0A    url = 'weight-add'%0A    pk = 8%0A    user_fail = False%0A    data = %7B'weight': 81.1,%0A            'creation_date': datetime.date(2013, 2, 1)%7D%0A%0A%0Aclass EditWeightEntryTestCase(WorkoutManagerEditTestCase):%0A    '''%0A    Tests editing a weight entry%0A    '''%0A%0A    object_class = WeightEntry%0A    url = reverse('weight-edit', kwargs=%7B'pk': 1%7D)%0A    pk = 1%0A    data = %7B'weight': 100,%0A            'creation_date': datetime.date(2013, 2, 1)%7D%0A    user_success = 'test'%0A    user_fail = 'admin'
 %0A

Rewrite the tests using the more compact base testcase