def export(args): """ Run logic associated with exportation subparser. :params args: (ArgumentParser, req) Command line args that tell us what pattern and database to use. :raises ValueError: if there's an issue with flags (bad filetype, etc) """ LOGGER.info( 'Exporting ids=%s and scalars=%s from database_type=%s ' 'to target=%s.', args.ids, args.scalars, args.database_type, args.target) error_message = [] error_message.extend(_check_common_args(args=args)) if not args.ids: error_message.append('Require one or more record ids to export.') if not args.scalars: error_message.append('Require one or more scalar names to export.') if error_message: msg = "\n".join(error_message) LOGGER.error(msg) raise ValueError(msg) utils.export(factory=_make_factory(args=args), id_list=args.ids.split(','), scalar_names=args.scalars.split(','), output_file=args.target, output_type=args.export_type)
def test_export_non_existent_scalar_csv(self): """Test export for a non existent scalar returns no scalars.""" export(factory=self.create_dao_factory(), id_list=['child_1'], scalar_names=['bad-scalar'], output_type='csv', output_file=self.test_file_path.name) with open(self.test_file_path.name, 'r') as csvfile: reader = csv.reader(csvfile) rows = [row for row in reader] self.assertEqual(len(rows), 1) self.assertEqual(rows[0], ['id', 'bad-scalar'])
def test_export_one_scalar_csv_good_input(self): """Test export one scalar correctly to csv from a sql database.""" factory = self.create_dao_factory() populate_database_with_data(factory.create_record_dao()) export(factory=factory, id_list=['spam'], scalar_names=['spam_scal'], output_type='csv', output_file=self.test_file_path.name) with open(self.test_file_path.name, 'r') as csvfile: reader = csv.reader(csvfile) rows = [row for row in reader] self.assertEqual(rows[0], ['id', 'spam_scal']) # 10 is stored but 10.0 is retrieved due to SQL column types self.assertAlmostEqual(float(rows[1][1]), 10)
def test_export_two_scalar_csv_good_input(self): """Test exporting two scalars & runs correctly to csv from sql.""" factory = self.create_dao_factory() populate_database_with_data(factory.create_record_dao()) export(factory=factory, id_list=['spam3', 'spam'], scalar_names=['spam_scal', 'spam_scal_2'], output_type='csv', output_file=self.test_file_path.name) with open(self.test_file_path.name, 'r') as csvfile: reader = csv.reader(csvfile) rows = [row for row in reader] self.assertEqual(rows[0], ['id', 'spam_scal', 'spam_scal_2']) self.assertEqual(rows[1], ['spam3', '10.5', '10.5']) # AlmostEqual doesn't work with lists, but we expect floats from # SQL, hence this workaround self.assertEqual(rows[2], ['spam', '10.0', '200.0'])
def test_export_csv_good_input_mocked(self, mock): """ Test export with mocked _csv_export() and good input. Test export with of one scalar from sql database to a csv file. Mock _export_csv() so we don't actually write to file. """ factory = self.create_dao_factory() populate_database_with_data(factory.create_record_dao()) scalars = ['spam_scal'] export(factory=factory, id_list=['spam_scal'], scalar_names=scalars, output_type='csv', output_file=self.test_file_path.name) self.assertTrue(mock.called) self.assertEqual(mock.call_count, 1) _, kwargs = mock.call_args self.assertEqual(kwargs['scalar_names'][0], scalars[0])
def test_export_csv_bad_input_mocked(self, mock): """ Test export with mocked _csv_export() and bad input. Test export with of one scalar from sql database to a csv file. Mock _export_csv() so we don't actually write to file. Bad input in this case is an output_type that is not supported. """ factory = self.create_dao_factory() populate_database_with_data(factory.create_record_dao()) scalars = ['spam_scal'] with self.assertRaises(ValueError) as context: export(factory=factory, id_list=['spam'], scalar_names=scalars, output_type='joes_output_type', output_file=self.test_file_path.name) self.assertIn( 'Given "joes_output_type" for output_type and it must ' 'be one of the following: csv', str(context.exception)) self.assertEqual(mock.call_count, 0)