import os
import tempfile
import shutil
import unittest
import textwrap
import json
import datetime

from .. import web

TMP_PREFIX = 'orgviz-test-'


def totimestamp(dt):
    zero = datetime.datetime.fromtimestamp(0)
    return (dt - zero).total_seconds()


class TestWebEventsData(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.tmpdir = tempfile.mkdtemp(prefix=TMP_PREFIX)
        cls.org_file = os.path.join(cls.tmpdir, 'test.org')
        web.app.config['ORG_FILE_COMMON'] = [cls.org_file]
        web.app.config['ORG_FILE_CAL'] = []
        web.app.config['NO_CACHE'] = True
        cls.app = web.app.test_client()

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.tmpdir)

    def test_single_event(self):
        with open(self.org_file, 'w') as f:
            f.write(textwrap.dedent("""
            * Node title
              SCHEDULED: <2012-10-23 Tue>
            """))
        rv = self.app.get('/events_data?start=1349042400&end=1352674800')
        events_data = json.loads(rv.data)
        self.assertEqual(len(events_data), 1)
        self.assertEqual(events_data[0]['title'], 'Node title')

    def test_start_end(self):
        with open(self.org_file, 'w') as f:
            f.write(textwrap.dedent("""
            * Node 1
              SCHEDULED: <2012-10-21 Tue>
            * Node 2
              SCHEDULED: <2012-10-22 Wed>
            * Node 3
              SCHEDULED: <2012-10-24 Fri>
            """))
        # FIXME: clarify boundary condition 2012-10-23 in Node 3 does not work!
        start = totimestamp(datetime.datetime(2012, 10, 20))
        end = totimestamp(datetime.datetime(2012, 10, 23))
        rv = self.app.get(
            '/events_data?start={0:.0f}&end={1:.0f}'.format(start, end))
        events_data = json.loads(rv.data)
        self.assertEqual(len(events_data), 2)
        self.assertEqual(events_data[0]['title'], 'Node 1')
        self.assertEqual(events_data[1]['title'], 'Node 2')

@@ -734,30 +734,33 @@
 def 
-test_single_event(self
+write_org_file(self, text
 ):%0A 
@@ -834,32 +834,97 @@
 wrap.dedent(
+text))%0A%0A    def test_single_event(self):%0A        self.write_org_file(
 %22%22%22%0A
-    
         * No
@@ -932,20 +932,16 @@
 e title%0A
-    
         
@@ -974,33 +974,28 @@
 ue%3E%0A        
-    
 %22%22%22)
-)
 %0A        rv 
@@ -1222,27 +1222,40 @@
 def 
-test_
+get_events_data(self, 
 start
-_
+, 
 end
-(self
 ):%0A 
@@ -1265,42 +1265,237 @@
     
-with open(self.org_file, 'w') as f
+start = totimestamp(datetime.datetime(*start))%0A        end = totimestamp(datetime.datetime(*end))%0A        return self.app.get(%0A            '/events_data?start=%7B0:.0f%7D&end=%7B1:.0f%7D'.format(start, end))%0A%0A    def test_start_end(self)
 :%0A  
@@ -1504,35 +1504,27 @@
     
-    
+sel
 f.write
-(textwrap.dedent
+_org_file
 (%22%22%22
@@ -1528,28 +1528,24 @@
 %22%22%22%0A        
-  
-  
 * Node 1%0A   
@@ -1541,20 +1541,16 @@
  Node 1%0A
-    
         
@@ -1583,28 +1583,24 @@
 ue%3E%0A        
-  
-  
 * Node 2%0A   
@@ -1596,20 +1596,16 @@
  Node 2%0A
-    
         
@@ -1638,28 +1638,24 @@
 ed%3E%0A        
- 
-   
 * Node 3%0A   
@@ -1651,20 +1651,16 @@
  Node 3%0A
-    
         
@@ -1701,17 +1701,12 @@
     
-    
 %22%22%22)
-)
 %0A   
@@ -1794,217 +1794,73 @@
     
-start = totimestamp(datetime.datetime(2012, 10, 20))%0A        end = totimestamp(datetime.datetime(2012, 10, 23))%0A        rv = self.app.get(%0A            '/events_data?start=%7B0:.0f%7D&end=%7B1:.0f%7D'.format(start, end
+rv = self.get_events_data(start=(2012, 10, 20), end=(2012, 10, 23
 ))%0A 

Refactor TestWebEventsData