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

#from __future__ import print_function
from .statements import *


#@pytest.mark.django_db
class DataTest(TestCase):
    def setUp(self):
        redis_connection.publish('tasks','KILL')
        u = User.objects.create_user('john',
                                     'sander@ds.ds',
                                     'koeinsloot')
        r = Rower.objects.create(user=u,gdproptin=True,surveydone=True,
                                 gdproptindate=timezone.now()
        )
        self.nu = datetime.datetime.now()


    def test_workoutform(self):
        form_data = {
            'name':'test',
            'date':'2016-05-01',
            'starttime':'07:53:00',
            'timezone':'UTC',
            'duration':'0:55:00.1',
            'distance':8000,
            'notes':'Aap noot \n mies',
            'weightcategory':'lwt',
            'adaptiveclass': 'PR1',
            'workouttype':'water',
            'boattype':'1x',
            'private':False,
            }
        form = WorkoutForm(data=form_data)
        self.assertTrue(form.is_valid())

    def test_rower_form_withvalidnumbers(self):
        form_data = {
            'max':192,
            'rest':48,
            'ut2':105,
            'ut1':148,
            'at':160,
            'tr':167,
            'an':180,
            'weightcategory':'lwt',
            }
        form = RowerForm(data=form_data)
        self.assertTrue(form.is_valid())


    def test_rower_form_twoequalvalues(self):
        form_data = {
            'max':192,
            'rest':48,
            'ut2':105,
            'ut1':105,
            'at':160,
            'an':180,
            'tr':167,
            'weightcategory':'lwt',
            }
        form = RowerForm(data=form_data)
        self.assertFalse(form.is_valid())

    def test_rower_form_abovemaxallowed(self):
        form_data = {
            'max':300,
            'rest':48,
            'ut2':105,
            'ut1':148,
            'at':160,
            'an':180,
            'tr':167,
            'weightcategory':'lwt',
            }
        form = RowerForm(data=form_data)
        self.assertFalse(form.is_valid())

    def test_rower_form_wrongorder(self):
        form_data = {
            'max':192,
            'rest':48,
            'ut2':20,
            'ut1':148,
            'at':160,
            'an':180,
            'tr':167,
            'weightcategory':'lwt',
            }
        form = RowerForm(data=form_data)
        self.assertFalse(form.is_valid())

    def test_rower_form_belowminalloed(self):
        form_data = {
            'max':192,
            'rest':2,
            'ut2':105,
            'ut1':148,
            'at':160,
            'an':180,
            'tr':167,
            'weightcategory':'lwt',
            }
        form = RowerForm(data=form_data)
        self.assertFalse(form.is_valid())

    def test_rower_form_wrongorder2(self):
        form_data = {
            'max':192,
            'rest':48,
            'ut2':105,
            'ut1':170,
            'at':160,
            'an':180,
            'tr':167,
            'weightcategory':'lwt',
            }
        form = RowerForm(data=form_data)
        self.assertFalse(form.is_valid())

    def test_painsled_form(self):
        filename = 'rowers/tests/testdata/testdata.csv'
        f = open(filename,'rb')
        file_data = {'file': SimpleUploadedFile(f.name, f.read())}
        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            'notes':'aap noot mies',
            }

        form = DocumentsForm(form_data,file_data)
        self.assertTrue(form.is_valid())

        f.close()
