Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / test_roots.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 from twisted.trial import unittest
5 from twisted.python import roots
6 import types
7
8 class RootsTest(unittest.TestCase):
9
10     def testExceptions(self):
11         request = roots.Request()
12         try:
13             request.write("blah")
14         except NotImplementedError:
15             pass
16         else:
17             self.fail()
18         try:
19             request.finish()
20         except NotImplementedError:
21             pass
22         else:
23             self.fail()
24
25     def testCollection(self):
26         collection = roots.Collection()
27         collection.putEntity("x", 'test')
28         self.assertEqual(collection.getStaticEntity("x"),
29                              'test')
30         collection.delEntity("x")
31         self.assertEqual(collection.getStaticEntity('x'),
32                              None)
33         try:
34             collection.storeEntity("x", None)
35         except NotImplementedError:
36             pass
37         else:
38             self.fail()
39         try:
40             collection.removeEntity("x", None)
41         except NotImplementedError:
42             pass
43         else:
44             self.fail()
45
46     def testConstrained(self):
47         class const(roots.Constrained):
48             def nameConstraint(self, name):
49                 return (name == 'x')
50         c = const()
51         self.assertEqual(c.putEntity('x', 'test'), None)
52         self.failUnlessRaises(roots.ConstraintViolation,
53                               c.putEntity, 'y', 'test')
54
55
56     def testHomogenous(self):
57         h = roots.Homogenous()
58         h.entityType = types.IntType
59         h.putEntity('a', 1)
60         self.assertEqual(h.getStaticEntity('a'),1 )
61         self.failUnlessRaises(roots.ConstraintViolation,
62                               h.putEntity, 'x', 'y')
63