import sys
import datetime
import unittest
from smartsheetclient import SmartsheetClient, SheetInfo, Column, CellTypes
import logging
logging.basicConfig(filename='tests.log', level=logging.DEBUG)

api_token = 'UNSET'


class CreateSheetTest(unittest.TestCase):


    def setUp(self):
        self.logger = logging.getLogger()
        self.client = SmartsheetClient(api_token, logger=self.logger)

        time_str = str(datetime.datetime.utcnow())
        self.new_sheet_name = 'test-new-sheet-%s' % time_str

        col_1 = Column("Col 1 - TextNumber", type=CellTypes.TextNumber,
                primary=True)
        col_2 = Column("Col 2 - Date", type=CellTypes.Date)
        col_3 = Column("Col 3 - Picklist" , type=CellTypes.Picklist,
                options=['Yes', 'No', 'Maybe'])
        col_4 = Column("Col 4 - Checkbox", type=CellTypes.Checkbox)
        col_5 = Column("Col 5 - ContactList", type=CellTypes.ContactList)
        self.columns = [col_1, col_2, col_3, col_4, col_5]
        # TODO: Add some system or autonumber columns.
        self.logger.info("########### Test uses Sheet: %s ############",
                                self.new_sheet_name)

    def test_create_sheet(self):
        '''
        Test the creation of a new sheet.
        '''
        si = self.client.createSheet(self.new_sheet_name, self.columns,
                location='')

        self.assertIsNotNone(si) 
        self.assertIsInstance(si, SheetInfo)
        self.assertEqual(si.name, self.new_sheet_name)
        self.assertIsNotNone(si.permalink)

        # Make sure it shows up on the SheetList by name.
        sheet_list = self.client.fetchSheetInfoByName(self.new_sheet_name)
        self.assertIsNotNone(sheet_list)
        self.assertEqual(1, len(sheet_list))
        self.assertEqual(si, sheet_list[0])

        # Fetch the Sheet and make sure it corresponds to what we meant to create.
        sheet = si.loadSheet()
        self.assertIsNotNone(sheet)
        self.assertEqual(sheet.name, self.new_sheet_name)
        self.assertTrue(len(sheet.columns) == len(self.columns))
        self.assertTrue(len(sheet.rows) == 0)
        self.assertEqual(sheet.permalink, si.permalink)

        for sheet_col, src_col in zip(sheet.columns, self.columns):
            self.assertEqual(sheet_col.title, src_col.title)

    

if __name__ == '__main__':
    if len(sys.argv) != 2:
        sys.exit("Error, must supply path to token file")
    api_token_file = sys.argv[1]

    with file(api_token_file, 'r') as fh:
        api_token = fh.read()
        api_token = api_token.strip()
    del sys.argv[1] 
    unittest.main()


@@ -111,16 +111,39 @@
 ellTypes
+, SmartsheetClientError
 %0Aimport 
@@ -504,18 +504,25 @@
 test
--new-she
+SheetCreateDel
 et
+e
 -%25s'
@@ -1216,16 +1216,27 @@
 _create_
+and_delete_
 sheet(se
@@ -1287,21 +1287,44 @@
 f a new 
-s
+S
 heet
+ and the deletion of it
 .%0A      
@@ -2383,16 +2383,503 @@
 e)%0A%0A    
+    # Delete the sheet and make sure it is gone.%0A        sheet_id = sheet.id%0A        sheet_permalink = sheet.permalink%0A        sheet.delete()%0A        with self.assertRaises(SmartsheetClientError):%0A            self.client.fetchSheetById(sheet_id)%0A        sheet_list = self.client.fetchSheetInfoByName(self.new_sheet_name)%0A        self.assertTrue(len(sheet_list) == 0)%0A        sheet_info = self.client.fetchSheetInfoByPermalink(sheet_permalink)%0A        self.assertTrue(sheet_info is None)%0A
 %0A%0Aif __n

Add support for testing Sheet deletion