products/CPSUid

changeset 58:f9e495b993e2

Fixed unit tests that had been broken for 4 years
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Wed, 18 Aug 2010 20:21:11 +0200
parents ab46d6b3aea2
children 8770033a0a4a
files CHANGES tests/test_uidcounter.py tests/test_uidgenerator.py
diffstat 3 files changed, 41 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/CHANGES
     1.2 +++ b/CHANGES
     1.3 @@ -6,7 +6,7 @@
     1.4  -
     1.5  Bug fixes
     1.6  ~~~~~~~~~
     1.7 --
     1.8 +- Fixed unit tests that had been broken for 4 years
     1.9  New internal features
    1.10  ~~~~~~~~~~~~~~~~~~~~~
    1.11  - 
     2.1 --- a/tests/test_uidcounter.py
     2.2 +++ b/tests/test_uidcounter.py
     2.3 @@ -22,17 +22,19 @@
     2.4  """
     2.5  
     2.6  import unittest
     2.7 +from Testing.ZopeTestCase import ZopeTestCase
     2.8  
     2.9  from zope.interface.verify import verifyClass
    2.10  
    2.11 +import transaction
    2.12  from Products.CPSUid.interfaces import IUidCounter
    2.13  from Products.CPSUid.uidcounter import UidCounter
    2.14  
    2.15 -class TestUidCounter(unittest.TestCase):
    2.16 +class TestUidCounter(ZopeTestCase):
    2.17  
    2.18      # fixture
    2.19  
    2.20 -    def setUp(self):
    2.21 +    def afterSetUp(self):
    2.22          # add a test counter
    2.23          self.criteria = {
    2.24              "source": "CPS",
    2.25 @@ -42,7 +44,14 @@
    2.26              "source CPS",
    2.27              "doument_type Common type",
    2.28              )
    2.29 -        self.counter = UidCounter("counter", 1, self.criteria)
    2.30 +        self.folder._setObject('counter',
    2.31 +                               UidCounter("counter", 1, self.criteria))
    2.32 +        self.counter = self.folder.counter
    2.33 +        transaction.commit()
    2.34 +
    2.35 +    def beforeTearDown(self):
    2.36 +        self.app._delObject(self.folder.getId())
    2.37 +        transaction.commit()
    2.38  
    2.39      # tests
    2.40  
    2.41 @@ -71,15 +80,20 @@
    2.42      def test_hit(self):
    2.43          self.assertEqual(self.counter.counter_current, 1)
    2.44          self.assertEqual(self.counter.hit(), 1)
    2.45 -        self.assertEqual(self.counter.counter_current, 2)
    2.46          self.assertEqual(self.counter.hit(), 2)
    2.47 +        # all these hits actually belong to the micro-transaction
    2.48 +        # we need to close this one to read the proper value if we want
    2.49 +        # to do it directly
    2.50 +        transaction.commit()
    2.51          self.assertEqual(self.counter.counter_current, 3)
    2.52  
    2.53      def test_reset(self):
    2.54          self.assertEqual(self.counter.counter_current, 1)
    2.55          self.counter.counter_current = 3
    2.56 +        transaction.commit()
    2.57          self.assertEqual(self.counter.counter_current, 3)
    2.58          self.counter.reset()
    2.59 +        transaction.commit()
    2.60          self.assertEqual(self.counter.counter_current, 1)
    2.61  
    2.62      def test__getCriteria(self):
     3.1 --- a/tests/test_uidgenerator.py
     3.2 +++ b/tests/test_uidgenerator.py
     3.3 @@ -22,7 +22,10 @@
     3.4  """
     3.5  
     3.6  import unittest
     3.7 +from Testing.ZopeTestCase import ZopeTestCase
     3.8  
     3.9 +import transaction
    3.10 +from DateTime.DateTime import DateTime
    3.11  from OFS.Folder import Folder
    3.12  
    3.13  from zope.interface.verify import verifyClass
    3.14 @@ -36,18 +39,18 @@
    3.15      def getPortalObject(self):
    3.16          return self.aq_inner.aq_parent
    3.17  
    3.18 -class TestUidGenerator(unittest.TestCase):
    3.19 +class TestUidGenerator(ZopeTestCase):
    3.20  
    3.21      # fixture
    3.22  
    3.23 -    def setUp(self):
    3.24 +    def afterSetUp(self):
    3.25          # add a test generator
    3.26          self.generation_criteria = ('source',)
    3.27          self.generation_keywords = ('portal_type',)
    3.28          # TAL expression to build the id as D-S-YY-NNN with D as document type,
    3.29          # S as source,YY as the current yea and NNN an incremented number
    3.30          self.expression = "python:str(portal_type)+'-'+str(source)+'-'+DateTime().strftime('%y')+'-'+'%#03d'%number"
    3.31 -        folder = Folder('folder')
    3.32 +        folder = self.folder
    3.33          generator = UidGenerator(
    3.34              'generator',
    3.35              generation_criteria=self.generation_criteria,
    3.36 @@ -58,6 +61,12 @@
    3.37  
    3.38          # add a fake url tool
    3.39          folder._setObject('portal_url', FakeUrlTool())
    3.40 +        transaction.commit()
    3.41 +
    3.42 +    def beforeTearDown(self):
    3.43 +        self.app._delObject(self.folder.getId())
    3.44 +        transaction.commit()
    3.45 +
    3.46  
    3.47      # tests
    3.48  
    3.49 @@ -91,14 +100,20 @@
    3.50          kw = {
    3.51              'portal_type': 'file_document',
    3.52              'source': 'CPS',
    3.53 -            }
    3.54 +            } 
    3.55 +        year = str(DateTime().year())[2:]
    3.56 +
    3.57          uid = self.generator.getUid(**kw)
    3.58 -        self.assertEqual(uid, 'file_document-CPS-06-000')
    3.59 +        self.assertEqual(uid, 'file_document-CPS-%s-000' % year)
    3.60 +        transaction.commit()
    3.61 +
    3.62          new_uid = self.generator.getUid(**kw)
    3.63 -        self.assertEqual(new_uid, 'file_document-CPS-06-001')
    3.64 +        self.assertEqual(new_uid, 'file_document-CPS-%s-001' % year)
    3.65 +        transaction.commit()
    3.66 +
    3.67          # hit again
    3.68          newer_uid = self.generator.getUid(**kw)
    3.69 -        self.assertEqual(newer_uid, 'file_document-CPS-06-002')
    3.70 +        self.assertEqual(newer_uid, 'file_document-CPS-%s-002' % year)
    3.71  
    3.72  
    3.73      def test__getCounter(self):