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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 _fieldgroups attr
  52. self.assertEqual(
  53. set([ fg.name for fg in emclass.fieldgroups()]),
  54. set(leclass._fieldgroups.keys())
  55. )
  56. for fgroup in emclass.fieldgroups():
  57. self.assertEqual(
  58. set([ f.name for f in fgroup.fields()]),
  59. set(leclass._fieldgroups[fgroup.name])
  60. )
  61. #Testing _linked_types attr
  62. self.assertEqual(
  63. set([ LeFactory.name2classname(lt.name) for lt in emclass.linked_types()]),
  64. set([ t.__name__ for t in leclass._linked_types ])
  65. )
  66. #Testing fieldtypes
  67. self.assertEqual(
  68. set([ f.name for f in emclass.fields()]),
  69. set(leclass._fieldtypes.keys())
  70. )
  71. for field in emclass.fields():
  72. self.assertEqual(
  73. hash(field.fieldtype_instance()),
  74. hash(leclass._fieldtypes[field.name])
  75. )
  76. def test_letype(self):
  77. """ Testing generated LeType childs classes """
  78. import dyncode
  79. for emtype in self.model.components(EditorialModel.types.EmType):
  80. letype_name = LeFactory.name2classname(emtype.name)
  81. self.assertTrue(hasattr(dyncode, letype_name))
  82. letype = getattr(dyncode, letype_name)
  83. self.assertEqual(letype._type_id, emtype.uid)
  84. self.assertEqual(letype._leclass._class_id, emtype.em_class.uid)
  85. #Testing inheritance
  86. self.assertEqual(
  87. set(letype.__bases__),
  88. set([leobject.letype.LeType, letype._leclass])
  89. )
  90. #Testing _fields
  91. self.assertEqual(
  92. set([ f.name for f in emtype.fields() ]),
  93. set(letype._fields)
  94. )
  95. #Testing superiors
  96. for nat, sups in emtype.superiors().items():
  97. self.assertIn(nat, letype._superiors.keys())
  98. self.assertEqual(
  99. set([ s.__name__ for s in letype._superiors[nat]]),
  100. set([ LeFactory.name2classname(s.name) for s in sups])
  101. )