def main(output_name, pretty, strict, log, output_format, csv): """A simple command line tool to convert CSV to other formats""" try: c = Converter(csv_file=csv, output_format=output_format, output_name=output_name, pretty=pretty, loglevel=log, strict=strict) c.convert() click.echo("Total Data Parsed: {}".format(c.get_total_data())) except CSVNotFound: click.echo("{} Not Found. Are you in the right directory?".format(csv)) except FormatterNotFound: click.echo( "{} is not supported yet. Can you add support to this format?". format(output_format)) except ConversionError: click.echo( "There was a problem in converting and writing to the output file." ) click.echo("This is not good") click.echo( "I suggest you enable debugging and send the logs to author") except ValidationError: click.echo( "Validation Failed! You might not want to use the --strict flag") except Exception as e: print e
def test_convert_data(self): """Method to test the conversion creates the right file""" c = Converter(self.csv, ('json', ), 'data', True) c.convert() self.assertTrue(os.path.exists('data.json')) c = Converter(self.csv, ('xml', ), 'data', True) c.convert() self.assertTrue(os.path.exists('data.xml'))
def test_parse_data_keys(self): """Method to test the keys of the resulting dictionary in c.data""" c = Converter(self.csv, 'json', 'data', True) for d in c.data: self.assertEqual( d.keys(), ['name', 'uri', 'phone', 'contact', 'stars', 'address'])
def test_converter_parameters(self): """Method to test if the converter is setup with the right params""" c = Converter(self.csv, 'json', 'data', True) self.assertEqual(c.csv_file, self.csv) self.assertEqual(c.output_format, 'json') self.assertEqual(c.output_name, 'data') self.assertEqual(c.pretty, True)
def test_parse_data(self): """Method to test the actual data""" c = Converter(self.csv, 'json', 'data', True) for d in c.data: self.assertEqual(d['name'], "Jürgen-Gehringer".decode('utf-8')) self.assertEqual(d['address'], "63847 Lowe Knoll, East Maxine, WA 97030-4876") self.assertEqual(d['stars'], '5') self.assertEqual(d['contact'], "Dr. Sinda Wyman") self.assertEqual(d['phone'], '1-270-665-9933x1626') self.assertEqual(d['uri'], 'http://www.paucek.com/search.htm')
def test_convert_data_format(self): c = Converter(self.csv, ('json', ), 'data', True) c.convert() try: with open('data.json') as f: json.load(f) except ValueError: self.fail("Invalid Json Format") c = Converter(self.csv, ('xml', ), 'data', True) c.convert() try: with open('data.xml') as f: xml_string = f.read() xml = ET.fromstring(xml_string) except xml.etree.ElementTree.ParseError: self.fail("Invalid xml Format")
def test_get_total_data(self): """Method to test get_total_data method of Converter""" c = Converter(self.csv, 'json', 'data', True) self.assertEqual(c.get_total_data(), 1)
def test_converter_data_length(self): """Method to test if the converter has the right number of data after parsing the csv""" c = Converter(self.csv, 'json', 'data', True) self.assertEqual(len(c.data), 1)