def test_number_from_csv_to_int_without_number_successfully(self): number = MarketParser.number_from_csv_to_int('') self.assertEqual(number, 'S/N')
def test_number_from_csv_to_int_distinct_number_successfully(self): for number in DISTINCT_NUMBERS: parsed_number = MarketParser.number_from_csv_to_int(number) self.assertEqual(parsed_number, number)
def test_number_from_csv_to_int_successfully(self): number = MarketParser.number_from_csv_to_int('666.000') self.assertEqual(number, '666')
print('Loading data from csv file') with open('datasource/DEINFO_AB_FEIRASLIVRES_2014.csv') as file: csv_reader = csv.reader(file) next(csv_reader, None) print('csv file loaded, starting insertion...') for row in csv_reader: if len(row) < 17: row.append('') row[ID_INDEX] = int(row[ID_INDEX]) row[LONG_INDEX] = int(row[LONG_INDEX]) row[LAT_INDEX] = int(row[LAT_INDEX]) row[SETCENS_INDEX] = int(row[SETCENS_INDEX]) row[AREAP_INDEX] = int(row[AREAP_INDEX]) row[CODDIST_INDEX] = int(row[CODDIST_INDEX]) row[CODSUBPREF_INDEX] = int(row[CODSUBPREF_INDEX]) row[NUMERO_INDEX] = MarketParser.number_from_csv_to_int( number=row[NUMERO_INDEX]) try: cursor.execute( 'INSERT INTO markets VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,' ' %s, %s, %s, %s, %s, %s, %s, %s)', row) except UniqueViolation: connection.rollback() print(f'Row duplicated, continue | {row}') print('Sync serial id') cursor.execute( 'SELECT SETVAL((SELECT PG_GET_SERIAL_SEQUENCE(\'"markets"\', \'id\')),' ' (SELECT (MAX("id") + 1) FROM "markets"), FALSE);') print('Insertion done') connection.commit() print('Committed changes') cursor.close()