class TestTimesheet(unittest.TestCase):
    def setUp(self):
        self.timesheet = Timesheet()

    def testTimecardsDefaultValue(self):
        self.assertEqual(self.timesheet.timecards, {})

    def testGetTimecardReturnsATimecardObject(self):
        timecard = self.timesheet.get_timecard("2015-06-01")
        self.assertTrue(isinstance(timecard, Timecard))

    def testGetTimecardReturnsTimecardWithCorrectDate(self):
        timecard = self.timesheet.get_timecard("2015-06-01")
        self.assertEqual(timecard.date,
                datetime.strptime("2015-06-01", "%Y-%m-%d"))

    def testGetTimecardAddsTimecardToTimecardsDict(self):
        self.timesheet.get_timecard("2015-07-22")
        self.assertEqual(len(self.timesheet.timecards), 1)

    def testRepeatedCallsToGetTimecardReturnSameTimecard(self):
        tc1 = self.timesheet.get_timecard("2015-07-22")
        tc2 = self.timesheet.get_timecard("2015-07-22")

        self.assertEqual(tc1, tc2)

    def testDatesReturnsEmptyListWithoutTimecards(self):
        self.assertEqual(self.timesheet.dates(), [])

    def testDatesReturnsLengthTwoListWithTwoDifferentTimecards(self):
        self.timesheet.get_timecard("2015-06-01")
        self.timesheet.get_timecard("2015-06-02")

        self.assertEqual(len(self.timesheet.dates()), 2)

    def testDatesReturnsLengthOneListWithTwoSameTimecards(self):
        self.timesheet.get_timecard("2015-06-01")
        self.timesheet.get_timecard("2015-06-01")

        self.assertEqual(len(self.timesheet.dates()), 1)

    def testToCsvReturnsCsvFormattedDataForOneTimecard(self):
        item = {
                "time_spec": "10:00-18:30",
                "notes": "did stuff",
                }
        timecard = self.timesheet.get_timecard("2015-06-01")
        timecard.add(**item)

        timesheet_csv = self.timesheet.to_csv()
        expected = "Date work,Description,Tickets,Hours\r\n"
        expected += "2015-06-01,did stuff,,8.50\r\n"
        self.assertEqual(timesheet_csv, expected)

    def testToCsvReturnsCsvFormattedDataForTwoTimecards(self):
        first_item = {
                "time_spec": "10:00-18:30",
                "notes": "did stuff",
                }
        first_timecard = self.timesheet.get_timecard("2015-06-01")
        first_timecard.add(**first_item)

        second_item = {
                "time_spec": "19:00-20:00",
                "notes": "did more stuff",
                "tickets": "#123",
                }
        second_timecard = self.timesheet.get_timecard("2015-06-02")
        second_timecard.add(**second_item)

        timesheet_csv = self.timesheet.to_csv()
        expected = "Date work,Description,Tickets,Hours\r\n"
        expected += "2015-06-01,did stuff,,8.50\r\n"
        expected += "2015-06-02,did more stuff,#123,1.00\r\n"
        self.assertEqual(timesheet_csv, expected)
import os.path

from timesheets.timesheet import Timesheet

parser = argparse.ArgumentParser(description="Convert timesheets to CSV")
parser.add_argument("--timesheet", type=str, required=True, help="timesheet file name")
args = parser.parse_args()
timesheet_yml_fname = args.timesheet

yml_fh = open(timesheet_yml_fname)
timesheet_items = yaml.load_all(yml_fh)

timesheet = Timesheet()
total_hours = 0.0
for item in timesheet_items:
    timecard = timesheet.get_timecard(item.pop("date"))
    timecard.add(**item)

timesheet_csv = timesheet.to_csv()
timesheet_basename = os.path.basename(timesheet_yml_fname)
timesheet_stem = os.path.splitext(timesheet_basename)[0]
timesheet_csv_fname = timesheet_stem + ".csv"

csv_fh = open(timesheet_csv_fname, "w")
csv_fh.write(timesheet_csv)
csv_fh.close()

print "Total hours: {0:.2f}".format(timesheet.total_hours_worked())

# vim: expandtab shiftwidth=4 softtabstop=4