def testMakeComplexQuery(self): """Test that make_query returns sane results""" test_date = DateTime() test_unicode = u'unic\xF3de' test_int = 42 test_str = 'str' test_record = {'arg1': [test_int, test_str, test_unicode, test_date], 'arg2': test_unicode} test_list = [test_int, test_str, test_unicode, test_date] query = make_query(int_key=test_int, date_key=test_date, str_key=test_str, unicode_key=test_unicode, record_key=test_record, list_key=test_list) expect = {'int_key:int': str(test_int), 'date_key:date': str(test_date), 'str_key': test_str, 'unicode_key:utf8:ustring': test_unicode.encode('utf8'), 'record_key.arg1:int:list:record': str(test_int), 'record_key.arg1:list:record': test_str, 'record_key.arg1:utf8:ustring:list:record':test_unicode.encode('utf8'), 'record_key.arg1:date:list:record': str(test_date), 'record_key.arg2:utf8:ustring:record': test_unicode.encode('utf8'), 'list_key:int:list': str(test_int), 'list_key:list': test_str, 'list_key:utf8:ustring:list': test_unicode.encode('utf8'), 'list_key:date:list': str(test_date) } result = parse_qs_nolist(query) self.maxDiff=3000 self.assertEqual(expect, result)
def testMakeQueryUnicode(self): ''' Test makequery against Github issue 15 https://github.com/zopefoundation/Zope/issues/15 ''' query = make_query(search_text=u'unic\xF3de') if PY2: arg_type = 'search_text:utf8:ustring=' else: arg_type = 'search_text=' self.assertEqual(arg_type + 'unic%C3%B3de', query)
def testMakeComplexQuery(self): '''Test that make_query returns sane results''' test_date = DateTime() quote_date = quote(str(test_date)) record = {'arg1': [1, test_date, 'str'], 'arg2': 1} list_ = [1, test_date, 'str'] int_ = 1 str_ = 'str' query = make_query(date=test_date, integer=int_, listing=list_, record=record, string=str_) self.assertEqual( set(query.split('&')), set([ 'date:date=%s' % quote_date, 'integer:int=1', 'listing:int:list=1', 'listing:date:list=%s' % quote_date, 'listing:list=str', 'string=str', 'record.arg1:int:list:record=1', 'record.arg1:date:list:record=%s' % quote_date, 'record.arg1:list:record=str', 'record.arg2:int:record=1', ]))
def testMakeQueryUnicode(self): ''' Test makequery against Github issue 15 https://github.com/zopefoundation/Zope/issues/15 ''' query = make_query(search_text=u'unic\xF3de') arg_type = 'search_text=' self.assertEqual(arg_type + 'unic%C3%B3de', query)
def get_results(self): """ Returns the results found in portal_catalog for query 'q'. @param: 'q': the query that s searched for @param: 'limit': what to limit results list to (default 10) @param: 'path': constrain only to path (default None) """ query_string = self.request.get('q', None) if query_string != None: limit = self.request.get('limit', 10) path = self.request.get('path', None) for char in '?-+*': query_string = query_string.replace(char, ' ') anded_parts = " AND ".join(query_string.split()) anded_parts = self.quote_bad_chars(anded_parts) + '*' self.searchterm = make_query({'searchterm': anded_parts}) query = {} query['SearchableText'] = anded_parts query['portal_type'] = self.ploneUtils.getUserFriendlyTypes() if path: query['path'] = path return self.catalog(query)[:limit]
def testMakeQuery(self): test_str = 'foo' test_unicode = u'\u03dd\xf5\xf6' query = make_query(str_key=test_str, unicode_key=test_unicode) expect = {'str_key': test_str, 'unicode_key': urllib.quote(test_unicode.encode('utf8')) } result = parse_qs_nolist(query)
def testMakeComplexQuery(self): '''Test that make_query returns sane results''' test_date = DateTime() quote_date = urllib.quote(str(test_date)) record = {'arg1': [1, test_date, 'str'], 'arg2': 1} list_ = [1, test_date, 'str'] date = test_date int_ = 1 str_ = 'str' query = make_query(date=test_date, integer=int_, listing=list_, record=record, string=str_) assert query == 'date:date=%s&integer:int=1&listing:int:list=1&listing:date:list=%s&listing:list=str&string=str&record.arg1:int:list:record=1&record.arg1:date:list:record=%s&record.arg1:list:record=str&record.arg2:int:record=1'%(quote_date,quote_date,quote_date)
def testMakeComplexQuery(self): '''Test that make_query returns sane results''' test_date = DateTime() quote_date = urllib.quote(str(test_date)) record = {'arg1': [1, test_date, 'str'], 'arg2': 1} list_ = [1, test_date, 'str'] date = test_date int_ = 1 str_ = 'str' query = make_query(date=test_date, integer=int_, listing=list_, record=record, string=str_) #XXX This test relies on dictionary ordering, which is unpredictable. ### consider urlparse.parse_qs and compare dictionaries assert query == 'date:date=%s&integer:int=1&listing:int:list=1&listing:date:list=%s&listing:list=str&string=str&record.arg1:int:list:record=1&record.arg1:date:list:record=%s&record.arg1:list:record=str&record.arg2:int:record=1'%(quote_date,quote_date,quote_date)
def testMakeComplexQuery(self): '''Test that make_query returns sane results''' test_date = DateTime() quote_date = urllib.quote(str(test_date)) record = {'arg1': [1, test_date, 'str'], 'arg2': 1} list_ = [1, test_date, 'str'] date = test_date int_ = 1 str_ = 'str' query = make_query(date=test_date, integer=int_, listing=list_, record=record, string=str_) assert query == 'date:date=%s&integer:int=1&listing:int:list=1&listing:date:list=%s&listing:list=str&string=str&record.arg1:int:list:record=1&record.arg1:date:list:record=%s&record.arg1:list:record=str&record.arg2:int:record=1' % ( quote_date, quote_date, quote_date)
def updateQuery(self, url, kwargs): """Utility method that takes a URL, parses its existing query string, url encodes and updates the query string using the values in kwargs""" d = self.combineArgs(url, kwargs) # parse the existing URL parsed_url = list(urlparse(url)) # re-encode the string # We use ZTUtils.make_query here because it # does Zope-specific marshalling of lists, # dicts, integers and DateTime. # XXX *Normal* people should not be affected by this. # but one can argue about the need of using # standard query encoding instead for non-Zope # destination urls. parsed_url[4] = make_query(**d) # rebuild the URL return urlunparse(parsed_url)
def updateQuery(self, url, kwargs): """Utility method that takes a URL, parses its existing query string, url encodes and updates the query string using the values in kwargs""" d = self.combineArgs(url, kwargs) import urlparse # parse the existing URL parsed_url = list(urlparse.urlparse(url)) # re-encode the string # We use ZTUtils.make_query here because it # does Zope-specific marshalling of lists, # dicts, integers and DateTime. # XXX *Normal* people should not be affected by this. # but one can argue about the need of using # standard query encoding instead for non-Zope # destination urls. parsed_url[4] = make_query(**d) # rebuild the URL return urlparse.urlunparse(parsed_url)