def test_substitute_country_data(self): SQLite3Mock.fetchall_returns = [sqlite3.Error("Error")] entity = CityEntity(("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l")) result = LocationDatabase.substitute_country_data([entity], SQLite3Mock) self.assertEqual(SQLite3Mock.execute_calls, [("SELECT * FROM country WHERE abbreviation = ?", ("a",))]) self.assertIsInstance(result[0], CityEntity)
def find_matching_landmarks(cls, data): """ Looks in the database for landmarks matching datastring :param data: string we want to check against landmarks :type data: str :return: list of landmarks :rtype: list """ database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute( 'SELECT * FROM landmark WHERE resource like ?', (data + '%',) ).fetchall() entities = LocationDatabase.hydrate(rows, LandmarkEntity) entities = \ LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() except sqlite3.Error: database.close() return return entities
def test_substitute_country_data(self): SQLite3Mock.fetchall_returns = [sqlite3.Error('Error')] entity = CityEntity( ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l')) result = \ LocationDatabase.substitute_country_data([entity], SQLite3Mock) self.assertEqual(SQLite3Mock.execute_calls, [('SELECT * FROM country WHERE abbreviation = ?', ('a', ))]) self.assertIsInstance(result[0], CityEntity)
def get_postal_code_data(cls, data): """ If this looks like a postal code, we try to get its info :param data: a possibly detected zip code :type data: str :return: possible locations for this zip code :rtype: list """ plus_postal_code = '-' in data if plus_postal_code: prefix = data.split('-')[0] database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute( 'SELECT * FROM city WHERE postal_code = ?', (data,) ).fetchall() entities = LocationDatabase.hydrate(rows, CityEntity) except sqlite3.Error: database.close() return None if plus_postal_code: try: rows = cursor.execute( 'SELECT * FROM city WHERE postal_code = ?', (prefix,) ).fetchall() prefix_entities = LocationDatabase.hydrate(rows, CityEntity) entities.extend(prefix_entities) except sqlite3.Error: database.close() return None if not entities: database.close() return None entities = LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() return entities
def find_matching_landmarks(cls, data): """Looks in the database for landmarks matching datastring""" database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute( 'SELECT * FROM landmark WHERE resource like ?', (data + '%', )).fetchall() entities = LocationDatabase.hydrate(rows, LandmarkEntity) entities = \ LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() except sqlite3.Error: database.close() return return entities
def get_postal_code_data(cls, data): """If this looks like a postal code, we try to get its info""" plus_postal_code = '-' in data if plus_postal_code: prefix = data.split('-')[0] database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute('SELECT * FROM city WHERE postal_code = ?', (data, )).fetchall() entities = LocationDatabase.hydrate(rows, CityEntity) except sqlite3.Error: database.close() return None if plus_postal_code: try: rows = cursor.execute( 'SELECT * FROM city WHERE postal_code = ?', (prefix, )).fetchall() prefix_entities = LocationDatabase.hydrate(rows, CityEntity) entities.extend(prefix_entities) except sqlite3.Error: database.close() return None if not entities: database.close() return None entities = LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() return entities