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 *
nu = datetime.datetime.now()

from rowers.views import add_defaultfavorites

#@pytest.mark.django_db
@override_settings(TESTING=True)
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,gdproptin=True,surveydone=True,
                                      gdproptindate=timezone.now(),
                                      rowerplan='coach',
        )

        add_defaultfavorites(self.r)

        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)

    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.getsmallrowdata_db',side_effect=mocked_getsmallrowdata_db)
    def test_upload_view_sled(self, mocked_sqlalchemy,mocked_getsmallrowdata_db):
        login = self.c.login(username='john',password='koeinsloot')
        self.assertTrue(login)

        filename = 'rowers/tests/testdata/testdata.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/edit/',
                             status_code=302,target_status_code=200)

        self.assertEqual(response.status_code, 200)

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


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


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


        f.close()


        response = self.c.get('/rowers/workout/'+encoded1+'/workflow/',
                              follow=True)

        self.assertEqual(response.status_code, 200)

        response = self.c.get('/rowers/workout/'+encoded1+'/get-thumbnails/',
                              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',
            'weightcategory':'hwt',
            'adaptiveclass':'PR1',
            'workouttype':'rower',
            'boattype':'1x',
            'dragfactor':'112',
            'private':True,
            'notes':'noot mies',
            }
        form = WorkoutForm(data=form_data)
        self.assertTrue(form.is_valid())
        response = self.c.post('/rowers/workout/'+encoded1+'/edit/', form_data, follow=True)
        self.assertEqual(response.status_code, 200)


        w = Workout.objects.get(id=1)
        f_to_be_deleted = w.csvfilename
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass




    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)

    @patch('rowers.dataprep.create_engine')
    def test_upload_view_sled_negativetime(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/tim.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            'notes':'aap noot mies',
            'make_plot':False,
            'upload_to_C2':False,
            'upload_to_Strava':False,
            'upload_to_SportTracks':False,
            'upload_to_RunKeeper':False,
            'upload_to_MapMyFitness':False,
            'plottype':'timeplot',
            'file': f,
            'makeprivate':False,
            'landingpage':'workout_edit_view',
            }

        form = DocumentsForm(form_data,file_data)

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


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

        self.assertEqual(response.status_code, 200)

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

        f.close()

        w = Workout.objects.get(id=1)
        f_to_be_deleted = w.csvfilename
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass



    @patch('rowers.dataprep.create_engine')
    def test_upload_view_sled_noname(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/testdata.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass

    @patch('rowers.dataprep.create_engine')
    def test_upload_view_logcard(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/logcard.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'',
            'workouttype':'rower',
            'boattype': '1x',
            '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)



    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.TCXParser')
    def test_upload_view_TCX_CN(self, mocked_sqlalchemy, mocked_tcx_parser):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/crewnerddata.tcx'
        f = open(filename,'rb')
        file_data = {'file': f}

        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/edit/',
                             status_code=302,target_status_code=200)

        self.assertEqual(response.status_code, 200)

        filename = 'rowers/tests/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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass

    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.TCXParser')
    def test_upload_view_TCX_SpeedCoach2a(self, mocked_sqlalchemy,
                                          mocked_tcx_parser):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/Speedcoach2example.csv'
        f = open(filename,'rb')
        file_data = {'file': f}

        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass

    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.TCXParser')
    def test_upload_view_TCX_SpeedCoach2b(self, mocked_sqlalchemy,
                                          mocked_tcx_parser):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/Speedcoach2example.csv'
        f = open(filename,'rb')
        file_data = {'file': f}

        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass



    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.TCXParser')
    def test_upload_view_TCX_SpeedCoach2c(self, mocked_sqlalchemy,
                                          mocked_tcx_parser):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/speedcoach3test3.csv'
        f = open(filename,'rb')
        file_data = {'file': f}

        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass


    @patch('rowers.dataprep.create_engine')
    def test_upload_view_SpeedCoach2v127(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/SpeedCoach2Linkv1.27.csv'
        f = open(filename,'rb')
        file_data = {'file': f}

        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass



    @patch('rowers.dataprep.create_engine')
    def test_upload_view_SpeedCoach2v127intervals(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/SpeedCoach2Link_interval.csv'
        f = open(filename,'rb')
        file_data = {'file': f}

        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass



    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.TCXParser')
    def test_upload_view_TCX_NoHR(self, mocked_sqlalchemy,
                                  mocked_tcx_parser):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/NoHR.tcx'
        f = open(filename,'rb')
        file_data = {'file': f}

        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass


    @patch('rowers.dataprep.create_engine')
    @patch('rowers.dataprep.TCXParser')
    def test_upload_view_TCX_CN(self, mocked_sqlalchemy,
                                mocked_tcx_parser):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/rowinginmotionexample.tcx'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'water',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass


    @patch('rowers.dataprep.create_engine')
    def test_upload_view_RP(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/RP_testdata.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass


    @patch('rowers.dataprep.create_engine')
    def test_upload_view_Mystery(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/mystery.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass


    @patch('rowers.dataprep.create_engine')
    def test_upload_view_RP_interval(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/RP_interval.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass



    @patch('rowers.dataprep.create_engine')
    def test_upload_view_sled_desktop(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/painsled_desktop_example.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass

    @patch('rowers.dataprep.create_engine')
    def test_upload_view_sled_ergdata(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/ergdata_example.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass

    @patch('rowers.dataprep.create_engine')
    def test_upload_view_sled_boatcoach(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/boatcoach.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass

    @patch('rowers.dataprep.create_engine')
    def test_upload_view_sled_ergstick(self, mocked_sqlalchemy):
        self.c.login(username='john',password='koeinsloot')

        filename = 'rowers/tests/testdata/ergstick.csv'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'title':'test',
            'workouttype':'rower',
            'boattype':'1x',
            '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/'+encoded1+'/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
        try:
            os.remove(f_to_be_deleted+'.gz')
        except (FileNotFoundError,OSError):
            pass
