from django.test import TestCase, Client,override_settings
from django.core.management import call_command
from django.utils.six import StringIO
from django.test.client import RequestFactory
from .views import checkworkoutuser,c2_open
from rowers.models import Workout, User, Rower, WorkoutForm,RowerForm,GraphImage
from rowers.forms import DocumentsForm,CNsummaryForm,RegistrationFormUniqueEmail
import rowers.plots as plots
import rowers.interactiveplots as iplots
import datetime
from rowingdata import rowingdata as rdata
from rowingdata import rower as rrower
from rowers.rows import handle_uploaded_file
from django.core.files.uploadedfile import SimpleUploadedFile
from time import strftime,strptime,mktime,time,daylight
import os
from rowers.tasks import handle_makeplot
from rowers.utils import serialize_list,deserialize_list
from rowers.c2stuff import C2NoTokenError
from shutil import copyfile

from minimocktest import MockTestCase
import pandas as pd

import json
import numpy as np

from rowers import urls
from rowers.views import error500_view,error404_view,error400_view,error403_view

from dataprep import delete_strokedata

from redis import StrictRedis
redis_connection = StrictRedis()



class DjangoTestCase(TestCase, MockTestCase):
    def _pre_setup(self):
	MockTestCase.setUp(self)
	TestCase._pre_setup(self)
	self.client = Client()

    def _post_teardown(self):
	TestCase._post_teardown(self)
	MockTestCase.tearDown(self)
        delete_strokedata(1)

# Create your tests here.

class C2Objects(DjangoTestCase):
    def test_strokedata(self):
	with open('rowers/testdata/c2stroketestdata.txt','r') as infile:
	    res = json.load(infile)

	strokedata = pd.DataFrame.from_dict(res['data'])

	with open('rowers/testdata/c2testdata.txt','r') as infile:
	    res = json.load(infile)

	data = res['data']

	from rowers.views import add_workout_from_strokedata

	u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
        u.first_name = 'John'
        u.last_name = 'Sander'
        u.save()
	r = Rower.objects.create(user=u)

	res = add_workout_from_strokedata(u,1,data,strokedata,source='c2')

    def test_strokedatanohr(self):
	with open('rowers/testdata/c2strokedatanohr.txt','r') as infile:
	    res = json.load(infile)

	strokedata = pd.DataFrame.from_dict(res['data'])

	with open('rowers/testdata/c2testdata.txt','r') as infile:
	    res = json.load(infile)

	data = res['data']

	from rowers.views import add_workout_from_strokedata

	u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
        u.first_name = 'John'
        u.last_name = 'Sander'
        u.save()
	r = Rower.objects.create(user=u)

	res = add_workout_from_strokedata(u,1,data,strokedata,source='c2')

        
class StravaObjects(DjangoTestCase):
    def test_strokedata(self):
        timejson = json.load(open('rowers/testdata/stravatimetestdata.txt','r'))
        velojson = json.load(open('rowers/testdata/stravavelotestdata.txt','r'))
        distancejson = json.load(open('rowers/testdata/stravadistancetestdata.txt','r'))
        latlongjson = json.load(open('rowers/testdata/stravalatlongtestdata.txt','r'))
        hrjson = json.load(open('rowers/testdata/stravahrtestdata.txt','r'))
        spmjson = json.load(open('rowers/testdata/stravaspmtestdata.txt','r'))
        workoutsummary = json.load(open('rowers/testdata/stravasummarytestdata.txt','r'))
	workoutsummary['timezone'] = "Etc/UTC"
	startdatetime = workoutsummary['start_date']

        
        t = np.array(timejson[0]['data'])
	d =  np.array(distancejson[0]['data'])
	nr_rows = len(t)

	try:
	    spm = np.array( spmjson[1]['data'])
	except IndexError:
	    spm = np.zeros(nr_rows)

	try:
	    hr = np.array(hrjson[1]['data'])
	except IndexError:
	    hr = np.zeros(nr_rows)

	try:
	    velo = np.array(velojson[1]['data'])
	except IndexError:
	    velo = np.zeros(nr_rows)

	dt = np.diff(t).mean()
	wsize = round(5./dt)

        from stravastuff import ewmovingaverage
	velo2 = ewmovingaverage(velo,wsize)
	coords = np.array(latlongjson[0]['data'])
	try:
	    lat = coords[:,0]
	    lon = coords[:,1]
	except IndexError:
	    lat = np.zeros(len(t))
	    lon = np.zeros(len(t))




	strokelength = velo*60./(spm)
	strokelength[np.isinf(strokelength)] = 0.0

	pace = 500./(1.0*velo2)
	pace[np.isinf(pace)] = 0.0

	strokedata = pd.DataFrame({'t':10*t,
			   'd':10*d,
			   'p':10*pace,
			   'spm':spm,
			   'hr':hr,
			   'lat':lat,
			   'lon':lon,
			   'strokelength':strokelength,
			   })

	from rowers.views import add_workout_from_strokedata

	u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
        u.first_name = 'John'
        u.last_name = 'Sander'
        u.save()
	r = Rower.objects.create(user=u)

	res = add_workout_from_strokedata(u,1,workoutsummary,strokedata,
                                      source='strava')

    def test_strokedatanohr(self):
        timejson = json.load(open('rowers/testdata/stravatimetestdata.txt','r'))
        velojson = json.load(open('rowers/testdata/stravavelotestdata.txt','r'))
        distancejson = json.load(open('rowers/testdata/stravadistancetestdata.txt','r'))
        latlongjson = json.load(open('rowers/testdata/stravalatlongtestdata.txt','r'))
        hrjson = json.load(open('rowers/testdata/stravahrtestnodata.txt','r'))
        spmjson = json.load(open('rowers/testdata/stravaspmtestdata.txt','r'))
        workoutsummary = json.load(open('rowers/testdata/stravasummarytestdata.txt','r'))
	workoutsummary['timezone'] = "Etc/UTC"
	startdatetime = workoutsummary['start_date']

        
        t = np.array(timejson[0]['data'])
	d =  np.array(distancejson[0]['data'])
	nr_rows = len(t)

	try:
	    spm = np.array( spmjson[1]['data'])
	except IndexError:
	    spm = np.zeros(nr_rows)

	try:
	    hr = np.array(hrjson[1]['data'])
	except IndexError:
	    hr = np.zeros(nr_rows)

	try:
	    velo = np.array(velojson[1]['data'])
	except IndexError:
	    velo = np.zeros(nr_rows)

	dt = np.diff(t).mean()
	wsize = round(5./dt)

        from stravastuff import ewmovingaverage
	velo2 = ewmovingaverage(velo,wsize)
	coords = np.array(latlongjson[0]['data'])
	try:
	    lat = coords[:,0]
	    lon = coords[:,1]
	except IndexError:
	    lat = np.zeros(len(t))
	    lon = np.zeros(len(t))




	strokelength = velo*60./(spm)
	strokelength[np.isinf(strokelength)] = 0.0

	pace = 500./(1.0*velo2)
	pace[np.isinf(pace)] = 0.0

	strokedata = pd.DataFrame({'t':10*t,
			   'd':10*d,
			   'p':10*pace,
			   'spm':spm,
			   'hr':hr,
			   'lat':lat,
			   'lon':lon,
			   'strokelength':strokelength,
			   })

	from rowers.views import add_workout_from_strokedata

	u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
        u.first_name = 'John'
        u.last_name = 'Sander'
        u.save()
	r = Rower.objects.create(user=u)

	res = add_workout_from_strokedata(u,1,workoutsummary,strokedata,
                                      source='strava')


        
class STObjects(DjangoTestCase):    
    def test_strokedata(self):
	with open('rowers/testdata/sporttrackstestdata.txt','r') as infile:
	    data = json.load(infile)

	from rowers.views import add_workout_from_stdata

	u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
	r = Rower.objects.create(user=u)

	res = add_workout_from_stdata(u,1,data)

    def test_strokedatanohr(self):
	with open('rowers/testdata/sporttrackstestnohr.txt','r') as infile:
	    data = json.load(infile)

	from rowers.views import add_workout_from_stdata

	u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
	r = Rower.objects.create(user=u)

	res = add_workout_from_stdata(u,1,data)

        
class TestErrorPages(TestCase):
    def test_error_handlers(self):

        self.assertTrue(urls.handler404.endswith('.error404_view'))
        self.assertTrue(urls.handler500.endswith('.error500_view'))
        factory = RequestFactory()
        request = factory.get('/')
        response = error404_view(request)
        self.assertEqual(response.status_code, 404)
        self.assertIn('404 Page not found', unicode(response))
        response = error500_view(request)
        self.assertEqual(response.status_code, 500)
        self.assertIn('500 Internal Server Error', unicode(response))

class NewUserRegistrationTest(TestCase):
    def setUp(self):
	self.c = Client()
        redis_connection.publish('tasks','KILL')

    def test_newuser(self):
        form_data = {
	    'first_name':'Jan',
	    'last_name':'Roeiert',
	    'email':'jan@loop.nl',
            'username':'janderoeiert',
            'password1':'aapindewei2',
            'password2':'aapindewei2',
            'tos':True,
            'weightcategory':'hwt',
            'sex':'male',
            'birthdate':datetime.datetime(year=1970,month=4,day=2)
	    }

	form = RegistrationFormUniqueEmail(form_data)
        self.assertTrue(form.is_valid())
    
	response = self.c.post('/rowers/register', form_data, follow=True)
        
	self.assertRedirects(response,
                             expected_url='/rowers/register/thankyou/',
			     status_code=302,target_status_code=200)

from django_mailbox.models import Mailbox,MessageAttachment,Message
        
class EmailTests(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)
	nu = datetime.datetime.now()
        workoutsbox = Mailbox.objects.create(name='workouts')
        workoutsbox.save()
        failbox = Mailbox.objects.create(name='Failed')
        failbox.save()
        
        for filename in os.listdir('rowers/testdata/emails'):
            m = Message(mailbox=workoutsbox,
                        from_header = u.email,
                        subject = filename,
                        body="""
                        chart time
                        """)
            m.save()
            a2 = 'media/mailbox_attachments/'+filename
            copyfile('rowers/testdata/emails/'+filename,a2)
            a = MessageAttachment(message=m,document=a2[6:])
            a.save()

    def tearDown(self):
        for filename in os.listdir('media/mailbox_attachments'):
            path = os.path.join('media/mailbox_attachments/',filename)
            if not os.path.isdir(path):
                os.remove(path)
            
    def test_emailprocessing(self):
        out = StringIO()
        call_command('processemail', stdout=out,testing=True)
        self.assertIn('Successfully processed email attachments',out.getvalue())
        
        
class WorkoutTests(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)
	nu = datetime.datetime.now()
	w = Workout.objects.create(name='testworkout',workouttype='On-water',
				   user=r,date=nu.strftime('%Y-%m-%d'),
				   starttime=nu.strftime('%H:%M:%S'),
				   duration="0:55:00",distance=8000)

    def test_checkworkoutuser(self):
	u = User.objects.get(username='john')
	r = Rower.objects.get(user=u)
	w = Workout.objects.get(user=r)

	self.assertEqual(checkworkoutuser(u,w),True)

class C2Tests(TestCase):
    def setUp(self):
        redis_connection.publish('tasks','KILL')
	self.u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
	self.r = Rower.objects.create(user=u)
	self.nu = datetime.datetime.now()
	self.w = Workout.objects.create(name='testworkout',workouttype='On-water',
				   user=r,date=nu.strftime('%Y-%m-%d'),
				   starttime=nu.strftime('%H:%M:%S'),
				   duration="0:55:00",distance=8000)

    def c2_notokentest(self):
	thetoken = c2_open(self.u)
	# should raise C2NoTokenError
	self.assertRaises(C2NoTokenError)

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)
	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',
	    'workouttype':'water',
	    }
	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(self):
	filename = 'rowers/testdata/testdata.csv'
	f = open(filename,'rb')
	file_data = {'file': SimpleUploadedFile(f.name, f.read())}
	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    }

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

	u = User.objects.get(username='john')
	r = Rower.objects.get(user=u)
	
	rr = rrower(hrmax=r.max,hrut2=r.ut2,
		    hrut1=r.ut1,hrat=r.at,
		    hrtr=r.tr,hran=r.an,ftp=r.ftp)
	row = rdata(filename,rower=rr)
	totaldist = row.df['cum_dist'].max()
	totaltime = row.df['TimeStamp (sec)'].max()-row.df['TimeStamp (sec)'].min()
	totaltime = totaltime+row.df.ix[0,' ElapsedTime (sec)']

	
	hours = int(totaltime/3600.)
	minutes = int((totaltime - 3600.*hours)/60.)
	seconds = int(totaltime - 3600.*hours - 60.*minutes)
	tenths = int(10*(totaltime - 3600.*hours - 60.*minutes - seconds))

	duration = "%s:%s:%s.%s" % (hours,minutes,seconds,tenths)


	workoutdate = row.rowdatetime.strftime('%Y-%m-%d')
	workoutstarttime = row.rowdatetime.strftime('%H:%M:%S')

	w = Workout.objects.create(name='testworkout',workouttype='On-water',
				   user=r,date=self.nu.strftime('%Y-%m-%d'),
				   starttime=workoutstarttime,
				   duration=duration,distance=totaldist,
				   csvfilename=filename)


	fig1 = plots.mkplot(row,'test')
	res = iplots.interactive_chart(w.id)
	res = iplots.interactive_chart(w.id,promember=1)
	res = iplots.interactive_bar_chart(w.id)
	res = iplots.interactive_bar_chart(w.id,promember=1)
	res = iplots.interactive_flex_chart2(w.id,promember=0,xparam='time',
					    yparam1='pace',yparam2='hr')
	res = iplots.interactive_flex_chart2(w.id,promember=0,xparam='distance',
					    yparam1='pace',yparam2='hr')
	res = iplots.interactive_flex_chart2(w.id,promember=0,xparam='time',
					    yparam1='pace',yparam2='spm')
	res = iplots.interactive_flex_chart2(w.id,promember=0,xparam='distance',
					    yparam1='pace',yparam2='spm')

	res = iplots.interactive_flex_chart2(w.id,promember=1,xparam='time',
					    yparam1='pace',yparam2='hr')
	res = iplots.interactive_flex_chart2(w.id,promember=1,xparam='distance',
					    yparam1='pace',yparam2='hr')
	res = iplots.interactive_flex_chart2(w.id,promember=1,xparam='time',
					    yparam1='pace',yparam2='spm')
	res = iplots.interactive_flex_chart2(w.id,promember=1,xparam='distance',
					    yparam1='pace',yparam2='spm')

	


class ViewTest(TestCase):
    def setUp(self):
        redis_connection.publish('tasks','KILL')
	self.c = Client()
	self.u = User.objects.create_user('john',
				     'sander@ds.ds',
				     'koeinsloot')
	self.r = Rower.objects.create(user=self.u)
	self.nu = datetime.datetime.now()

    def test_upload_view_notloggedin(self):
	response = self.c.post('/rowers/workout/upload/',follow=True)


	self.assertRedirects(response, expected_url='/login/?next=/rowers/workout/upload/',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)
	
    def test_upload_view_sled(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/testdata.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)
	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)

	f.close()

	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	response = self.c.get('/rowers/workout/1/', form_data, follow=True)
	self.assertEqual(response.status_code, 200)

        response = self.c.get('/rowers/workout/1/export', form_data, follow=True)
        self.assertEqual(response.status_code, 200)
        
	response = self.c.get('/rowers/workout/1/interactiveplot', form_data, follow=True)
	self.assertEqual(response.status_code, 200)

        response = self.c.get('/rowers/workout/1/histo', form_data, follow=True)
	self.assertEqual(response.status_code, 200)

	form_data = {
            'name':'aap',
            'date':'2016-11-05',
            'starttime':'09:07:14',
            'timezone':'Europe/Berlin',
            'duration':'1:00:00.5',
            'distance':'15000',
            'workouttype':'rower',
            'notes':'noot mies',
            }
        form = WorkoutForm(data=form_data)
        self.assertTrue(form.is_valid())
        response = self.c.post('/rowers/workout/1/edit', form_data, follow=True)
	self.assertEqual(response.status_code, 200)
        

        w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')


    def test_upload_view_notloggedin(self):
	response = self.c.post('/rowers/workout/upload/',follow=True)


	self.assertRedirects(response, expected_url='/login/?next=/rowers/workout/upload/',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)
	
    def test_upload_view_sled_negativetime(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/tim.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)

	f.close()

	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	response = self.c.get('/rowers/workout/1/', form_data, follow=True)
	self.assertEqual(response.status_code, 200)
	
	response = self.c.get('/rowers/workout/1/interactiveplot', form_data, follow=True)
	self.assertEqual(response.status_code, 200)

	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

	
	

    def test_upload_view_sled_noname(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/testdata.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)

	f.close()

	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_logcard(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/logcard.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)

	f.close()

	self.assertRedirects(response,
                             expected_url='/rowers/workout/upload/',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)



	
    def test_upload_view_TCX_CN(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/crewnerddata.tcx'
	f = open(filename,'rb')
	file_data = {'file': f}

	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	f.close()
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	filename = 'rowers/testdata/crewnerddata.CSV'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {}
	form = CNsummaryForm(form_data,file_data)

	response = self.c.post('rowers/workout/1/crewnerdsummary',
			       form_data,
			       follow=True)

	f.close()

	self.assertEqual(response.status_code, 200)
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_TCX_SpeedCoach2(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/Speedcoach2example.csv'
	f = open(filename,'rb')
	file_data = {'file': f}

	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	f.close()
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_TCX_SpeedCoach2(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/Speedcoach2example.csv'
	f = open(filename,'rb')
	file_data = {'file': f}

	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	f.close()
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')



    def test_upload_view_TCX_SpeedCoach2(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/speedcoach3test3.csv'
	f = open(filename,'rb')
	file_data = {'file': f}

	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	f.close()
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_SpeedCoach2v127(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/SpeedCoach2Linkv1.27.csv'
	f = open(filename,'rb')
	file_data = {'file': f}

	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	f.close()
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')


    def test_upload_view_SpeedCoach2v127intervals(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/SpeedCoach2Link_interval.csv'
	f = open(filename,'rb')
	file_data = {'file': f}

	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	f.close()
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')


    def test_upload_view_TCX_NoHR(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/NoHR.tcx'
	f = open(filename,'rb')
	file_data = {'file': f}

	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	f.close()
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)

	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_TCX_CN(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/rowinginmotionexample.tcx'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'water',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)

	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_RP(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/RP_testdata.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)
	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_Mystery(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/mystery.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)
	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_RP_interval(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/RP_interval.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)
	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

        
    def test_upload_view_sled_desktop(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/painsled_desktop_example.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)
	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_sled_ergdata(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/ergdata_example.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)
	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_sled_boatcoach(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/boatcoach.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)
	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

    def test_upload_view_sled_ergstick(self):
	self.c.login(username='john',password='koeinsloot')
	
	filename = 'rowers/testdata/ergstick.csv'
	f = open(filename,'rb')
	file_data = {'file': f}
	form_data = {
	    'title':'test',
	    'workouttype':'rower',
	    'notes':'aap noot mies',
	    'make_plot':False,
	    'upload_to_c2':False,
	    'plottype':'timeplot',
	    'file': f,
	    }

	form = DocumentsForm(form_data,file_data)

	response = self.c.post('/rowers/workout/upload/', form_data, follow=True)
	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
			     status_code=302,target_status_code=200)
	self.assertEqual(response.status_code, 200)
	
	w = Workout.objects.get(id=1)
	f_to_be_deleted = w.csvfilename
	os.remove(f_to_be_deleted+'.gz')

        
#    def test_upload_view_FIT_SpeedCoach2a(self):
#	self.c.login(username='john',password='koeinsloot')
	
#	filename = 'rowers/testdata/3x250m.fit'
#	with open(filename,'rb') as f: 

#	    file_data = {'file': f}

#	    form_data = {
#		'title':'test',
#		'workouttype':'water',
#		'notes':'aap noot mies',
#		'make_plot':False,
#		'upload_to_c2':False,
#		'plottype':'timeplot',
#		'file': f,
#		}

#	    form = DocumentsForm(form_data,file_data)

#	    response = self.c.post('/rowers/workout/upload/',
#				   form_data, follow=True,
#				   format='multipart')
#	f.close()
#	self.assertRedirects(response, expected_url='/rowers/workout/1/edit',
#			     status_code=302,target_status_code=200)

#	self.assertEqual(response.status_code, 200)

#	w = Workout.objects.get(id=1)
#	f_to_be_deleted = w.csvfilename
#	os.remove(f_to_be_deleted+'.gz')

class subroutinetests(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)
	nu = datetime.datetime.now()
	filename = 'rowers/testdata/testdata.csv'
	self.w = Workout.objects.create(name='testworkout',
					workouttype='On-water',
					user=r,date=nu.strftime('%Y-%m-%d'),
					starttime=nu.strftime('%H:%M:%S'),
					duration="0:55:00",distance=8000,
					csvfilename=filename)


    def c2stuff(self):
	data = c2stuff.createc2workoutdata(self.w)
	jsond = json.dumps(data)
	data = c2stuff.createc2workoutdata_as_splits(w)
	jsond = json.dumps(data)



class PlotTests(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)
	self.nu = datetime.datetime.now()
	filename = 'rowers/testdata/testdata.csv'
	self.wotw = Workout.objects.create(name='testworkout',
					workouttype='On-water',
					user=r,date=self.nu.strftime('%Y-%m-%d'),
					starttime=self.nu.strftime('%H:%M:%S'),
					duration="0:55:00",distance=8000,
					csvfilename=filename)
	
	self.wote = Workout.objects.create(name='testworkout',
					workouttype='Indoor Rower',
					user=r,date=self.nu.strftime('%Y-%m-%d'),
					starttime=self.nu.strftime('%H:%M:%S'),
					duration="0:55:00",distance=8000,
					csvfilename=filename)
	
        #	timestr = strftime("%Y%m%d-%H%M%S")
        #	imagename = f1+timestr+'.png'
        #	fullpathimagename = 'static/plots/'+imagename
        powerperc = 100*np.array([r.pw_ut2,
                                  r.pw_ut1,
                                  r.pw_at,
                                  r.pw_tr,r.pw_an])/r.ftp

        
	self.hrdata = {
	    'hrmax':r.max,
	    'hrut2':r.ut2,
	    'hrut1':r.ut1,
	    'hrat':r.at,
	    'hrtr':r.tr,
	    'hran':r.an,
	    'ftp':r.ftp,
            'powerperc':serialize_list(powerperc),
            'powerzones':serialize_list(r.powerzones),
	}


    def test_ote_plots(self):
	w = self.wote
	f1 = 'testdata.csv'[:-4] 
	timestr = strftime("%Y%m%d-%H%M%S")
	imagename = f1+timestr+'.png'
	fullpathimagename = 'static/plots/'+imagename
	# make plot - asynchronous task
	plotnr = 1
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 1
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 2
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 3
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 4
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 5
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 6
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 7
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 8
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 13
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

    def test_otw_plots(self):
	w = self.wotw
	f1 = 'testdata.csv'[:-4] 
	timestr = strftime("%Y%m%d-%H%M%S")
	imagename = f1+timestr+'.png'
	fullpathimagename = 'static/plots/'+imagename
	# make plot - asynchronous task
	plotnr = 1
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 1
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 2
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 3
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 4
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 5
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 6
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 7
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 8
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 9
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)

	plotnr = 13
	if (w.workouttype=='water'):
	    plotnr = plotnr+3
		    
	res = handle_makeplot(f1,w.csvfilename,
			      w.name,self.hrdata,plotnr,imagename)
	i = GraphImage(workout=w,creationdatetime=self.nu,
			   filename=fullpathimagename)
	os.remove(fullpathimagename)
