def search(): """ Renders the search results page. """ context = helpers.get_context(request) dbconnection = database.DatabaseConnection() dbconnection.connect() if context.get('query'): query_index = database.build_mongo_index_query(input=context['query']) #flash('query: '+str(mongo_query), category='text-info') context['query_index'] = query_index if query_index != None: cursor, cursor_count = database.query_mongo(query=query_index, collection=settings.COLLECTION_INDEX, db=dbconnection) context['cursor'] = cursor context['cursor_count'] = cursor_count query_pages, query_pages_hits = database.build_mongo_pages_query(input=cursor) if (query_pages_hits != {}): context['query_pages'] = query_pages context['query_pages_hits'] = query_pages_hits documents, documents_count = database.query_mongo(query=query_pages, collection=settings.COLLECTION_DOCUMENTS, db=dbconnection, sort=context['sort'], number_of_results=context['results'], order=context['order']) context['documents'] = documents context['documents_count'] = documents_count if(context['documents_count'] == 0): no_results() else: no_results() else: no_results() return render_template("search.html", context=context)
def test_building_a_three_word_mongo_index_query(self): """ Tests if a three word search will build the proper mongo index query. """ self.test_function_input = "there are three" self.expected_function_output = {'$or':[{'word':'there'},{'word':'are'},{'word':'three'}]} self.result = database.build_mongo_index_query(input=self.test_function_input) self.assertEqual(self.result, self.expected_function_output), "A test for three words is not building the proper mongo index query"
def test_building_a_single_word_mongo_index_query(self): """ Tests if a single word searched will build the proper mongo index query. """ self.test_function_input = "thisisaword" self.expected_function_output = {'word':'thisisaword'} self.result = database.build_mongo_index_query(input=self.test_function_input) self.assertEqual(self.result, self.expected_function_output), "Searching for one word is not building the proper mongo index query"
def test_building_a_two_word_mongo_index_query(self): """ Tests if a two word search will build the proper mongo index query. """ self.test_function_input = "two words" self.expected_function_output = {'$or':[{'word':'two'},{'word':'words'}]} self.result = database.build_mongo_index_query(input=self.test_function_input) self.assertEqual(self.result, self.expected_function_output), "A case for two words is not building the proper mongo index query"
def test_a_known_search_of_two_words_returns_expected_indices(self): """ Tests a search of two words with known results for the expected result. """ self.search_query="word1 word2" self.expected_result={u'word1':{u'url4': [6], u'url1': [9], u'url3': [1, 3, 4], u'url2': [1]}, u'word2':{u'url4': [5], u'url1': [6], u'url3': [12], u'url2': [74]}} self.query = database.build_mongo_index_query(input=self.search_query) self.cursor, self.cursor_count = database.query_mongo(query=self.query, collection=settings.COLLECTION_INDEX, db=self.dbconnection) self.assertEqual(self.cursor, self.expected_result), "Test of two known search results does not give the correct response."