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_model.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. import tests.loader_utils
  4. from lodel.editorial_model.model import EditorialModel
  5. from lodel.editorial_model.components import EmComponent, EmClass, EmField, EmGroup
  6. from lodel.utils.mlstring import MlString
  7. from lodel.editorial_model.exceptions import *
  8. class EditorialModelTestCase(unittest.TestCase):
  9. def test_d_hash(self):
  10. """ Test the deterministic hash method """
  11. model = EditorialModel("test model", description = "Test EM")
  12. cls1 = model.new_class('testclass1', display_name = 'Classe de test 1', help_text = 'super aide')
  13. c1f1 = cls1.new_field('c1testfield1', data_handler = 'varchar')
  14. c1f2 = cls1.new_field('c1testfield2', data_handler = 'varchar')
  15. cls2 = model.new_class('testclass2')
  16. c2f1 = cls2.new_field('c2testfield1', data_handler = 'varchar')
  17. c2f2 = cls2.new_field('c2testfield2', data_handler = 'varchar')
  18. grp1 = model.new_group('testgroup1')
  19. grp1.add_components((cls1, c1f1))
  20. grp2 = model.new_group('testgroup2')
  21. grp2.add_components((cls2, c1f2, c2f1, c2f2))
  22. grp2.add_dependencie(grp1)
  23. e_hash = 0x250eab75e782e51bbf212f47c6159571
  24. self.assertEqual(model.d_hash(), e_hash)
  25. c2f1.uid = 'foobar'
  26. self.assertNotEqual(model.d_hash(), e_hash)
  27. c2f1.uid = 'c2testfield1'
  28. self.assertEqual(model.d_hash(), e_hash)
  29. def test_translator_from_name(self):
  30. """ Test the translator_from_name method """
  31. import lodel.editorial_model.translator.picklefile as expected
  32. translator = EditorialModel.translator_from_name('picklefile')
  33. self.assertEqual(translator, expected)
  34. def test_invalid_translator_from_name(self):
  35. """ Test the translator_from_name method when invalid names given as argument """
  36. import lodel.editorial_model.translator.picklefile
  37. invalid_names = [
  38. lodel.editorial_model.translator.picklefile,
  39. 'foobar',
  40. 42,
  41. ]
  42. for name in invalid_names:
  43. with self.assertRaises(NameError):
  44. EditorialModel.translator_from_name(name)
  45. class EmComponentTestCase(unittest.TestCase):
  46. def test_abstract_init(self):
  47. with self.assertRaises(NotImplementedError):
  48. EmComponent('test')
  49. class EmClassTestCase(unittest.TestCase):
  50. def test_init(self):
  51. cls = EmClass('testClass', 'test class', 'A test class')
  52. self.assertEqual(cls.uid, 'testClass')
  53. self.assertEqual(cls.display_name, MlString('test class'))
  54. self.assertEqual(cls.help_text, MlString('A test class'))
  55. def test_fields(self):
  56. """ Bad test on add field method (only check uid presence) """
  57. cls = EmClass('testClass', 'test_class', 'A test class')
  58. cls.new_field('name', data_handler = 'varchar')
  59. cls.new_field('string', data_handler = 'varchar')
  60. cls.new_field('lodel_id', data_handler = 'varchar')
  61. #Automatic field added
  62. fields = cls.fields()
  63. self.assertEqual(len(fields), 4)
  64. self.assertEqual(
  65. set([f.uid for f in fields]),
  66. set(['name', 'string', 'lodel_id', 'classname'])
  67. )
  68. def test_d_hash(self):
  69. """ Test the deterministic hash method """
  70. field = EmField('test field', 'varchar')
  71. e_hash = 0x23415e5cab5cabf29ab2d2db99998ba4
  72. self.assertEqual(field.d_hash(), e_hash)
  73. field.uid = 'test field.'
  74. self.assertNotEqual(field.d_hash(), e_hash)
  75. def test_field_override(self):
  76. """ Test EmClass field overriding in inheritance """
  77. cls1 = EmClass('testClass', 'test class')
  78. cls1.new_field('test', data_handler = 'varchar', max_length = 16, nullable=True)
  79. cls1.new_field('test2', data_handler = 'integer', nullable = True)
  80. cls2 = EmClass('testClass2', parents = cls1)
  81. cls2.new_field('test', data_handler = 'varchar', max_length = 16, nullable=False)
  82. cls2.new_field('test2', data_handler = 'integer', nullable = False)
  83. self.assertEqual(len(cls1.fields()), len(cls2.fields()))
  84. self.assertTrue(cls1.fields('test').data_handler_options['nullable'])
  85. self.assertFalse(cls2.fields('test').data_handler_options['nullable'])
  86. ## @todo add more test when data handlers implements compatibility checks
  87. def test_field_invalid_type_override(self):
  88. """ Testing invalid fields overriding (incompatible data_handler)"""
  89. cls1 = EmClass('testClass', 'test class')
  90. cls1.new_field('test', data_handler = 'varchar', max_length = 8)
  91. cls1.new_field('test2', data_handler = 'integer', nullable = True)
  92. cls2 = EmClass('testClass2', parents = cls1)
  93. with self.assertRaises(AttributeError):
  94. cls2.new_field('test', data_handler = 'integer')
  95. def test_field_invalid_options_overrid(self):
  96. """ Testing invalid fields overriding (incompatible data handler options) """
  97. cls1 = EmClass('testClass', 'test class')
  98. cls1.new_field('test', data_handler = 'varchar', max_length = 8)
  99. cls1.new_field('test2', data_handler = 'integer', nullable = True)
  100. cls2 = EmClass('testClass2', parents = cls1)
  101. with self.assertRaises(AttributeError):
  102. cls2.new_field('test', data_handler = 'varchar', max_length = 2)
  103. def test_parents_recc(self):
  104. """ Test the reccursive parents property """
  105. model = EditorialModel(
  106. "test_model",
  107. description = "Model for LeFactoryTestCase"
  108. )
  109. cls1 = model.new_class('testclass1')
  110. cls2 = model.new_class('testclass2')
  111. cls3 = model.new_class('testclass3', parents = [cls2])
  112. cls4 = model.new_class('testclass4', parents = [cls1, cls3])
  113. cls5 = model.new_class('testclass5', parents = [cls4])
  114. cls6 = model.new_class('testclass6')
  115. self.assertEqual(cls5.parents_recc, set((cls4, cls1, cls2, cls3)))
  116. self.assertEqual(cls1.parents_recc, set())
  117. self.assertEqual(cls4.parents_recc, set((cls1, cls2, cls3)))
  118. self.assertEqual(cls3.parents_recc, set((cls2,)))
  119. class EmGroupTestCase(unittest.TestCase):
  120. def test_init(self):
  121. """ Test EmGroup instanciation """
  122. grp = EmGroup('testgrp', display_name = "Test group", help_text="No Help")
  123. self.assertEqual(grp.uid, 'testgrp')
  124. self.assertEqual(grp.dependencies(), dict())
  125. self.assertEqual(grp.display_name, MlString("Test group"))
  126. self.assertEqual(grp.help_text, MlString("No Help"))
  127. grp2 = EmGroup('test')
  128. self.assertEqual(grp2.uid, 'test')
  129. self.assertEqual(grp2.display_name, None)
  130. self.assertEqual(grp2.help_text, None)
  131. grp3 = EmGroup('depends', depends = (grp, grp2))
  132. self.assertEqual(set(grp3.dependencies().values()), set((grp, grp2)))
  133. def test_add_comps(self):
  134. """ Test components adding in groups"""
  135. grp = EmGroup('grp')
  136. cpn1 = EmField('test1','integer')
  137. cpn2 = EmClass('testClass', 'test class')
  138. grp.add_components([cpn1,cpn2])
  139. s1=grp.components()
  140. s2=grp.components()
  141. s1.add(EmField('test2','varchar'))
  142. self.assertEqual(s2,set([cpn1,cpn2]));
  143. def test_deps(self):
  144. """ Test dependencies """
  145. grp1 = EmGroup('grp1')
  146. grp2 = EmGroup('grp2')
  147. grp3 = EmGroup('grp3')
  148. grp4 = EmGroup('grp4')
  149. grp2.add_dependencie(grp1)
  150. grp3.add_dependencie(grp2)
  151. grp4.add_dependencie(grp2)
  152. grp4.add_dependencie(grp1)
  153. self.assertEqual(set(grp1.dependencies().values()), set())
  154. self.assertEqual(set(grp2.dependencies().values()), set([grp1]))
  155. self.assertEqual(set(grp3.dependencies().values()), set([grp2]))
  156. self.assertEqual(set(grp4.dependencies().values()), set([grp2, grp1]))
  157. self.assertEqual(set(grp3.dependencies(True).values()), set([grp2, grp1]))
  158. self.assertEqual(set(grp4.dependencies(True).values()), set([grp2, grp1]))
  159. self.assertEqual(set(grp1.required_by.values()), set([grp2, grp4]))
  160. self.assertEqual(set(grp2.required_by.values()), set([grp3, grp4]))
  161. self.assertEqual(set(grp3.required_by.values()), set())
  162. self.assertEqual(set(grp4.required_by.values()), set())
  163. for grp in [grp1, grp2, grp3, grp4]:
  164. for uid, dep in grp.dependencies(recursive = True).items():
  165. self.assertEqual(uid, dep.uid)
  166. for uid, dep in grp.required_by.items():
  167. self.assertEqual(uid, dep.uid)
  168. def test_apps(self):
  169. """ Test applicants """
  170. grp1 = EmGroup('grp1')
  171. grp2 = EmGroup('grp2')
  172. grp3 = EmGroup('grp3')
  173. grp4 = EmGroup('grp4')
  174. grp2.add_applicant(grp1)
  175. grp3.add_applicant(grp2)
  176. grp4.add_applicant(grp2)
  177. grp4.add_applicant(grp1)
  178. self.assertEqual(set(grp1.applicants().values()), set())
  179. self.assertEqual(set(grp2.applicants().values()), set([grp1]))
  180. self.assertEqual(set(grp3.applicants().values()), set([grp2]))
  181. self.assertEqual(set(grp4.applicants().values()), set([grp2, grp1]))
  182. self.assertEqual(set(grp3.applicants(True).values()), set([grp2, grp1]))
  183. self.assertEqual(set(grp4.applicants(True).values()), set([grp2, grp1]))
  184. self.assertEqual(set(grp1.required_by.values()), set())
  185. self.assertEqual(set(grp2.required_by.values()), set([grp1]))
  186. self.assertEqual(set(grp3.required_by.values()), set([grp2]))
  187. self.assertEqual(set(grp4.required_by.values()), set([grp2,grp1]))
  188. for grp in [grp1, grp2, grp3, grp4]:
  189. for uid, dep in grp.applicants(recursive = True).items():
  190. self.assertEqual(uid, dep.uid)
  191. for uid, dep in grp.required_by.items():
  192. self.assertEqual(uid, dep.uid)
  193. def test_display_name(self):
  194. grp = EmGroup('grp',None,'Test affichage du nom')
  195. a = grp.get_display_name()
  196. b = a
  197. b = 'Test de copie du nom'
  198. self.assertEqual(a,'Test affichage du nom')
  199. grp1 = EmGroup('grp')
  200. c = grp1.get_display_name()
  201. self.assertEqual(c, None)
  202. with self.assertRaises(ValueError): grp.get_display_name('ita')
  203. def test_help_text(self):
  204. grp = EmGroup('grp',None,None,'Test affichage du nom')
  205. a = grp.get_help_text()
  206. b = a
  207. b = 'Test de copie du nom'
  208. self.assertEqual(a,'Test affichage du nom')
  209. grp1 = EmGroup('grp')
  210. c = grp1.get_help_text()
  211. self.assertEqual(c, None)
  212. with self.assertRaises(ValueError): grp.get_help_text('ita')
  213. def test_deps_complex(self):
  214. """ More complex dependencies handling test """
  215. grps = [ EmGroup('group%d' % i) for i in range(6) ]
  216. grps[5].add_dependencie( (grps[1], grps[2], grps[4]) )
  217. grps[4].add_dependencie( (grps[1], grps[3]) )
  218. grps[3].add_dependencie( (grps[0],) )
  219. grps[1].add_dependencie( (grps[2], grps[0]) )
  220. self.assertEqual(
  221. set(grps[5].dependencies(True).values()),
  222. set( grps[i] for i in range(5))
  223. )
  224. self.assertEqual(
  225. set(grps[4].dependencies(True).values()),
  226. set( grps[i] for i in range(4))
  227. )
  228. grps[2].add_dependencie(grps[0])
  229. self.assertEqual(
  230. set(grps[5].dependencies(True).values()),
  231. set( grps[i] for i in range(5))
  232. )
  233. self.assertEqual(
  234. set(grps[4].dependencies(True).values()),
  235. set( grps[i] for i in range(4))
  236. )
  237. # Inserting circular deps
  238. with self.assertRaises(EditorialModelError):
  239. grps[0].add_dependencie(grps[5])
  240. def test_circular_dep(self):
  241. """ Test circular dependencies detection """
  242. grps = [ EmGroup('group%d' % i) for i in range(10) ]
  243. for i in range(1,10):
  244. grps[i].add_dependencie(grps[i-1])
  245. for i in range(1,10):
  246. for j in range(i+1,10):
  247. with self.assertRaises(EditorialModelError):
  248. grps[i].add_dependencie(grps[j])
  249. def test_d_hash(self):
  250. """ Test the deterministic hash method """
  251. grp = EmGroup('testgrp', display_name = "Test group", help_text="No Help")
  252. e_hash = 0x74657374677270333130363537393137343730343438343139303233393838303936373730323936353536393032313839313536333632313037343435323138313735343936303237373532343436303639363137
  253. self.assertEqual(grp.d_hash(), e_hash)