def test_output_file_permission(self): """ Check that the exporter raises an IOError when given a wrong output file path (directory or permission denied) """ # Export results to a directory self.assertIsNotNone(ExportToJson.export_contacts([self.contact_test], [1], "/tmp")) self.assertRaises(IOError, open, "/tmp") # Export results to a file without permissions self.assertIsNotNone(ExportToJson.export_contacts([self.contact_test], [1], "/dev/test.out")) self.assertRaises(IOError, open, "/dev/test.out")
def test_output_file_permission(self): """ Check that the exporter raises an IOError when given a wrong output file path (directory or permission denied) """ # Export results to a directory self.assertIsNotNone( ExportToJson.export_contacts([self.contact_test], [1], "/tmp")) self.assertRaises(IOError, open, "/tmp") # Export results to a file without permissions self.assertIsNotNone( ExportToJson.export_contacts([self.contact_test], [1], "/dev/test.out")) self.assertRaises(IOError, open, "/dev/test.out")
def test_output_file(self): """ Validate the file creation, and compare the file content to what is expected """ result_file_path = tempfile.gettempdir() + "result.out" ExportToJson.export_contacts([self.contact_test], [42], result_file_path) # Assert that the result file as been created and exist self.assertTrue(os.path.isfile(result_file_path)) # Get the file's content with open(result_file_path, "r") as f: file_content = f.read() # Compare the JSON file's content with the expected output self.assertEqual(file_content, JSON_OUTPUT)
def test_json_output(self): """ Check that the result JSON is correctly formatted and as expected. Line-indent: 2 spaces """ json_result = ExportToJson.export_contacts([self.contact_test], [42]) self.assertEqual(json_result, JSON_OUTPUT)
def test_json_output(self): """ Check that the result JSON is correctly formatted and as expected. Line-indent: 2 spaces """ json_result = ExportToJson.export_contacts([self.contact_test], [42]) self.assertEqual(json_result, JSON_OUTPUT)
def test_output_file(self): """ Validate the file creation, and compare the file content to what is expected """ result_file_path = tempfile.gettempdir() + "result.out" ExportToJson.export_contacts([self.contact_test], [42], result_file_path) # Assert that the result file as been created and exist self.assertTrue(os.path.isfile(result_file_path)) # Get the file's content with open(result_file_path, "r") as f: file_content = f.read() # Compare the JSON file's content with the expected output self.assertEqual(file_content, JSON_OUTPUT)
def test_empty_json_output(self): """ Check that the exporter accepts empty arrays and that the result is correctly formatted. """ expected_result = '{\n\ "entries": [],\n\ "errors": []\n\ }' json_result = ExportToJson.export_contacts([], []) self.assertEqual(json_result, expected_result)
def test_empty_json_output(self): """ Check that the exporter accepts empty arrays and that the result is correctly formatted. """ expected_result = '{\n\ "entries": [],\n\ "errors": []\n\ }' json_result = ExportToJson.export_contacts([], []) self.assertEqual(json_result, expected_result)
with open(input_file_path) as input_file: for index, line in enumerate(input_file): try: # Remove line breaks at the end of the entry contact = parser.parse(line.rstrip()) # The parser found a valid contact, we add it to the final list extracted_contacts.append(contact) except ContactParseError, e: logging.warn(e) # We keep track of the line index in case of an error parse_errors_indexes.append(index) except Exception, e: logging.error(e) # Export the results and errors indexes to JSON ExportToJson.export_contacts(extracted_contacts, parse_errors_indexes, output_file_path) logging.info("nb contacts extracted: %s" % len(extracted_contacts)) logging.info("nb invalid input: %s" % len(parse_errors_indexes)) except IOError, e: logging.error(e) def main(): """ Main program entry point. Defines and get the program's arguments and then execute the program's logic calling rolodex() function """ verbose = ["DEBUG", "INFO", "WARNING", "ERROR", "FATAL"] parser = argparse.ArgumentParser(description="Rolodex parses the input file and extract contact information. "