[Zope-Checkins] CVS: Zope2 - test_UnIndex.py:1.3.6.1.2.1
chrism@serenade.digicool.com
chrism@serenade.digicool.com
Thu, 12 Apr 2001 18:40:15 -0400
Update of /cvs-repository/Zope2/lib/python/SearchIndex/tests
In directory serenade.digicool.com:/home/chrism/sandboxes/CatalogForNow/lib/python/SearchIndex/tests
Modified Files:
Tag: chrism-CatalogForNow-branch
test_UnIndex.py
Log Message:
overhauled fieldindex tests
--- Updated File test_UnIndex.py in package Zope2 --
--- test_UnIndex.py 2001/03/15 13:10:34 1.3.6.1
+++ test_UnIndex.py 2001/04/12 22:40:15 1.3.6.1.2.1
@@ -95,81 +95,30 @@
import unittest
from SearchIndex.UnIndex import UnIndex
-class Dummy:
-
- def __init__( self, foo ):
- self._foo = foo
-
- def foo( self ):
- return self._foo
-
- def __str__( self ):
- return '<Dummy: %s>' % self._foo
-
- __repr__ = __str__
-
class TestCase( unittest.TestCase ):
"""
Test FieldIndex objects.
"""
def setUp( self ):
- """
- """
self._index = UnIndex( 'foo' )
self._marker = []
- self._values = [ ( 0, Dummy( 'a' ) )
- , ( 1, Dummy( 'ab' ) )
- , ( 2, Dummy( 'abc' ) )
- , ( 3, Dummy( 'abca' ) )
- , ( 4, Dummy( 'abcd' ) )
- , ( 5, Dummy( 'abce' ) )
- , ( 6, Dummy( 'abce' ) )
- , ( 7, Dummy( 0 ) ) # Collector #1959
- , ( 8, Dummy(None) )]
- self._forward = {}
- self._backward = {}
- for k, v in self._values:
- self._backward[k] = v
- keys = self._forward.get( v, [] )
- self._forward[v] = keys
-
- self._noop_req = { 'bar': 123 }
- self._request = { 'foo': 'abce' }
- self._min_req = { 'foo': 'abc'
- , 'foo_usage': 'range:min'
- }
- self._max_req = { 'foo': 'abc'
- , 'foo_usage': 'range:max'
- }
- self._range_req = { 'foo': ( 'abc', 'abcd' )
- , 'foo_usage': 'range:min:max'
- }
- self._zero_req = { 'foo': 0 }
- self._none_req = { 'foo': None }
-
-
- def tearDown( self ):
- """
- """
- def _populateIndex( self ):
- for k, v in self._values:
- self._index.index_object( k, v )
-
- def _checkApply( self, req, expectedValues ):
- result, used = self._index._apply_index( req )
- if hasattr(result, 'keys'):
- result = result.keys()
- assert used == ( 'foo', )
- assert len( result ) == len( expectedValues ), \
- '%s | %s' % ( map( None, result ), expectedValues )
- for k, v in expectedValues:
- assert k in result
-
def testEmpty( self ):
"Test an empty FieldIndex."
+ self._checkApply({'bar':123}, None) # should ignore (noop)
+ self._checkApply({'foo':''}, None) # bare blank string
+ self._checkApply({'foo':('',)}, []) # blank string in tuple
+ self._checkApply({'foo':'abce'}, [])
+ self._checkApply({'foo':('abce', 'abc')}, [])
+ self._checkApply({'foo':'abc','foo_usage':'range:max'}, [])
+ self._checkApply({'foo':'abc', 'foo_usage':'range:min'}, [])
+ self._checkApply({'foo':0}, [])
+ self._checkApply({'foo':None}, [])
+ self._checkApply({'foo':('abc', 'abcd'), 'foo_usage':'range:min:max'},
+ [] )
+
assert len( self._index ) == 0
assert len( self._index.referencedObjects() ) == 0
@@ -182,16 +131,31 @@
assert not self._index.hasUniqueValuesFor( 'bar' )
assert len( self._index.uniqueValues( 'foo' ) ) == 0
- assert self._index._apply_index( self._noop_req ) is None
- self._checkApply( self._request, [] )
- self._checkApply( self._min_req, [] )
- self._checkApply( self._max_req, [] )
- self._checkApply( self._range_req, [] )
-
def testPopulated( self ):
""" Test a populated FieldIndex """
- self._populateIndex()
- values = self._values
+ values = [ ( 0, Dummy( 'a' ) ),
+ ( 1, Dummy( 'ab' ) ),
+ ( 2, Dummy( 'abc' ) ),
+ ( 3, Dummy( 'abca' ) ),
+ ( 4, Dummy( 'abcd' ) ),
+ ( 5, Dummy( 'abce' ) ),
+ ( 6, Dummy( 'abce' ) ),
+ ( 7, Dummy( 0 ) ),
+ ( 8, Dummy(None) ),
+ ( 9, Dummy('')) ]
+ for k, v in values:
+ self._index.index_object( k, v )
+ self._checkApply({'bar':123}, None) # should ignore (noop)
+ self._checkApply({'foo':''}, None) # bare blank string
+ self._checkApply({'foo':('',)}, [9]) # blank string in tuple
+ self._checkApply({'foo':'abce'}, [5,6])
+ self._checkApply({'foo':('abce', 'abc')}, [2,5,6])
+ self._checkApply({'foo':'abc','foo_usage':'range:max'}, [0,1,2,7,8,9])
+ self._checkApply({'foo':'abc', 'foo_usage':'range:min'}, [2,3,4,5,6])
+ self._checkApply({'foo':0}, [7])
+ self._checkApply({'foo':None}, [8])
+ self._checkApply({'foo':('abc', 'abcd'), 'foo_usage':'range:min:max'},
+ [2,3,4] )
assert len( self._index ) == len( values )-1 #'abce' is duplicate
assert len( self._index.referencedObjects() ) == len( values )
@@ -205,28 +169,11 @@
assert self._index.getEntryForObject( k ) == v.foo()
assert len( self._index.uniqueValues( 'foo' ) ) == len( values )-1
-
- assert self._index._apply_index( self._noop_req ) is None
-
- self._checkApply( self._request, values[ -4:-2 ] )
- self._checkApply( self._min_req, values[ 2:-2 ] )
- self._checkApply( self._max_req, values[ :3 ] + values[ -2: ] )
- self._checkApply( self._range_req, values[ 2:5 ] )
-
- def testZero( self ):
- """ Make sure 0 gets indexed """
- self._populateIndex()
- values = self._values
- self._checkApply( self._zero_req, values[ -2:-1 ] )
- assert 0 in self._index.uniqueValues( 'foo' )
-
- def testNone(self):
- """ make sure None gets indexed """
- self._populateIndex()
- values = self._values
- self._checkApply(self._none_req, values[-1:])
+ assert self._index.hasUniqueValuesFor( 'foo' )
+ assert not self._index.hasUniqueValuesFor( 'bar' )
+ assert 0 in self._index.uniqueValues('foo')
assert None in self._index.uniqueValues('foo')
-
+
def testRange(self):
"""Test a range search"""
index = UnIndex( 'foo' )
@@ -248,6 +195,33 @@
assert r==expect, r
+ def _checkApply( self, req, expectedValues ):
+ result = self._index._apply_index( req )
+ if result is None:
+ assert expectedValues is None
+ return
+ result, used = result
+ if hasattr(result, 'keys'):
+ result = result.keys()
+ assert used == ( 'foo', )
+ assert len( result ) == len( expectedValues ), \
+ '%s | %s' % ( map( None, result ), expectedValues )
+ for k in expectedValues:
+ assert k in result
+
+class Dummy:
+
+ def __init__( self, foo ):
+ self._foo = foo
+
+ def foo( self ):
+ return self._foo
+
+ def __str__( self ):
+ return '<Dummy: %s>' % self._foo
+
+ __repr__ = __str__
+
def test_suite():
return unittest.makeSuite( TestCase )