class ERepDBInterface(object): ''' classdocs ''' def __init__(self): ''' Constructor ''' self.csv = CSV() self.eRepDB = eRepLokalDBManager() self.currentQuery = None self.currentQueryResult = None def executeQuery(self, query): self.currentQuery = query self.currentQueryResult = self.eRepDB.selectorQuery(self.currentQuery) def printCurQueryResult(self): print "++++++++++++++++++++++" print "executing query:\n %s" %self.currentQuery print "----------------------" print self.currentQueryResult def writeCurQueryResult2CSV(self): self.csv.writeCSVfromSQLResult(self.currentQueryResult)
class TestCSV(unittest.TestCase): def setUp(self): self.csv = CSV() def test_should_all_lines_have_same_quantity_fields(self): field_number = len(self.csv.csv_table[0]) for line in self.csv.csv_table[1:]: self.assertEquals(len(line), field_number) def test_first_line_should_have_column_names(self): self.assertEquals(', '.join(self.csv.column_names), '"Estado", "Localização", "URL"') def test_first_field_should_be_state(self): states = ['Acre', 'Alagoas', 'Amapá', 'Amazonas', 'Bahia', 'Ceará', 'Distrito Federal', 'Espírito Santo', 'Goiás', 'Maranhão', 'Mato Grosso', 'Mato Grosso do Sul', 'Minas Gerais', 'Pará', 'Paraíba', 'Paraná', 'Pernambuco', 'Piauí', 'Rio de Janeiro', 'Rio Grande do Norte', 'Rio Grande do Sul', 'Rondônia', 'Roraima', 'Santa Catarina', 'São Paulo', 'Sergipe', 'Tocantins'] for line in self.csv.csv_table[1:]: self.assertIn(line[0][1:-1], states) def test_last_field_should_be_url_http(self): for line in self.csv.csv_table[1:]: self.assertRegexpMatches(line[2], r'^"http://www.bicicletada.org/') def test_should_save_csv_as_file(self): self.csv.save_csv('XXXXXXXXXXXXXXXXX_my_csv_test_file.csv') self.assertTrue(isfile('XXXXXXXXXXXXXXXXX_my_csv_test_file.csv')) remove('XXXXXXXXXXXXXXXXX_my_csv_test_file.csv')
def test_get_cell_value_empty_cell(self): """ Tests the ability to look up a value at a specified row and column when the cell is empty """ new_csv = CSV("empty_cells.csv") value_at_index = new_csv.get_cell_value(1,1) self.assertEqual(value_at_index, '')
def test_get_types_normal_input(self): """ Tests get_type on an input file containing only strings, commas, and integer numerics """ new_csv = CSV("example.csv") types = new_csv.get_types() self.assertEqual(new_csv.type_list, ['String', 'String', 'String', 'Numeric'])
def test_get_cell_value_empty_cell(self): """ Tests the ability to look up a value at a specified row and column when the cell is empty """ new_csv = CSV("empty_cells.csv") value_at_index = new_csv.get_cell_value(1, 1) self.assertEqual(value_at_index, '')
class TestCSV(unittest.TestCase): def setUp(self): self.a_csv = CSV("C:/Users/ASUS/python_csv_class/demo.csv") def test_is_csv(self): a_csv = CSV("C:/Users/ASUS/python_csv_class/demo.csv") result = self.a_csv.is_csv() self.assertTrue(result) def test_get_head(self): if_head = self.a_csv.get_head() self.assertTrue(if_head)
def test_create_instance(self): """ Tests the basic functionality: that the CSV init function creates an instance of the CSV class. """ new_csv = CSV("example.csv") self.assertIsInstance(new_csv, CSV)
def __init__(self): ''' Constructor ''' self.csv = CSV() self.eRepDB = eRepLokalDBManager() self.currentQuery = None self.currentQueryResult = None
def writeNMEA(self, mount, file, head, data): """Write NMEA message in a file.""" if CSV.mount(mount): if head != None and not file in os.listdir("/" + mount): CSV.write(head, mount, file) if isinstance(data, list): CSV.write(data, mount, file, mode="a") elif isinstance(data, dict): CSV.writeFromDictionnary(data)
def test_parse_csv_normal_input(self): """ Tests parse_csv on an input file containing only strings, commas, and integer numerics """ new_csv = CSV("example.csv") self.assertEqual( new_csv.result_csv, [['John D', '120 any st.', '"Anytown WW"', '08123'], ['Andrew P', '114 Sansome st.', '"San Francisco CA"', '94105'], ['Morgan R', '905 Green st.', '"Chicago IL"', '68100']])
def test_combine_double_quotes_commas(self): """ Tests combine_double_quotes on an input file that contains commas, the delimeter used to split the CSV input file """ new_csv = CSV("example.csv") self.assertEqual( new_csv.result_csv, [['John D', '120 any st.', '"Anytown WW"', '08123'], ['Andrew P', '114 Sansome st.', '"San Francisco CA"', '94105'], ['Morgan R', '905 Green st.', '"Chicago IL"', '68100']])
def test_combine_double_quotes_split_quotes(self): """ Tests combine_double_quotes on an input file that contains quotes spread across lines """ new_csv = CSV("example2.csv") self.assertEqual(new_csv.result_csv, [['"For whom the bells toll"', '0', '0'], ['"Bring me some shrubbery"', '2', '3'], ['"Once upon a time"', '5', '6'], ['"\'It\'s just a flesh wound."', '8', '9']])
def test_empty_instance(self): """ Tests that the CSV init function creates an instance of the CSV class when given an empty file, empty.csv. For the purposes of this class, we assume that an empty CSV is still a valid CSV; since the parse_csv function allows us to access individual rows and columns, we can add methods to add values to specific rows and columns later if desired. """ new_csv = CSV("empty.csv") self.assertIsInstance(new_csv, CSV)
def test_parse_csv_empty_cells(self): """ Tests parse_csv on an input file containing strings, numerics, and empty cells denoted by spaces between commas For example: Andrew P,,94105 """ new_csv = CSV("empty_cells.csv") self.assertEqual(new_csv.result_csv, [['John D', '', '08123'], ['Andrew P', '', '94105'], ['Morgan R', '', '68100']])
def test_is_csv(self): a_csv = CSV("C:/Users/ASUS/python_csv_class/demo.csv") result = self.a_csv.is_csv() self.assertTrue(result)
def setUp(self): self.a_csv = CSV("C:/Users/ASUS/python_csv_class/demo.csv")
def test_get_cell_value_normal_input(self): """Tests the ability to look up a value at a specified row and column""" new_csv = CSV("example.csv") value_at_index = new_csv.get_cell_value(1, 2) self.assertEqual(value_at_index, '"San Francisco CA"')
self.CAN_SAVE = can_save # additional config options for database connection or fukebane(s) self.HAS_CONFIG = has_config def parse(self, addressbook, conf): '''load file / open database connection''' # XXX: set addressbook in __init__? self.ab = addressbook pass def add(self, name, birthday): '''save new birthday to file/database (only if CAN_SAVE == true)''' pass def save_config(self, conf): '''record current entries in config menu into configuration''' pass def create_config(self, vbox, conf): '''create additional pygtk config in config menu''' pass from csv import CSV from evolution import Evolution from lightning import Lightning from mysql import MySQL from sunbird import Sunbird mysql_db = MySQL() DATABASES = [CSV(), Evolution(), Lightning(), mysql_db, Sunbird()]
def test_get_types_decimal_input(self): """Tests get_types on an input file containing negative numbers""" new_csv = CSV("float_numbers.csv") types = new_csv.get_types() self.assertEqual(new_csv.type_list, ['String', 'Numeric', 'Numeric'])
def test_get_types_empty_input(self): """Tests get_types on an empty file""" new_csv = CSV("empty.csv") types = new_csv.get_types() self.assertEqual(new_csv.type_list, ['String'])
def test_parse_csv_empty_input(self): """Tests parse_csv on an empty file""" new_csv = CSV("empty.csv") self.assertEqual(new_csv.result_csv, [])
def test_get_types_empty_cells(self): """Tests get_types on an input file containing empty cells""" new_csv = CSV("empty_cells.csv") types = new_csv.get_types() self.assertEqual(new_csv.type_list, ['String', 'String', 'Numeric'])
def setUp(self): self.csv = CSV()
def test_get_cell_value_normal_input(self): """Tests the ability to look up a value at a specified row and column""" new_csv = CSV("example.csv") value_at_index = new_csv.get_cell_value(1,2) self.assertEqual(value_at_index, '"San Francisco CA"')