test_roots.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. from __future__ import absolute_import, division
  4. from twisted.trial import unittest
  5. from twisted.python import roots
  6. class RootsTests(unittest.TestCase):
  7. def testExceptions(self):
  8. request = roots.Request()
  9. try:
  10. request.write(b"blah")
  11. except NotImplementedError:
  12. pass
  13. else:
  14. self.fail()
  15. try:
  16. request.finish()
  17. except NotImplementedError:
  18. pass
  19. else:
  20. self.fail()
  21. def testCollection(self):
  22. collection = roots.Collection()
  23. collection.putEntity("x", 'test')
  24. self.assertEqual(collection.getStaticEntity("x"),
  25. 'test')
  26. collection.delEntity("x")
  27. self.assertEqual(collection.getStaticEntity('x'),
  28. None)
  29. try:
  30. collection.storeEntity("x", None)
  31. except NotImplementedError:
  32. pass
  33. else:
  34. self.fail()
  35. try:
  36. collection.removeEntity("x", None)
  37. except NotImplementedError:
  38. pass
  39. else:
  40. self.fail()
  41. def testConstrained(self):
  42. class const(roots.Constrained):
  43. def nameConstraint(self, name):
  44. return (name == 'x')
  45. c = const()
  46. self.assertIsNone(c.putEntity('x', 'test'))
  47. self.assertRaises(roots.ConstraintViolation,
  48. c.putEntity, 'y', 'test')
  49. def testHomogenous(self):
  50. h = roots.Homogenous()
  51. h.entityType = int
  52. h.putEntity('a', 1)
  53. self.assertEqual(h.getStaticEntity('a'),1 )
  54. self.assertRaises(roots.ConstraintViolation,
  55. h.putEntity, 'x', 'y')