source: main/waeup.sirp/trunk/src/waeup/sirp/schema/tests/test_textlinechoice.py @ 7195

Last change on this file since 7195 was 7137, checked in by Henrik Bettermann, 13 years ago

Set value Id for property svn:keywords in all Python files.

  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1"""Test TextLineChoice
2"""
3import unittest
4from zope.interface import implements, directlyProvides
5from zope.schema import vocabulary
6from zope.schema.interfaces import (
7    InvalidValue, ValidationError,
8    IVocabulary, ISource, IContextSourceBinder,)
9from zope.schema.tests.test_strfield import TextLineTest
10from waeup.sirp.schema import TextLineChoice
11
12# Define some helper functions, classes, vars for tests
13class DummyRegistry(vocabulary.VocabularyRegistry):
14    def get(self, object, name):
15        v = SampleVocabulary()
16        v.object = object
17        v.name = name
18        return v
19
20class SampleTerm(object):
21    pass
22
23SAMPLE_VALUES = [u'a', u'b', u'c', u'd']
24class SampleVocabulary(object):
25    implements(IVocabulary)
26
27    def __iter__(self):
28        return iter([self.getTerm(x) for x in SAMPLE_VALUES])
29
30    def __contains__(self, value):
31        return value in SAMPLE_VALUES
32        return 0 <= value < 10
33
34    def __len__(self):
35        return len(SAMPLE_VALUES)
36
37    def getTerm(self, value):
38        if value in self:
39            t = SampleTerm()
40            t.value = value
41            t.upper = value.upper() #2 * value
42            return t
43        raise LookupError("no such value: %r" % value)
44
45class LetterASource(object):
46    # A source that contains all strings with letter 'a'
47    implements(ISource)
48    required_letter = 'a'
49    def __contains__(self, value):
50        return self.required_letter in value
51letter_a_source = LetterASource()
52
53def letter_source_binder(context):
54    source = LetterASource()
55    source.required_letter = context.req_letter
56    return source
57directlyProvides(letter_source_binder, IContextSourceBinder)
58
59class SampleContext(object):
60    req_letter = 'b'
61
62
63
64class TextLineChoiceAsTextLineTest(TextLineTest):
65    # Tests to guarantee TextLine like behaviour of TextLineChoice
66    # i.e. make sure that a TextLineChoice can everything a TextLine can
67    _Field_Factory = TextLineChoice
68
69class TextLineChoice_Values_Tests(unittest.TestCase):
70    # Tests to guarantee values support
71    def test_create_vocabulary(self):
72        choice = TextLineChoice(values=[1, 3])
73        self.assertEqual([term.value for term in choice.vocabulary], [1, 3])
74
75    def test_validate_string(self):
76        choice = TextLineChoice(values=[u'a', u'c'])
77        choice.validate(u'a')
78        #choice.validate('c')
79        choice.validate(u'c')
80        self.assertRaises(InvalidValue, choice.validate, u'd')
81
82    def test_validate_strings(self):
83        choice = TextLineChoice(values=[u'foo', u'bar'])
84        choice.validate(u'foo')
85        choice.validate(u'bar')
86        choice.validate(u'bar')
87        self.assertRaises(InvalidValue, choice.validate, u'baz')
88
89class TextLineChoice_Vocabulary_Tests(unittest.TestCase):
90    # Tests of the TextLineChoice Field using vocabularies.
91    # Most tests were copied (slightly modified) from zope.schema.tests.
92
93    def setUp(self):
94        vocabulary._clear()
95
96    def tearDown(self):
97        vocabulary._clear()
98
99    def check_preconstructed(self, cls, okval, badval):
100        v = SampleVocabulary()
101        field = cls(vocabulary=v)
102        self.assert_(field.vocabulary is v)
103        self.assert_(field.vocabularyName is None)
104        bound = field.bind(None)
105        self.assert_(bound.vocabulary is v)
106        self.assert_(bound.vocabularyName is None)
107        bound.default = okval
108        self.assertEqual(bound.default, okval)
109        self.assertRaises(ValidationError, setattr, bound, "default", badval)
110
111    def test_preconstructed_vocabulary(self):
112        self.check_preconstructed(TextLineChoice, u'a', u'z')
113
114    def check_constructed(self, cls, okval, badval):
115        vocabulary.setVocabularyRegistry(DummyRegistry())
116        field = cls(vocabulary="vocab")
117        self.assert_(field.vocabulary is None)
118        self.assertEqual(field.vocabularyName, "vocab")
119        o = object()
120        bound = field.bind(o)
121        self.assert_(isinstance(bound.vocabulary, SampleVocabulary))
122        bound.default = okval
123        self.assertEqual(bound.default, okval)
124        self.assertRaises(ValidationError, setattr, bound, "default", badval)
125
126    def test_constructed_vocabulary(self):
127        self.check_constructed(TextLineChoice, u'a', u'z')
128
129    def test_create_vocabulary(self):
130        vocabulary.setVocabularyRegistry(DummyRegistry())
131        field = TextLineChoice(vocabulary="vocab")
132        o = object()
133        bound = field.bind(o)
134        self.assertEqual([term.value for term in bound.vocabulary],
135                         SAMPLE_VALUES)
136
137    def test_undefined_vocabulary(self):
138        choice = TextLineChoice(vocabulary="unknown")
139        self.assertRaises(ValueError, choice.validate, u"value")
140
141class TextLineChoice_Source_Tests(unittest.TestCase):
142    # Tests of the TextLineChoice Field using sources.
143
144    def test_simple_source(self):
145        choice = TextLineChoice(__name__='astring', source=letter_a_source)
146        bound = choice.bind(object())
147        self.assertEqual(bound.vocabulary, letter_a_source)
148        return
149
150    def test_validate_simple_source(self):
151        choice = TextLineChoice(__name__='astring', source=letter_a_source)
152        bound = choice.bind(object())
153        bound.validate(u'string_with_letter_a')
154        self.assertRaises(
155            InvalidValue, bound.validate, u'other_string')
156        return
157
158    def test_contextual_source(self):
159        # make sure we can pass contextual sources
160        choice = TextLineChoice(__name__='astring', source=letter_source_binder)
161        bound = choice.bind(SampleContext()) # requires 'b' instead of 'a'
162        self.assertTrue(isinstance(bound.vocabulary, LetterASource))
163        self.assertTrue(bound.vocabulary is not letter_a_source)
164        return
165
166    def test_validate_contextual_source(self):
167        # make sure the values from context are considered
168        choice = TextLineChoice(__name__='astring', source=letter_source_binder)
169        bound = choice.bind(SampleContext()) # requires 'b' instead of 'a'
170        bound.validate(u'string_with_letter_b')
171        self.assertRaises(
172            InvalidValue, bound.validate, u'other_string') # no 'b'
173        return
174
175def test_suite():
176    # We must define a test suite as we do not want the imported
177    # TextLineTest to be collected by testrunner.
178    return unittest.TestSuite((
179        unittest.makeSuite(TextLineChoiceAsTextLineTest),
180        unittest.makeSuite(TextLineChoice_Values_Tests),
181        unittest.makeSuite(TextLineChoice_Vocabulary_Tests),
182        unittest.makeSuite(TextLineChoice_Source_Tests),
183        ))
Note: See TracBrowser for help on using the repository browser.