[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/DateIndexes/tests - test_DateIndex.py:1.1.2.1 test_DateRangeIndex.py:1.1.2.1
Martijn Pieters
mj@zope.com
Wed, 8 May 2002 16:41:43 -0400
Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/DateIndexes/tests
In directory cvs.zope.org:/tmp/cvs-serv7740/PluginIndexes/DateIndexes/tests
Added Files:
Tag: mj-dateindexes_integration-branch
test_DateIndex.py test_DateRangeIndex.py
Log Message:
Whoops, wrong place. Code needs lots of work to make it all work.
=== Added File Zope/lib/python/Products/PluginIndexes/DateIndexes/tests/test_DateIndex.py ===
import Zope
import unittest
from DateTime import DateTime
from Products.DateIndexes.DateIndex import DateIndex
class Dummy:
def __init__(self, name, date):
self._name = name
self._date = date
def name(self):
return self._name
def date(self):
return self._date
dummies = (Dummy('a', None), Dummy('b', DateTime(0)), Dummy('c',
DateTime('2002-05-08 15:16:17')))
def matchingDummies(value):
return [d for d in dummies if d.date() == value]
class DI_Tests(unittest.TestCase):
def test_empty(self):
empty = DateIndex('empty')
self._assert(empty.getEntryForObject(1234) is None)
empty.unindex_object(1234) # shouldn't throw
assert not empty.uniqueValues('foo')
assert not empty.uniqueValues('foo', 1)
assert empty._apply_index({'zed' : 12345}) is None
result, used = empty._apply_index({'empty': 12345})
assert not result
assert used == ( None )
def test_retrieval( self ):
work = DateRangeIndex( 'work', 'date' )
for i in range( len( dummies ) ):
work.index_object( i, dummies[i] )
for i in range( len( dummies ) ):
assert work.getEntryForObject( i ) == dummies[i].date()
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( DI_Tests ) )
return suite
def run():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
run()
=== Added File Zope/lib/python/Products/PluginIndexes/DateIndexes/tests/test_DateRangeIndex.py ===
import Zope
import unittest
from Products.DateIndexes.DateRangeIndex import DateRangeIndex
class Dummy:
def __init__( self, name, start, stop ):
self._name = name
self._start = start
self._stop = stop
def name( self ):
return self._name
def start( self ):
return self._start
def stop( self ):
return self._stop
def datum( self ):
return ( self._start, self._stop )
dummies = [ Dummy( 'a', None, None )
, Dummy( 'b', None, None )
, Dummy( 'c', 0, None )
, Dummy( 'd', 10, None )
, Dummy( 'e', None, 4 )
, Dummy( 'f', None, 11 )
, Dummy( 'g', 0, 11 )
, Dummy( 'h', 2, 9 )
]
def matchingDummies( value ):
result = []
for dummy in dummies:
if ( ( dummy.start() is None or dummy.start() <= value )
and ( dummy.stop() is None or dummy.stop() >= value )
):
result.append( dummy )
return result
class DRI_Tests( unittest.TestCase ):
def setUp( self ):
pass
def tearDown( self ):
pass
def test_empty( self ):
empty = DateRangeIndex( 'empty' )
assert empty.getEntryForObject( 1234 ) is None
empty.unindex_object( 1234 ) # shouldn't throw
assert not empty.uniqueValues( 'foo' )
assert not empty.uniqueValues( 'foo', 1 )
assert empty._apply_index( { 'zed' : 12345 } ) is None
result, used = empty._apply_index( { 'empty' : 12345 } )
assert not result
assert used == ( None, None )
def test_retrieval( self ):
work = DateRangeIndex( 'work', 'start', 'stop' )
for i in range( len( dummies ) ):
work.index_object( i, dummies[i] )
for i in range( len( dummies ) ):
assert work.getEntryForObject( i ) == dummies[i].datum()
for value in range( -1, 15 ):
matches = matchingDummies( value )
results, used = work._apply_index( { 'work' : value } )
assert used == ( 'start', 'stop' )
assert len( matches ) == len( results ), ( '%s: %s == %s'
% ( value, map( lambda x: x.name(), matches ), results ) )
matches.sort( lambda x, y: cmp( x.name(), y.name() ) )
for result, match in map( None, results, matches ):
assert work.getEntryForObject( result ) == match.datum()
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( DRI_Tests ) )
return suite
def run():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
run()