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.3KB

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