Back to index

plone3  3.1.7
testAdvancedQuery.py
Go to the documentation of this file.
00001 # Copyright (C) 2004-2006 by Dr. Dieter Maurer, Eichendorffstr. 23, D-66386 St. Ingbert, Germany
00002 # see "LICENSE.txt" for details
00003 #       $Id: testAdvancedQuery.py,v 1.7 2006/11/09 19:27:33 dieter Exp $
00004 
00005 # as specified by "ZopeTestCase.framework"
00006 import os, sys
00007 if __name__ == '__main__':
00008   execfile(os.path.join(sys.path[0], 'framework.py'))
00009 else: import framework
00010 
00011 from Products.AdvancedQuery import *
00012 
00013 from TestBase import TestCase, getSuite
00014 
00015 class TestAdvancedQuery(TestCase):
00016 
00017   def testSlicing(self):
00018     c = self.catalog
00019     r = c.evalAdvancedQuery(Eq('I1', 'a'), ('I1',))
00020     # must not raise an exception
00021     r[1:]
00022 
00023   def testEq(self):
00024     self._checkQuery(Eq('I1','a'), '13')
00025     self._checkQuery(Eq('I1','a', filter=True), '13')
00026     self._checkQuery(Eq('I1','c'), '6')
00027     self._checkQuery(Eq('I1','x'), '')
00028 
00029   def testLe(self):
00030     self._checkQuery(Le('I1','a'), '13')
00031     self._checkQuery(Le('I1','a', filter=True), '13')
00032     self._checkQuery(Le('I1','b'), '1234')
00033 
00034   def testGe(self):
00035     self._checkQuery(Ge('I1','b'), '246')
00036     self._checkQuery(Ge('I1','b', filter=True), '246')
00037 
00038   def testIn(self):
00039     self._checkQuery(In('I1',('a','c')), '136')
00040     self._checkQuery(In('I1',('a','c'), filter=True), '136')
00041 
00042   def testBetween(self):
00043     self._checkQuery(Between('I1','a','b'), '1234')
00044     self._checkQuery(Between('I1','a','b', filter=True), '1234')
00045 
00046   def testGeneric(self):
00047     self._checkQuery(Generic('I1', {'query':'a'}), '13')
00048     self._checkQuery(Generic('I1', {'query':'a'}, filter=True), '13')
00049 
00050   def testIndexes(self):
00051     self._checkQuery(Indexed('I1'), '12346')
00052 
00053   def testAnd(self):
00054     self._checkQuery(Eq('I1', 'a') & Eq('I2', 'A'), '1')
00055     self._checkQuery(Eq('I1', 'a') & ~ Eq('I2', 'A'), '3')
00056     q = And(Eq('I1', 'c')); q &= ~ Eq('I2', 'A'); self._checkQuery(q, '6')
00057     self._checkQuery(And(), '123456')
00058     self._checkQuery(~Eq('I1', 'a') & ~Eq('I2', 'A'), '46')
00059     
00060   def testOr(self):
00061     self._checkQuery(Eq('I1', 'a') | Eq('I2', 'A'), '1235')
00062     self._checkQuery(Or(), '')
00063     # many or
00064     q = Or(); qb = Eq('I1', 'a')
00065     for i in range(10): q |= qb
00066     self._checkQuery(q, '13')
00067 
00068   def testSortSmallIndex(self):
00069     C = self.catalog
00070     self._check(C.evalAdvancedQuery(And(), ('I1', ('I2','desc'))), '314265', False)
00071     self._check(C.evalAdvancedQuery(Eq('I1', 'x'), ('I1', ('I2','desc'))), '', False)
00072 
00073   def testSortLargeIndex(self):
00074     C = self.catalog; folder = self.folder
00075     for i in range(7,100): self._addObject(folder, i, `i`, None)
00076     q = Eq('I2', 'A')
00077     self._check(C.evalAdvancedQuery(q, ('I1',)), '125', False)
00078     self._check(C.evalAdvancedQuery(q, (('I1','desc'),)), '215', False)
00079     
00080 
00081 
00082 class TestAdvancedQuery_ReverseOrder(TestAdvancedQuery):
00083   _ReverseOrder = 1
00084 
00085 
00086 class TestIndexIndependant(TestCase):
00087   '''tests independent from index type.'''
00088   def testMakeAdvancedQuery(self):
00089     C = self.catalog
00090     q = C.makeAdvancedQuery({'I1':'a', 'I2': {'query':'A'},})
00091     self._check(C.evalAdvancedQuery(q), '1')
00092     q = C.makeAdvancedQuery({'I1':('a','b'), 'I1_usage': 'range:min:max',})
00093     self._check(C.evalAdvancedQuery(q), '1234')
00094 
00095   def testStr(self):
00096     self.assertEqual(str(Eq('I1','a')), "I1 = 'a'")
00097     self.assertEqual(str(Le('I1',1)), 'I1 <= 1')
00098     self.assertEqual(str(Ge('I1',1)), 'I1 >= 1')
00099     self.assertEqual(str(MatchGlob('I1',1)), 'I1 =~ 1')
00100     self.assertEqual(str(MatchRegexp('I1',1)), 'I1 =~~ 1')
00101     self.assertEqual(str(Generic('I1',1)), 'I1 ~~ 1')
00102     self.assertEqual(str(In('I1',(1,2))), 'I1 in (1, 2)')
00103     self.assertEqual(str(Between('I1',1,2)), '1 <= I1 <= 2')
00104     self.assertEqual(str(Indexed('I1')), 'Indexed(I1)')
00105     self.assertEqual(str(~Le('I1',1)), '~(I1 <= 1)')
00106     self.assertEqual(str(Le('I1',1) & Eq('I2',2)), '(I1 <= 1 & I2 = 2)')
00107     self.assertEqual(str(Le('I1',1) | Eq('I2',2) | Eq('I3',3)), '(I1 <= 1 | I2 = 2 | I3 = 3)' )
00108 
00109   def testRanking_Sum(self):
00110     self._checkRank(
00111                RankByQueries_Sum,
00112                ((In('I1', ('b', 'c')),1), (Eq('I2','A'),1)),
00113                '221141615130'
00114                )
00115     self._checkRank(
00116                RankByQueries_Sum,
00117                ((Eq('I1', 'b'),1), (Eq('I2','X'),1)),
00118                '214110306050'
00119                )
00120     self._checkRank(
00121                RankByQueries_Sum,
00122                ((Eq('I1', 'x'),1), (Eq('I2','B'),1)),
00123                '314110206050'
00124                )
00125 
00126   def testRanking_Max(self):
00127     self._checkRank(
00128                RankByQueries_Max,
00129                ((In('I1', ('b', 'c')),1), (Eq('I2','A'),1)),
00130                '112141615130'
00131                )
00132     self._checkRank(
00133                RankByQueries_Max,
00134                ((Eq('I1', 'b'),2), (Eq('I2','A'),1)),
00135                '224211513060'
00136                )
00137 
00138   def _checkRank(self, ranker, rs, should):
00139     all = And()
00140     C = self.catalog; eval = C.evalAdvancedQuery
00141     rl = []; c = C._catalog
00142     for r in eval(all, (ranker(*rs), 'I1', 'I2')):
00143       rl.append(c.paths[r.data_record_id_])
00144       rl.append(`r.data_record_score_[0]`)
00145     self.assertEqual(''.join(rl), should)
00146 
00147 
00148 
00149 class TestAdvancedQuery_Field(TestAdvancedQuery):
00150   _indexType = 'FieldIndex'
00151 
00152 class TestMatching(TestCase):
00153   _stringType = 1
00154 
00155   def testGlob(self):
00156     self._checkQuery(MatchGlob('I1','*'), '12346')
00157 
00158   def testRegexp(self):
00159     self._checkQuery(MatchRegexp('I1','.'), '12346')
00160 
00161 
00162 def test_suite(): return getSuite(TestAdvancedQuery,
00163                                   TestAdvancedQuery_Field,
00164                                   TestAdvancedQuery_ReverseOrder,
00165                                   TestIndexIndependant,
00166                                   TestMatching,
00167                                   )
00168 
00169 if __name__ == '__main__': framework()