[Zope3-checkins] CVS: Zope3/src/zope/app/tests - test_batching.py:1.1

Stephan Richter srichter@cosmos.phy.tufts.edu
Thu, 31 Jul 2003 15:14:19 -0400


Update of /cvs-repository/Zope3/src/zope/app/tests
In directory cvs.zope.org:/tmp/cvs-serv22793/tests

Added Files:
	test_batching.py 
Log Message:
Added a first cut of batching support. It still does not handle orphans and
roman or alphabetic numerals, but this is just the icing on the cake. For 
now this version works really well for me in the bug tracker.


=== Added File Zope3/src/zope/app/tests/test_batching.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Bug Tracker Mail Subscription and Mailer Tests

$Id: test_batching.py,v 1.1 2003/07/31 19:14:14 srichter Exp $
"""
import unittest

from zope.app.batching import Batch
from zope.app.interfaces.batching import IBatch

class BatchTest(unittest.TestCase):

    def getData(self):
        return ['one', 'two', 'three', 'four', 'five', 'six',
                'seven', 'eight', 'nine', 'ten']

    def test_Interface(self):
        self.failUnless(IBatch.isImplementedBy(Batch([], 0, 0)))

    def test_constructor(self):
        batch = Batch(self.getData(), 9, 3)
        self.assertRaises(IndexError, Batch, self.getData(), start=10, size=3)

    def test__len__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(len(batch), 3)
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(len(batch), 1)

    def test__getitem__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch[0], 'one')
        self.assertEqual(batch[1], 'two')
        self.assertEqual(batch[2], 'three')
        batch = Batch(self.getData(), 3, 3)
        self.assertEqual(batch[0], 'four')
        self.assertEqual(batch[1], 'five')
        self.assertEqual(batch[2], 'six')
        batch = Batch(self.getData(), 9, 3)
        self.assertRaises(IndexError, batch.__getitem__, 3)
        
    def test__iter__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(list(iter(batch)), ['one', 'two', 'three'])
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(list(iter(batch)), ['ten'])

    def test__contains__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assert_(batch.__contains__('one'))
        self.assert_(batch.__contains__('two'))
        self.assert_(batch.__contains__('three'))
        self.assert_(not batch.__contains__('four'))
        batch = Batch(self.getData(), 6, 3)
        self.assert_(not batch.__contains__('one'))
        self.assert_(batch.__contains__('seven'))
        self.assert_(not batch.__contains__('ten'))

    def test_nextBatch(self):
        next = Batch(self.getData(), 0, 3).nextBatch()
        self.assertEqual(list(iter(next)), ['four', 'five', 'six'])
        nextnext = next.nextBatch()
        self.assertEqual(list(iter(nextnext)), ['seven', 'eight', 'nine'])
        next = Batch(self.getData(), 9, 3).nextBatch()
        self.assertEqual(next, None)

    def test_prevBatch(self):
        prev = Batch(self.getData(), 9, 3).prevBatch()
        self.assertEqual(list(iter(prev)), ['seven', 'eight', 'nine'])
        prevprev = prev.prevBatch()
        self.assertEqual(list(iter(prevprev)), ['four', 'five', 'six'])
        prev = Batch(self.getData(), 0, 3).prevBatch()
        self.assertEqual(prev, None)

    def test_batchRoundTrip(self):
        batch = Batch(self.getData(), 0, 3).nextBatch()
        self.assertEqual(list(iter(batch.nextBatch().prevBatch())),
                         list(iter(batch)))

    def test_first_last(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.first(), 'one')
        self.assertEqual(batch.last(), 'three')
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(batch.first(), 'ten')
        self.assertEqual(batch.last(), 'ten')
        
    def test_total(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.total(), 10)
        batch = Batch(self.getData(), 6, 3)
        self.assertEqual(batch.total(), 10)
    
    def test_startNumber(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.startNumber(), 1)
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(batch.startNumber(), 10)

    def test_endNumber(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.endNumber(), 3)
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(batch.endNumber(), 10)
        

def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(BatchTest),
        ))

if __name__ == '__main__':
    unittest.main()