source: main/waeup.kofa/trunk/src/waeup/kofa/tests/test_sourcefactory.py @ 8685

Last change on this file since 8685 was 8605, checked in by uli, 13 years ago

Add tests for new contextual sources.

File size: 2.3 KB
Line 
1## Tests for waeup.kofa.sourcefactory.
2import unittest
3from waeup.kofa.sourcefactory import SmartBasicContextualSourceFactory
4
5class MyContext(object):
6    # a sample context with min and max values
7    minimum = 6
8    maximum = 8
9
10class MyContextualSource(SmartBasicContextualSourceFactory):
11    # a contextual sample source containing all values from context
12    def __init__(self, defaults):
13        super(MyContextualSource, self).__init__()
14        self.defaults = defaults
15
16    def getValues(self, context):
17        # This tuple could become quite large. Bad for containment checks.
18        return self.defaults + tuple(
19            range(context.minimum, context.maximum + 1))
20
21class MySmartContextualSource(MyContextualSource):
22    # a smart contextual sample source that can check a value for
23    # containment fast, even if there are zillions of values in the
24    # source.
25    def contains(self, context, value):
26        if value in self.defaults:
27            return True
28        # The context max is excluded. So we know that this method was
29        # called.
30        if value >= context.minimum and value < context.maximum:
31            return True
32        return False
33
34class SmartBasicContextualSourceFactoryTests(unittest.TestCase):
35
36    def test_default_get_values(self):
37        # smart bcsfs behave like regular ones when asked for values.
38        default_values = (4, 5, 6)
39        source = MyContextualSource(default_values)(MyContext())
40        self.assertEqual(list(source), [4, 5, 6, 6, 7, 8])
41        return
42
43    def test_default_contains(self):
44        # smart bcsfs can behave like regular ones when checking
45        # containment.  The default method is very expensive as it
46        # scans each single value of the source.
47        default_values = (4, 5, 6)
48        source = MyContextualSource(default_values)(MyContext())
49        self.assertEqual(6 in source, True)
50        self.assertEqual(8 in source, True)
51        self.assertEqual(10 in source, False)
52        return
53
54    def test_smart_contains(self):
55        # smart bcfss support smarter containment checks.
56        default_values = (4, 5, 6)
57        source = MySmartContextualSource(default_values)(MyContext())
58        self.assertEqual(6 in source, True)
59        self.assertEqual(8 in source, False) # the special contains()
60        self.assertEqual(10 in source, False)
61        return
Note: See TracBrowser for help on using the repository browser.