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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = 0x476f5917265b6fbd06d94b81f4e9b402
  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. fields = cls.fields()
  62. self.assertEqual(len(fields), 3)
  63. self.assertEqual(
  64. set([f.uid for f in fields]),
  65. set(['name', 'string', 'lodel_id'])
  66. )
  67. def test_d_hash(self):
  68. """ Test the deterministic hash method """
  69. field = EmField('test field', 'varchar')
  70. e_hash = 0x23415e5cab5cabf29ab2d2db99998ba4
  71. self.assertEqual(field.d_hash(), e_hash)
  72. field.uid = 'test field.'
  73. self.assertNotEqual(field.d_hash(), e_hash)
  74. def test_field_override(self):
  75. """ Test EmClass field overriding in inheritance """
  76. cls1 = EmClass('testClass', 'test class')
  77. cls1.new_field('test', data_handler = 'varchar', max_length = 16, nullable=True)
  78. cls1.new_field('test2', data_handler = 'integer', nullable = True)
  79. cls2 = EmClass('testClass2', parents = cls1)
  80. cls2.new_field('test', data_handler = 'varchar', max_length = 16, nullable=False)
  81. cls2.new_field('test2', data_handler = 'integer', nullable = False)
  82. self.assertEqual(len(cls1.fields()), len(cls2.fields()))
  83. self.assertTrue(cls1.fields('test').data_handler_options['nullable'])
  84. self.assertFalse(cls2.fields('test').data_handler_options['nullable'])
  85. ## @todo add more test when data handlers implements compatibility checks
  86. def test_field_invalid_type_override(self):
  87. """ Testing invalid fields overriding (incompatible data_handler)"""
  88. cls1 = EmClass('testClass', 'test class')
  89. cls1.new_field('test', data_handler = 'varchar', max_length = 8)
  90. cls1.new_field('test2', data_handler = 'integer', nullable = True)
  91. cls2 = EmClass('testClass2', parents = cls1)
  92. with self.assertRaises(AttributeError):
  93. cls2.new_field('test', data_handler = 'integer')
  94. def test_field_invalid_options_overrid(self):
  95. """ Testing invalid fields overriding (incompatible data handler options) """
  96. cls1 = EmClass('testClass', 'test class')
  97. cls1.new_field('test', data_handler = 'varchar', max_length = 8)
  98. cls1.new_field('test2', data_handler = 'integer', nullable = True)
  99. cls2 = EmClass('testClass2', parents = cls1)
  100. with self.assertRaises(AttributeError):
  101. cls2.new_field('test', data_handler = 'varchar', max_length = 2)
  102. def test_parents_recc(self):
  103. """ Test the reccursive parents property """
  104. model = EditorialModel(
  105. "test_model",
  106. description = "Model for LeFactoryTestCase"
  107. )
  108. cls1 = model.new_class('testclass1')
  109. cls2 = model.new_class('testclass2')
  110. cls3 = model.new_class('testclass3', parents = [cls2])
  111. cls4 = model.new_class('testclass4', parents = [cls1, cls3])
  112. cls5 = model.new_class('testclass5', parents = [cls4])
  113. cls6 = model.new_class('testclass6')
  114. self.assertEqual(cls5.parents_recc, set((cls4, cls1, cls2, cls3)))
  115. self.assertEqual(cls1.parents_recc, set())
  116. self.assertEqual(cls4.parents_recc, set((cls1, cls2, cls3)))
  117. self.assertEqual(cls3.parents_recc, set((cls2,)))
  118. class EmGroupTestCase(unittest.TestCase):
  119. def test_init(self):
  120. """ Test EmGroup instanciation """
  121. grp = EmGroup('testgrp', display_name = "Test group", help_text="No Help")
  122. self.assertEqual(grp.uid, 'testgrp')
  123. self.assertEqual(grp.dependencies(), dict())
  124. self.assertEqual(grp.display_name, MlString("Test group"))
  125. self.assertEqual(grp.help_text, MlString("No Help"))
  126. grp2 = EmGroup('test')
  127. self.assertEqual(grp2.uid, 'test')
  128. self.assertEqual(grp2.display_name, None)
  129. self.assertEqual(grp2.help_text, None)
  130. grp3 = EmGroup('depends', depends = (grp, grp2))
  131. self.assertEqual(set(grp3.dependencies().values()), set((grp, grp2)))
  132. def test_add_comps(self):
  133. """ Test components adding in groups"""
  134. grp = EmGroup('grp')
  135. cpn1 = EmField('test1','integer')
  136. cpn2 = EmClass('testClass', 'test class')
  137. grp.add_components([cpn1,cpn2])
  138. s1=grp.components()
  139. s2=grp.components()
  140. s1.add(EmField('test2','varchar'))
  141. self.assertEqual(s2,set([cpn1,cpn2]));
  142. def test_deps(self):
  143. """ Test dependencies """
  144. grp1 = EmGroup('grp1')
  145. grp2 = EmGroup('grp2')
  146. grp3 = EmGroup('grp3')
  147. grp4 = EmGroup('grp4')
  148. grp2.add_dependencie(grp1)
  149. grp3.add_dependencie(grp2)
  150. grp4.add_dependencie(grp2)
  151. grp4.add_dependencie(grp1)
  152. self.assertEqual(set(grp1.dependencies().values()), set())
  153. self.assertEqual(set(grp2.dependencies().values()), set([grp1]))
  154. self.assertEqual(set(grp3.dependencies().values()), set([grp2]))
  155. self.assertEqual(set(grp4.dependencies().values()), set([grp2, grp1]))
  156. self.assertEqual(set(grp3.dependencies(True).values()), set([grp2, grp1]))
  157. self.assertEqual(set(grp4.dependencies(True).values()), set([grp2, grp1]))
  158. self.assertEqual(set(grp1.required_by.values()), set([grp2, grp4]))
  159. self.assertEqual(set(grp2.required_by.values()), set([grp3, grp4]))
  160. self.assertEqual(set(grp3.required_by.values()), set())
  161. self.assertEqual(set(grp4.required_by.values()), set())
  162. for grp in [grp1, grp2, grp3, grp4]:
  163. for uid, dep in grp.dependencies(recursive = True).items():
  164. self.assertEqual(uid, dep.uid)
  165. for uid, dep in grp.required_by.items():
  166. self.assertEqual(uid, dep.uid)
  167. def test_apps(self):
  168. """ Test applicants """
  169. grp1 = EmGroup('grp1')
  170. grp2 = EmGroup('grp2')
  171. grp3 = EmGroup('grp3')
  172. grp4 = EmGroup('grp4')
  173. grp2.add_applicant(grp1)
  174. grp3.add_applicant(grp2)
  175. grp4.add_applicant(grp2)
  176. grp4.add_applicant(grp1)
  177. self.assertEqual(set(grp1.applicants().values()), set())
  178. self.assertEqual(set(grp2.applicants().values()), set([grp1]))
  179. self.assertEqual(set(grp3.applicants().values()), set([grp2]))
  180. self.assertEqual(set(grp4.applicants().values()), set([grp2, grp1]))
  181. self.assertEqual(set(grp3.applicants(True).values()), set([grp2, grp1]))
  182. self.assertEqual(set(grp4.applicants(True).values()), set([grp2, grp1]))
  183. self.assertEqual(set(grp1.required_by.values()), set())
  184. self.assertEqual(set(grp2.required_by.values()), set([grp1]))
  185. self.assertEqual(set(grp3.required_by.values()), set([grp2]))
  186. self.assertEqual(set(grp4.required_by.values()), set([grp2,grp1]))
  187. for grp in [grp1, grp2, grp3, grp4]:
  188. for uid, dep in grp.applicants(recursive = True).items():
  189. self.assertEqual(uid, dep.uid)
  190. for uid, dep in grp.required_by.items():
  191. self.assertEqual(uid, dep.uid)
  192. def test_display_name(self):
  193. grp = EmGroup('grp',None,'Test affichage du nom')
  194. a = grp.get_display_name()
  195. b = a
  196. b = 'Test de copie du nom'
  197. self.assertEqual(a,'Test affichage du nom')
  198. grp1 = EmGroup('grp')
  199. c = grp1.get_display_name()
  200. self.assertEqual(c, None)
  201. with self.assertRaises(ValueError): grp.get_display_name('ita')
  202. def test_help_text(self):
  203. grp = EmGroup('grp',None,None,'Test affichage du nom')
  204. a = grp.get_help_text()
  205. b = a
  206. b = 'Test de copie du nom'
  207. self.assertEqual(a,'Test affichage du nom')
  208. grp1 = EmGroup('grp')
  209. c = grp1.get_help_text()
  210. self.assertEqual(c, None)
  211. with self.assertRaises(ValueError): grp.get_help_text('ita')
  212. def test_deps_complex(self):
  213. """ More complex dependencies handling test """
  214. grps = [ EmGroup('group%d' % i) for i in range(6) ]
  215. grps[5].add_dependencie( (grps[1], grps[2], grps[4]) )
  216. grps[4].add_dependencie( (grps[1], grps[3]) )
  217. grps[3].add_dependencie( (grps[0],) )
  218. grps[1].add_dependencie( (grps[2], grps[0]) )
  219. self.assertEqual(
  220. set(grps[5].dependencies(True).values()),
  221. set( grps[i] for i in range(5))
  222. )
  223. self.assertEqual(
  224. set(grps[4].dependencies(True).values()),
  225. set( grps[i] for i in range(4))
  226. )
  227. grps[2].add_dependencie(grps[0])
  228. self.assertEqual(
  229. set(grps[5].dependencies(True).values()),
  230. set( grps[i] for i in range(5))
  231. )
  232. self.assertEqual(
  233. set(grps[4].dependencies(True).values()),
  234. set( grps[i] for i in range(4))
  235. )
  236. # Inserting circular deps
  237. with self.assertRaises(EditorialModelError):
  238. grps[0].add_dependencie(grps[5])
  239. def test_circular_dep(self):
  240. """ Test circular dependencies detection """
  241. grps = [ EmGroup('group%d' % i) for i in range(10) ]
  242. for i in range(1,10):
  243. grps[i].add_dependencie(grps[i-1])
  244. for i in range(1,10):
  245. for j in range(i+1,10):
  246. with self.assertRaises(EditorialModelError):
  247. grps[i].add_dependencie(grps[j])
  248. def test_d_hash(self):
  249. """ Test the deterministic hash method """
  250. grp = EmGroup('testgrp', display_name = "Test group", help_text="No Help")
  251. e_hash = 0x74657374677270333130363537393137343730343438343139303233393838303936373730323936353536393032313839313536333632313037343435323138313735343936303237373532343436303639363137
  252. self.assertEqual(grp.d_hash(), e_hash)