def test_basic_search(self): """ TESTING IF redis will return all 'Locations' which have words within starting with 'a' COMPARING WITH data retrieved from data loaded into MySQL from raw file 'film_locations_in_san_francisco.csv' which was downloaded from site @ https://data.sfgov.org/Culture-and-Recreation/Film-Locations-in-San-Francisco/yitu-d5am? The comparison data was retrieved from MySQL using the following query: SELECT Locations FROM Locations WHERE Locations REGEXP '[[:<:]]a' ORDER BY Locations ASC """ basepath = os.path.dirname(__file__) filepath = os.path.abspath(os.path.join(basepath, "../app", "data", self.mysql_results_file_for_A)) f = open(filepath, 'r') rStore = RedisStore(current_app.config['REDIS_AUTOCOMPLETE_SORTED_SET'], current_app.config['REDIS_HOSTNAME'], current_app.config['REDIS_PORT'], current_app.config['REDIS_DB'], current_app.config['REDIS_PASSWORD']) mysql_locations_results = f.readlines() mysql_locations_results = [mysql_locations_result.strip(' \t\n\r') for mysql_locations_result in mysql_locations_results] redis_results = rStore.search('a') redis_locations_results = [redis_result['Locations'] for redis_result in redis_results] redis_locations_results = [redis_locations_result.encode('ascii').strip(' \t\n\r') for redis_locations_result in redis_locations_results] redis_locations_results = sorted(redis_locations_results) self.assertEquals(set(mysql_locations_results),set(redis_locations_results))
def test_search(self): basepath = os.path.dirname(__file__) filepath = os.path.abspath(os.path.join(basepath, "../app", "data", self.mysql_results_file_for_Mar)) f = open(filepath, 'r') rStore = RedisStore(current_app.config['REDIS_AUTOCOMPLETE_SORTED_SET'], current_app.config['REDIS_HOSTNAME'], current_app.config['REDIS_PORT'], current_app.config['REDIS_DB'], current_app.config['REDIS_PASSWORD']) mysql_locations_results = f.readlines() mysql_locations_results = [mysql_locations_result.strip(' \t\n\r') for mysql_locations_result in mysql_locations_results] redis_results = rStore.search('Mar') redis_locations_results = [redis_result['Locations'] for redis_result in redis_results] redis_locations_results = [redis_locations_result.encode('ascii').strip(' \t\n\r') for redis_locations_result in redis_locations_results] redis_locations_results = sorted(redis_locations_results) self.assertEquals(set(mysql_locations_results),set(redis_locations_results))
def get(self): """ :queryparam - term Word prefix, whole word, or phrase that is passed in an queried upon RETURNS list of results """ search_term = request.args.get('term').replace ("'", " ") response = [] # Wrap access to Redis in try/catch to insure against unavailability for any reason try: rStore = RedisStore(current_app.config['REDIS_AUTOCOMPLETE_SORTED_SET'], current_app.config['REDIS_HOSTNAME'], current_app.config['REDIS_PORT'], current_app.config['REDIS_DB'], current_app.config['REDIS_PASSWORD']) rStoreResults = rStore.search(search_term) response = [rStoreResult['Locations'] for rStoreResult in rStoreResults] except RedisException: print "Redis unavailable ..." return response
def get(self, location): """ :param - location The actual word prefix, whole word or words, comprising Location search phrase. :queryparam - ac_selected Whether or not user selected the Location via the autocomplete result dropdown or from typed entry. If the former an exact match is made to the chosen Location, else a partial match is made. :queryparam - page Which page of paginated results to show RETURNS list of json results with each entry comprising full film metadata. Pagination information is sent via headers: prev - previous page to current, 0 if none next - next page to current, 0 if none page - current requested page num_films_at_locations - total number of films 'matching' location pages - number of pages of results, 10 per page """ p = request.args.get("page") ac = request.args.get("ac_selected") if p is None: abort(400) try: page = int(p) except ValueError: abort(400) # it was a string, not an int. location = urllib.unquote(location).decode("utf8").replace("'", " ") try: r_store = RedisStore( current_app.config["REDIS_AUTOCOMPLETE_SORTED_SET"], current_app.config["REDIS_HOSTNAME"], current_app.config["REDIS_PORT"], current_app.config["REDIS_DB"], current_app.config["REDIS_PASSWORD"], ) film_locations = r_store.search(location) except RedisException: print "Redis unavailable ..." response = Response("[]", status=200, mimetype="application/json") response.headers["prev"] = 0 response.headers["next"] = 0 response.headers["page"] = page response.headers["num_films_at_locations"] = 0 response.headers["pages"] = 0 return response if ac is not None and ac == "True": film_locations = [ film_location for film_location in film_locations if film_location["Locations"].replace("'", " ") == location ] num_films_at_locations = len(film_locations) start = 10 * (page - 1) end = (start + 10) if (start + 10) < num_films_at_locations else num_films_at_locations results_per_page = current_app.config["RESULTS_PER_PAGE"] pages = num_films_at_locations / results_per_page pages = (pages + 1) if num_films_at_locations % results_per_page > 0 else pages if (page != 1) and ((page < 1) or (page > pages)): abort(400) prev = 0 if (page + 1) <= pages: next = page + 1 else: next = 0 if (page - 1) > 0: prev = page - 1 js = json.dumps(film_locations[start:end]) response = Response(js, status=200, mimetype="application/json") response.headers["prev"] = prev response.headers["next"] = next response.headers["page"] = page response.headers["num_films_at_locations"] = num_films_at_locations response.headers["pages"] = pages return response