No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_lefactory.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import unittest
  2. from unittest import TestCase
  3. import tempfile
  4. import sys
  5. import shutil
  6. import EditorialModel
  7. import leobject
  8. import leobject.test.utils
  9. from leobject.lefactory import LeFactory
  10. class TestLeFactorySyntax(TestCase):
  11. def test_generated_code_syntax(self):
  12. py = LeFactory.generate_python(**leobject.test.utils.genepy_args)
  13. pyc = compile(py, "dyn.py", 'exec')
  14. exec(pyc, globals())
  15. class TestLeFactory(TestCase):
  16. @classmethod
  17. def setUpClass(cls):
  18. """ Write the generated code in a temporary directory and import it """
  19. cls.tmpdir = leobject.test.utils.tmp_load_factory_code()
  20. @classmethod
  21. def tearDownClass(cls):
  22. """ Remove the temporary directory created at class setup """
  23. leobject.test.utils.cleanup(cls.tmpdir)
  24. def setUp(self):
  25. backend=leobject.test.utils.genepy_args['backend_cls'](**leobject.test.utils.genepy_args['backend_args'])
  26. self.model = EditorialModel.model.Model(backend = backend)
  27. def test_leobject(self):
  28. """ Testing the generated LeObject class """
  29. import dyncode
  30. self.assertTrue(hasattr(dyncode, 'LeObject'))
  31. for uid, cls in dyncode.LeObject._me_uid.items():
  32. if leobject.letype.LeType in cls.__bases__:
  33. self.assertNotEqual(cls, leobject.letype.LeType)
  34. self.assertEqual(cls._type_id, uid)
  35. elif leobject.leclass.LeClass in cls.__bases__:
  36. self.assertNotEqual(cls, leobject.leclass.LeClass)
  37. self.assertEqual(cls._class_id, uid)
  38. else:
  39. self.fail("Bad instance type for _me_uid values : %s"%type(cls))
  40. ## @todo Testing _fieldtypes attribute but we need an __hash__ method on fieldtypes
  41. def test_leclass(self):
  42. """ Testing generated LeClass childs classes """
  43. import dyncode
  44. for emclass in self.model.components(EditorialModel.classes.EmClass):
  45. leclass_name = LeFactory.name2classname(emclass.name)
  46. self.assertTrue(hasattr(dyncode, leclass_name))
  47. leclass = getattr(dyncode, leclass_name)
  48. self.assertEqual(leclass._class_id, emclass.uid)
  49. #Testing inheritance
  50. self.assertEqual(set(leclass.__bases__), set([dyncode.LeObject, leobject.leclass.LeClass]))
  51. #Testing _linked_types attr
  52. self.assertEqual(
  53. set([ LeFactory.name2classname(lt.name) for lt in emclass.linked_types()]),
  54. set([ t.__name__ for t in leclass._linked_types ])
  55. )
  56. #Testing fieldtypes
  57. self.assertEqual(
  58. set([ f.name for f in emclass.fields(relational=False)]),
  59. set(leclass._fieldtypes.keys())
  60. )
  61. for field in emclass.fields(relational=False):
  62. self.assertEqual(
  63. hash(field.fieldtype_instance()),
  64. hash(leclass._fieldtypes[field.name])
  65. )
  66. def test_letype(self):
  67. """ Testing generated LeType childs classes """
  68. import dyncode
  69. for emtype in self.model.components(EditorialModel.types.EmType):
  70. letype_name = LeFactory.name2classname(emtype.name)
  71. self.assertTrue(hasattr(dyncode, letype_name))
  72. letype = getattr(dyncode, letype_name)
  73. self.assertEqual(letype._type_id, emtype.uid)
  74. self.assertEqual(letype._leclass._class_id, emtype.em_class.uid)
  75. #Testing inheritance
  76. self.assertEqual(
  77. set(letype.__bases__),
  78. set([leobject.letype.LeType, letype._leclass])
  79. )
  80. #Testing _fields
  81. self.assertEqual(
  82. set([ f.name for f in emtype.fields() ]),
  83. set(letype._fields)
  84. )
  85. #Testing superiors
  86. for nat, sups in emtype.superiors().items():
  87. self.assertIn(nat, letype._superiors.keys())
  88. self.assertEqual(
  89. set([ s.__name__ for s in letype._superiors[nat]]),
  90. set([ LeFactory.name2classname(s.name) for s in sups])
  91. )