def test_treasurydirect_parsing_past_valuation_w_mock(self, mock_response, mock_page):
        test_params = {'Series': 'EE', 'SerialNumber': 'R114463707EE', 'IssueDate': '08/1994', 'Denomination': '200'}
        test_valuation_date = '07/2015'

        self.assertEqual(td.parse_url(params = test_params, valuation_date = test_valuation_date), ['R114463707EE', '07/2015', 'EE', '$200', '08/1994', '08/2015', '08/2024', '$100.00', '$129.04', '4.00%', '08/1994', '$229.04', 0.04039191319543778])

        mock_response.assert_called_once_with('http://www.treasurydirect.gov/BC/SBCPrice', data={'RedemptionDate': '07/2015', 'Series': 'EE', 'SerialNumber': 'R114463707EE', 'Denomination': '200', 'IssueDate': '08/1994', 'btnAdd.x': 'CALCULATE'})

        self.assertEqual(mock_page.called, True)
def main(opts):

    """
    This function handles the execution of the script
    opts:  Namespace(delimeter=',', header=True, input_csv_filepath='somefilepath', output='Bond_Valuations.cvs', v=False, valuation_date='08/2015')
    """

    if opts.v:
        logging.basicConfig(level=logging.INFO)

    bonds_list = csv_parser.load_from_csv(filepath = opts.input_csv_filepath, header = opts.header, delim = opts.delimeter)

    f = open(opts.output, 'a')

    logging.info("Opening output file:  %s", opts.output)

    writer = csv.writer(f)

    headers = ['Number', 'SerialNum', 'ValuationDate', 'SeriesType', 'Denomination', 'IssueDate', 'NextAccrualDate', 'FinalMaturity', 'IssuePrice', 'TotalInterest', 'InterestRate', 'YtdInterest', 'Value', 'CAGR']

    for n, bond in enumerate(bonds_list, 1):
        try:
            valuation_results = td.parse_url(params = bond, valuation_date = opts.valuation_date)


            if n == 1 and f.tell() == 0:
                writer.writerow(headers)

            output = [n] + valuation_results

            logging.info('Writing to file:  %s', output)
            writer.writerow(output)

        except Exception as e:
            logging.warning("Issue writing row: %s", n, bond)
            logging.warning("Exception: %s", e)
            raise

    f.close()
    logging.info("Completed output to file: %s", opts.output)
    def test_treasurydirect_parsing_past_valuation_live(self):
        test_params = {'Series': 'EE', 'SerialNumber': 'R114463707EE', 'IssueDate': '08/1994', 'Denomination': '200'}
        test_valuation_date = '07/2015'

        self.assertEqual(td.parse_url(params = test_params, valuation_date = test_valuation_date), ['R114463707EE', '07/2015', 'EE', '$200', '08/1994', '08/2015', '08/2024', '$100.00', '$129.04', '4.00%', '$5.28', '$229.04', 0.04039191319543778])
    def test_treasurydirect_parsing_current_valuation_live(self):
        test_params = {'Series': 'EE', 'SerialNumber': 'C777535063EE', 'IssueDate': '08/1989', 'Denomination': '100'}
        test_valuation_date = '08/2015'

        self.assertEqual(td.parse_url(params = test_params, valuation_date = test_valuation_date), ['C777535063EE', '08/2015', 'EE', '$100', '08/1989', '02/2016', '08/2019', '$50.00', '$127.00', '4.00%', '$6.88', '$177.00', 0.049789363935413666])