class MarketService: def __init__(self): self.validations = MarketValidations() self.database = Postgre() self.parser = MarketParser() def get_market_by_registry(self, registry: str): self.validations.is_valid_registry(registry=registry) LOGGER.debug(f'Getting maker by record {registry} service') market = self.database.get_market_by_registry(registry=registry) return self.parser.object_to_json(market=market) def create_market(self, data: dict): self.validations.new_market(data=data) self.validations.is_valid_registry(registry=data.get('registro')) LOGGER.debug('Creating new market service') new_market = Market(**data) market = self.database.insert_market(market=new_market) return self.parser.object_to_json(market=market) def delete_market(self, registry: str): self.validations.is_valid_registry(registry=registry) LOGGER.debug('Deleting market service') self.database.delete_market_by_registry(registry=registry) def get_markets(self, params: list): LOGGER.debug('Getting markets service') query = self.parser.params_to_query(params=params) markets = self.database.get_markets(query=query) return self.parser.markets_to_json(markets=markets) def update_market(self, data: dict, registry: str): self.validations.update_market(data=data) self.validations.is_valid_registry(registry=registry) LOGGER.debug('Updating new market service') market = self.database.get_market_by_registry(registry=registry) market = self.database.update_market(market=market, data=data) return self.parser.object_to_json(market=market)
def __init__(self): self.validations = MarketValidations() self.database = Postgre() self.parser = MarketParser()
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_object_to_json(self): market = Market(**INSTANCE_MARKET_SUCCESS) market_dict = MarketParser.object_to_json(market=market) self.assertEqual(market.id, market_dict.get('id'))
def test_markets_to_json(self): market = list(INSTANCE_MARKET_SUCCESS.values()) markets = [market] market_list = MarketParser.markets_to_json(markets=markets) self.assertEqual(market[0], market_list[0].get('id'))
def test_params_to_query_return_nome_feira(self): query = MarketParser.params_to_query(params=[('nome_feira', 1)]) self.assertEqual(query, '"nome_feira" = \'1\'')
def test_params_to_query_return_bairro(self): query = MarketParser.params_to_query(params=[('bairro', 1)]) self.assertEqual(query, '"bairro" = \'1\'')
def test_params_to_query_return_distrito(self): query = MarketParser.params_to_query(params=[('distrito', 1)]) self.assertEqual(query, '"distrito" = \'1\'')
def test_params_to_query_return_regiao5(self): query = MarketParser.params_to_query(params=[('regiao5', 1)]) self.assertEqual(query, '"regiao5" = \'1\'')
def test_params_to_query_return_empty_string(self): query = MarketParser.params_to_query(params=[('pudim', 1)]) self.assertEqual(query, '')
def test_params_to_query_failure_raise_exception(self): with self.assertRaises(Exception): MarketParser.params_to_query(params=[1, 2, 3, 4, 5])
def test_number_from_csv_to_int_successfully(self): number = MarketParser.number_from_csv_to_int('666.000') self.assertEqual(number, '666')
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)
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()