products/CPSUid

changeset 24:475f0b8fe40a 0.1.0

bugfix: make sure counter is found even if some criteria values are not basestring instances
author atchertchian
date Sat, 12 Aug 2006 08:24:53 +0000
parents ee2f601b4d83
children 609ab66c5ca4
files tests/test_uidgenerator.py uidgenerator.py
diffstat 2 files changed, 25 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/tests/test_uidgenerator.py
     1.2 +++ b/tests/test_uidgenerator.py
     1.3 @@ -104,16 +104,27 @@
     1.4      def test__getCounter(self):
     1.5          criteria = {
     1.6              'source': 'CPS',
     1.7 +            'year': 2006,
     1.8              }
     1.9          criteria_lines = (
    1.10              'source CPS',
    1.11 +            'year 2006',
    1.12              )
    1.13          counter = self.generator._getCounter(**criteria)
    1.14 -        self.assertEqual(counter.getId(), 'source-cps')
    1.15 +        self.assertEqual(counter.getId(), 'source-cps_year-2006')
    1.16          self.assertEqual(counter.counter_start, 0)
    1.17          self.assertEqual(counter.counter_current, 0)
    1.18          self.assertEqual(counter.criteria, criteria_lines)
    1.19 -        self.assertEqual(counter.getCriteria(), criteria)
    1.20 +
    1.21 +        counter.hit()
    1.22 +        self.assertEqual(counter.counter_current, 1)
    1.23 +
    1.24 +        # get it again
    1.25 +        counter_again = self.generator._getCounter(**criteria)
    1.26 +        self.assertEqual(counter.getId(), 'source-cps_year-2006')
    1.27 +        self.assertEqual(counter.counter_start, 0)
    1.28 +        self.assertEqual(counter.counter_current, 1)
    1.29 +        self.assertEqual(counter.criteria, criteria_lines)
    1.30  
    1.31  
    1.32      def test___createCounter(self):
     2.1 --- a/uidgenerator.py
     2.2 +++ b/uidgenerator.py
     2.3 @@ -43,6 +43,13 @@
     2.4  from Products.CPSUid.interfaces import IUidGenerator
     2.5  from Products.CPSUid.uidcounter import UidCounter
     2.6  
     2.7 +
     2.8 +def stringify(value):
     2.9 +    if not isinstance(value, basestring):
    2.10 +        value = str(value)
    2.11 +    return value
    2.12 +
    2.13 +
    2.14  class UidGenerator(PropertiesPostProcessor, Folder):
    2.15      """Uid Generator
    2.16  
    2.17 @@ -137,8 +144,12 @@
    2.18          """Get counter for given keyword criteria
    2.19          """
    2.20          winner = None
    2.21 +        # stringify criteria in case it holds some integers
    2.22 +        scriteria = criteria.copy()
    2.23 +        for key, value in scriteria.items():
    2.24 +            scriteria[key] = stringify(value)
    2.25          for counter in self.objectValues():
    2.26 -            if criteria == counter.getCriteria():
    2.27 +            if scriteria == counter.getCriteria():
    2.28                  winner = counter
    2.29                  break
    2.30          if winner is None: