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

from .statements import *

class CoursesTest(TestCase):
    def setUp(self):
        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',
        )
        self.nu = datetime.datetime.now()


    def test_courses(self):
        login = self.c.login(username='john',password='koeinsloot')
        self.assertTrue(login)

        filename = 'rowers/tests/testdata/Courses.kml'
        f = open(filename,'rb')
        file_data = {'file': f}
        form_data = {
            'name': 'test courses',
            'notes': 'aap nn',
            'file':f,
            }

        courseform = CourseForm(form_data)
        self.assertTrue(courseform.is_valid())

        response = self.c.get('/rowers/courses/upload/')
        self.assertTrue(response.status_code,200)

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

        f.close()

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

        self.assertEqual(response.status_code, 200)


        response = self.c.get('/rowers/list-courses/')
        self.assertEqual(response.status_code, 200)

        response = self.c.get('/rowers/courses/1/edit/')
        self.assertEqual(response.status_code, 200)

        response = self.c.get('/rowers/courses/1/')
        self.assertEqual(response.status_code, 200)

        response = self.c.get('/rowers/courses/1/map/')
        self.assertEqual(response.status_code, 200)

        form_data = {
            'name':'apekoers',
            'country':'United States of Atlantis',
            'notes':'nota bene'
            }

        form = GeoCourseEditForm(form_data)
        self.assertTrue(form.is_valid())

        response = self.c.post('/rowers/courses/1/edit/',form_data)
        self.assertTrue(response.status_code,200)

        response = self.c.get('/rowers/courses/1/downloadkml/')
        self.assertEqual(response.status_code,200)

        self.assertEquals(
            response.get('Content-Disposition'),
            'attachment; filename="course_1.kml"'
            )
