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_translator_xmlfile.py 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. import tempfile
  4. import os
  5. import tests.loader_utils
  6. from lodel.editorial_model.translator import picklefile
  7. from lodel.editorial_model.translator import xmlfile
  8. from lodel.editorial_model.model import EditorialModel
  9. from lodel.editorial_model.components import *
  10. from lodel.editorial_model.exceptions import *
  11. from lodel.settings import Settings
  12. class XmlFileTestCase(unittest.TestCase):
  13. def __init__(self, *args):
  14. super().__init__(*args)
  15. self.tmpfile = None
  16. def setUp(self):
  17. if self.tmpfile is not None:
  18. os.unlink(self.tmpfile)
  19. f_tmp, self.tmpfile = tempfile.mkstemp()
  20. os.close(f_tmp)
  21. def tearDown(self):
  22. if self.tmpfile is not None:
  23. os.unlink(self.tmpfile)
  24. def test_save(self):
  25. emmodel = EditorialModel("test model", description = "Test EM")
  26. cls1 = emmodel.new_class( 'testclass1',
  27. display_name = 'Classe de test 1',
  28. help_text = 'super aide')
  29. c1f1 = cls1.new_field('testfield1', data_handler = 'varchar')
  30. c1f2 = cls1.new_field('testfield2', data_handler = 'varchar')
  31. cls2 = emmodel.new_class('testclass2')
  32. c2f1 = cls2.new_field('testfield1', data_handler = 'varchar')
  33. c2f2 = cls2.new_field('testfield2', data_handler = 'varchar')
  34. grp1 = emmodel.new_group('testgroup1')
  35. grp1.add_components((cls1, c1f1))
  36. grp2 = emmodel.new_group('testgroup2')
  37. grp2.add_components((cls2, c1f2, c2f1, c2f2))
  38. grp2.add_dependencie(grp1)
  39. f_tmp, file_name = tempfile.mkstemp()
  40. os.close(f_tmp)
  41. emmodel.save(xmlfile, filename=file_name)
  42. new_model = EditorialModel.load(xmlfile, filename=file_name)
  43. f_tmp, fname = tempfile.mkstemp()
  44. os.close(f_tmp)
  45. new_model.save(xmlfile, filename=fname)
  46. os.unlink(file_name)
  47. os.unlink(fname)
  48. self.assertNotEqual(id(new_model), id(emmodel))
  49. self.assertEqual(new_model.d_hash(), emmodel.d_hash())
  50. def test_abstract_classes(self):
  51. """ Testing xmlfile abtract class handling """
  52. emmodel = EditorialModel("em_test", description = "Test model")
  53. cls1 = emmodel.new_class( 'testclass1',
  54. display_name = "test class 1",
  55. abstract = True)
  56. emmodel.save(xmlfile, filename=self.tmpfile)
  57. emmodel_loaded = xmlfile.load(self.tmpfile)
  58. self.assertEqual( emmodel.d_hash(),
  59. emmodel_loaded.d_hash())
  60. def test_groups(self):
  61. """ Testing xmlfile groups handling """
  62. emmodel = EditorialModel("em_test", description = "test model")
  63. emmodel.new_group( 'test_grp',
  64. display_name = "Test group")
  65. emmodel.save(xmlfile, filename=self.tmpfile)
  66. emmodel_loaded = xmlfile.load(self.tmpfile)
  67. self.assertEqual( emmodel.d_hash(),
  68. emmodel_loaded.d_hash())
  69. def test_groups_population(self):
  70. """ Testing xmlfile groups population handling """
  71. emmodel = EditorialModel("em_test", description = "test model")
  72. cls1 = emmodel.new_class( 'testclass1',
  73. display_name = "test class 1")
  74. cls2 = emmodel.new_class( 'testclass2',
  75. display_name = "test class 2")
  76. cls1f1 = cls1.new_field( 'testfield1',
  77. data_handler = 'varchar')
  78. cls2f1 = cls2.new_field( 'testfield2',
  79. data_handler = 'varchar')
  80. cls2f2 = cls2.new_field( 'testfield3',
  81. data_handler = 'varchar')
  82. grp1 = emmodel.new_group( 'test_grp',
  83. display_name = "Test group")
  84. grp2 = emmodel.new_group( 'test_grp2',
  85. display_name = "Test group2")
  86. grp1.add_components([cls1,cls2])
  87. grp2.add_components([cls1f1,cls2f1, cls2f2])
  88. emmodel.save(xmlfile, filename=self.tmpfile)
  89. emmodel_loaded = xmlfile.load(self.tmpfile)
  90. self.assertEqual( emmodel.d_hash(),
  91. emmodel_loaded.d_hash())
  92. def test_groups_dependencies(self):
  93. """ Testing xmlfile groups population dependencies """
  94. emmodel = EditorialModel("em_test", description = "test model")
  95. grp1 = emmodel.new_group( 'test_grp',
  96. display_name = "Test group")
  97. grp2 = emmodel.new_group( 'test_grp2',
  98. display_name = "Test group2")
  99. grp3 = emmodel.new_group( 'test_grp3',
  100. display_name = "Test group3",
  101. depends = [grp1, grp2])
  102. emmodel.save(xmlfile, filename=self.tmpfile)
  103. emmodel_loaded = xmlfile.load(self.tmpfile)
  104. self.assertEqual( emmodel.d_hash(),
  105. emmodel_loaded.d_hash())
  106. def test_emfield_with_prop_bool(self):
  107. """ Testing xmlfile with bool as property for datahandler """
  108. emmodel = EditorialModel("em_test", description = "test model")
  109. cls1 = emmodel.new_class( 'testclass1',
  110. display_name = "test class 1")
  111. cls1f1 = cls1.new_field( 'testfield1',
  112. data_handler = 'varchar',
  113. nullable = True)
  114. emmodel.save(xmlfile, filename=self.tmpfile)
  115. emmodel_loaded = xmlfile.load(self.tmpfile)
  116. self.assertEqual( emmodel.d_hash(),
  117. emmodel_loaded.d_hash())
  118. def test_emfield_with_prop_tuple(self):
  119. """ Testing xmlfile with iterable as property for datahandler """
  120. emmodel = EditorialModel("em_test", description = "test model")
  121. cls1 = emmodel.new_class( 'testclass1',
  122. display_name = "test class 1")
  123. cls2 = emmodel.new_class( 'testclass2',
  124. display_name = "test class 2")
  125. cls1f1 = cls1.new_field( 'testfield1',
  126. data_handler = 'varchar')
  127. cls2f1 = cls2.new_field( 'testfield2',
  128. data_handler = 'list',
  129. allowed_classes = [cls1, cls2])
  130. cls2f1 = cls2.new_field( 'testfield3',
  131. data_handler = 'varchar',
  132. back_reference = ( 'testclass2',
  133. 'testfield1'))
  134. emmodel.save(xmlfile, filename=self.tmpfile)
  135. emmodel_loaded = xmlfile.load(self.tmpfile)
  136. self.assertEqual( emmodel.d_hash(),
  137. emmodel_loaded.d_hash())
  138. def test_emclass_with_ml_strings(self):
  139. """ Testing xmlfile mlstring handling in classes"""
  140. emmodel = EditorialModel("em_test", description = "test model")
  141. cls1 = emmodel.new_class( 'testclass1',
  142. display_name = { 'eng': "test class 1",
  143. 'fre': 'classe de test 1'})
  144. emmodel.save(xmlfile, filename=self.tmpfile)
  145. emmodel_loaded = xmlfile.load(self.tmpfile)
  146. self.assertEqual( emmodel.d_hash(),
  147. emmodel_loaded.d_hash())
  148. def test_emfield_with_ml_strings(self):
  149. """ Testing xmlfile mlstring handling in data handlers """
  150. emmodel = EditorialModel("em_test", description = "test model")
  151. cls1 = emmodel.new_class( 'testclass1',
  152. display_name = "test class 1")
  153. cls1f1 = cls1.new_field( 'testfield1',
  154. display_name = { 'eng': 'test1',
  155. 'fre': 'test1'},
  156. data_handler = 'varchar')
  157. emmodel.save(xmlfile, filename=self.tmpfile)
  158. emmodel_loaded = xmlfile.load(self.tmpfile)
  159. self.assertEqual( emmodel.d_hash(),
  160. emmodel_loaded.d_hash())
  161. def test_groups_with_ml_strings(self):
  162. """ Testing xmlfile groups handling """
  163. emmodel = EditorialModel("em_test", description = "test model")
  164. emmodel.new_group( 'test_grp',
  165. display_name = { 'eng': "Test group",
  166. 'fre': "Groupe de test"})
  167. emmodel.save(xmlfile, filename=self.tmpfile)
  168. emmodel_loaded = xmlfile.load(self.tmpfile)
  169. self.assertEqual( emmodel.d_hash(),
  170. emmodel_loaded.d_hash())
  171. def test_em_test(self):
  172. """ Testing xmlfile with the test editorial model """
  173. emmodel = picklefile.load(Settings.editorialmodel.emfile)
  174. emmodel.save(xmlfile, filename=self.tmpfile)
  175. emmodel.save(xmlfile, filename = 'empick.xml')
  176. emmodel_loaded = xmlfile.load(self.tmpfile)
  177. emmodel_loaded.save(xmlfile, filename = 'empick2.xml')
  178. #self.assertEqual( emmodel.d_hash(),
  179. # emmodel_loaded.d_hash())