from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from .statements import *

nu = datetime.datetime.now()

from django.http import Http404

from rowers.views import get_workout

# tests simple functions from views.py
class SimpleViewTest(TestCase):
    def setUp(self):
        self.u = UserFactory()

        self.r = Rower.objects.create(user=self.u,
                                      birthdate=faker.profile()['birthdate'],
                                      gdproptin=True,surveydone=True,
                                      gdproptindate=timezone.now(),
                                      protrialexpires=datetime.date(1970,1,1),
                                      plantrialexpires=datetime.date(1970,1,1),
                                      rowerplan='basic')

        self.c = Client()
        self.user_workouts = WorkoutFactory.create_batch(5, user=self.r)
        #        self.factory = RequestFactory()
        self.password = faker.word()
        self.u.set_password(self.password)
        self.u.save()

    def tearDown(self):
        for workout in self.user_workouts:
            try:
                os.remove(workout.csvfilename)
            except (IOError, FileNotFoundError,OSError):
                pass

    def test_start_protrial(self):
        login = self.c.login(username=self.u.username, password=self.password)
        self.assertTrue(login)

        url = '/rowers/starttrial/'

        response = self.c.get(url,follow=True)

        self.assertRedirects(response,
                             expected_url='/rowers/list-workouts/',
                             status_code=302,target_status_code=200)

    def test_start_plantrial(self):
        login = self.c.login(username=self.u.username, password=self.password)
        self.assertTrue(login)

        url = '/rowers/startplantrial/'

        response = self.c.get(url,follow=True)

        self.assertRedirects(response,
                             expected_url='/rowers/list-workouts/',
                             status_code=302,target_status_code=200)
