[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - test_vocabulary.py:1.3

Casey Duncan casey@zope.com
Wed, 28 May 2003 17:23:21 -0400


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

Modified Files:
	test_vocabulary.py 
Log Message:
Add SimpleVocabulary implementation and tests
Allows a field vocabulary defined from a list or dict


=== Zope3/src/zope/schema/tests/test_vocabulary.py 1.2 => 1.3 ===
--- Zope3/src/zope/schema/tests/test_vocabulary.py:1.2	Tue May 20 12:10:30 2003
+++ Zope3/src/zope/schema/tests/test_vocabulary.py	Wed May 28 17:23:20 2003
@@ -17,6 +17,9 @@
 import sys
 import unittest
 
+from zope.interface.verify import verifyObject
+from zope.interface import Interface
+
 from zope.schema import interfaces
 from zope.schema import vocabulary
 
@@ -122,9 +125,67 @@
                                [1], [1, 42])
 
 
+class SimpleVocabularyTests(unittest.TestCase):
+    
+    def setUp(self):
+        self.list_vocab = vocabulary.SimpleVocabulary([1, 2, 3])
+        self.dict_vocab = vocabulary.SimpleVocabulary.fromDict(
+            {'one': 1, 'two': 2, 'three': 3, 'fore!': 4})
+    
+    def test_simple_term(self):
+        t = vocabulary.SimpleTerm(1)
+        verifyObject(interfaces.ITokenizedTerm, t)
+        self.assertEqual(t.value, 1)
+        self.assertEqual(t.token, "1")
+        t = vocabulary.SimpleTerm(1, "One")
+        verifyObject(interfaces.ITokenizedTerm, t)
+        self.assertEqual(t.value, 1)
+        self.assertEqual(t.token, "One")
+    
+    def test_implementation(self):
+        self.failUnless(verifyObject(interfaces.IVocabulary, self.list_vocab))
+        self.failUnless(
+            verifyObject(interfaces.IVocabularyTokenized, self.list_vocab))
+        self.failUnless(verifyObject(interfaces.IVocabulary, self.dict_vocab))
+        self.failUnless(
+            verifyObject(interfaces.IVocabularyTokenized, self.dict_vocab))
+            
+    def test_addt_interfaces(self):
+        class IStupid(Interface):
+            pass
+        v = vocabulary.SimpleVocabulary([1, 2, 3], IStupid)
+        self.failUnless(IStupid.isImplementedBy(v))
+    
+    def test_len(self):
+        self.assertEqual(len(self.list_vocab), 3)
+        self.assertEqual(len(self.dict_vocab), 4)
+    
+    def test_contains(self):
+        for v in (self.list_vocab, self.dict_vocab):
+            self.assert_(1 in v and 2 in v and 3 in v)
+            self.assert_(5 not in v)
+            
+    def test_get_query(self):
+        self.assert_(self.list_vocab.getQuery() is None)
+        
+    def test_iter_and_get_term(self):
+        for v in (self.list_vocab, self.dict_vocab):
+            for term in v:
+                self.assert_(v.getTerm(term.value) is term)
+                self.assert_(v.getTermByToken(term.token) is term)
+        
+    def test_nonunique_tokens(self):
+        self.assertRaises(
+            AssertionError, vocabulary.SimpleVocabulary, [2, '2'])
+        self.assertRaises(
+            AssertionError, vocabulary.SimpleVocabulary.fromDict, 
+            {1:'one', '1':'another one'})
+        
+
 def test_suite():
     suite = unittest.makeSuite(RegistryTests)
     suite.addTest(unittest.makeSuite(VocabularyFieldTests))
+    suite.addTest(unittest.makeSuite(SimpleVocabularyTests))
     return suite
 
 if __name__ == "__main__":