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_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."
    def test_a_known_search_of_one_word_returns_expected_documents(self):
        """
        Verifies a known input index cursor returns the correct documents.
        """
        self.search_query = {'$or': [{'url': u'url1'}, {'url': u'url4'}, {'url': u'url2'}, {'url': u'url3'}]}
        self.expected_result = {
u'url4': {u'title': u"URL4's page title", u'url': u'url4', u'pagetext': u'beep beep honk honk beep beep honk honk honk', u'pagerank': 0.27, u'date': u'20130511', u'flavor': u'lightning', u'_id': ObjectId('518ee0c72938d8587f000000')},
u'url1': {u'title': u"URL1's page title", u'url': u'url1', u'pagetext': u'Derp doop deep diggty doo wop dee voop.', u'pagerank': 0.4, u'date': u'20130511', u'flavor': u'blood', u'_id': ObjectId('518edf9f2938d80c5a000000')},
u'url3': {u'title': u"URL3's page title", u'url': u'url3', u'pagetext': u'Hurrrrr derr doop boo boo doo too see dee bee dee.', u'pagerank': 0.37, u'date': u'20130511', u'flavor': u'thunder', u'_id': ObjectId('518ee0c02938d8dc7c000002')},
u'url2': {u'title': u"URL2's page title", u'url': u'url2', u'pagetext': u'beebity boop bop beeweeoop bop bop bleeb beep boop.', u'pagerank': 0.32, u'date': u'20130511', u'flavor': u'blood', u'_id': ObjectId('518edfdc2938d80e5a000000')}}
        self.expected_result_count = 4
        self.result, self.result_count = database.query_mongo(query=self.search_query, collection=settings.COLLECTION_DOCUMENTS, db=self.dbconnection)
        self.assertEqual(self.result, self.expected_result), "A known pages query with known results returned incorrect results."
        self.assertEqual(self.result_count, self.expected_result_count), "A known pages query with known results returned the wrong number of results."