import transaction
import unittest

from datetime import datetime, timedelta
from pyramid import testing

from budget.models import (Base,
                           DBSession,
                           GcpLineItem)

now = datetime.now()
today = datetime(now.year, now.month, now.day, 0, 0, 0)
yesterday = today - timedelta(days=1)

class TestFilenameTodate(unittest.TestCase):
    def runTest(self):
        from budget.scripts.gcp_billing_import import filename_to_date
        filename = 'gcp-billing-2001-02-03.json'
        result = filename_to_date(filename)
        self.assertEqual(result, datetime(2001, 2, 3, 0, 0, 0))

class TestDateToFilename(unittest.TestCase):
    def runTest(self):
        from budget.scripts.gcp_billing_import import date_to_filename
        result = date_to_filename(datetime(2001, 2, 3))
        self.assertEqual(result, 'gcp-billing-2001-02-03.json')

class TestInsertData(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
        from sqlalchemy import create_engine
        engine = create_engine('sqlite://')
        DBSession.configure(bind=engine)
        Base.metadata.drop_all(engine)
        Base.metadata.create_all(engine)

    def tearDown(self):
        DBSession.remove()
        testing.tearDown()

    def runTest(self):
        import os
        cache_dir = os.path.dirname(__file__) + "/gcp"

        from budget.scripts.gcp_billing_import import insert_data
        filename = 'gcp-billing-2001-01-01.json'
        insert_data(filename, cache_dir)
        result = DBSession.query(GcpLineItem).all()
        self.assertEqual(result[0].cost_amount, 1.234567)

class TestRunWithDate(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
        from sqlalchemy import create_engine
        engine = create_engine('sqlite://')
        DBSession.configure(bind=engine)
        Base.metadata.drop_all(engine)
        Base.metadata.create_all(engine)

    def tearDown(self):
        DBSession.remove()
        testing.tearDown()

    def runTest(self):
        import os
        from budget.scripts.gcp_billing_import import run
        settings = {'cache.dir' : os.path.dirname(__file__)}
        options = {'rundate' : '2001-01-01', 'nocacheupdate' : True}
        run(settings, options)
        result = DBSession.query(GcpLineItem).all()
        self.assertEqual(result[0].cost_amount, 1.234567)

class TestRunWithoutDate(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
        from sqlalchemy import create_engine
        engine = create_engine('sqlite://')
        DBSession.configure(bind=engine)
        Base.metadata.drop_all(engine)
        Base.metadata.create_all(engine)

    #TODO: because of how run() works, we need to write out the test data file
    # to be <6 months old.

    def tearDown(self):
        DBSession.remove()
        testing.tearDown()

    def runTest(self):
        import os
        from budget.scripts.gcp_billing_import import run
        settings = {'cache.dir' : os.path.dirname(__file__)}
        options = {'nocacheupdate' : True}
        run(settings, options)
        result = DBSession.query(GcpLineItem).all()
        self.assertEqual(result[0].cost_amount, 1.234567)

@@ -1,16 +1,26 @@
+import os%0A
 import transacti
@@ -2447,32 +2447,861 @@
 test.TestCase):%0A
+    testpath = os.path.dirname(__file__)+'/gcp'%0A    testfilename = 'gcp-billing-%2504i-%2502i-%2502i.json' %25 (yesterday.year,%0A                                                        yesterday.month,%0A                                                        yesterday.day)%0A    testdata = '''%5B %7B%0A  %22accountId%22 : %22000000-111111-222222%22,%0A  %22lineItemId%22 : %22com.google.cloud/tests/test-service/TestTest%22,%0A  %22description%22 : %22TestTest%22,%0A  %22startTime%22 : %22%25s%22,%0A  %22endTime%22 : %22%25s%22,%0A  %22projectNumber%22 : %22123456789012%22,%0A  %22projectId%22 : %22test-project%22,%0A  %22projectName%22 : %22test-project%22,%0A  %22measurements%22 : %5B %7B%0A    %22measurementId%22 : %22com.google.cloud/tests/test-service/TestTest%22,%0A    %22sum%22 : %2286400%22,%0A    %22unit%22 : %22seconds%22%0A  %7D %5D,%0A  %22cost%22 : %7B%0A    %22amount%22 : %221.234567%22,%0A    %22currency%22 : %22USD%22%0A  %7D%0A%7D %5D''' %25 (yesterday.isoformat(), today.isoformat())%0A%0A
     def setUp(se
@@ -3562,109 +3562,144 @@
     
-#TODO: because of how run() works, we need to
+    import json%0A       
  w
-r
 it
-e out the test data file%0A    # to be %3C6 months old.
+h open(self.testpath+'/'+self.testfilename, 'w+') as fh:%0A            json.dump(json.loads(self.testdata), fh)
 %0A%0A  
@@ -3765,32 +3765,87 @@
 sting.tearDown()
+%0A        os.remove(self.testpath+'/'+self.testfilename)
 %0A%0A    def runTes
@@ -3853,34 +3853,16 @@
 (self):%0A
-        import os%0A
         

fix gcp without date unit test