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 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import unittest
  20. import tempfile
  21. import os
  22. import tests.loader_utils
  23. from lodel.editorial_model.translator import picklefile
  24. from lodel.editorial_model.translator import xmlfile
  25. from lodel.editorial_model.model import EditorialModel
  26. from lodel.editorial_model.components import *
  27. from lodel.editorial_model.exceptions import *
  28. from lodel.settings import Settings
  29. class XmlFileTestCase(unittest.TestCase):
  30. def __init__(self, *args):
  31. super().__init__(*args)
  32. self.tmpfile = None
  33. def setUp(self):
  34. if self.tmpfile is not None:
  35. os.unlink(self.tmpfile)
  36. f_tmp, self.tmpfile = tempfile.mkstemp()
  37. os.close(f_tmp)
  38. def tearDown(self):
  39. if self.tmpfile is not None:
  40. os.unlink(self.tmpfile)
  41. def test_save(self):
  42. emmodel = EditorialModel("test model", description = "Test EM")
  43. cls1 = emmodel.new_class( 'testclass1',
  44. display_name = 'Classe de test 1',
  45. help_text = 'super aide')
  46. c1f1 = cls1.new_field('testfield1', data_handler = 'varchar')
  47. c1f2 = cls1.new_field('testfield2', data_handler = 'varchar')
  48. cls2 = emmodel.new_class('testclass2')
  49. c2f1 = cls2.new_field('testfield1', data_handler = 'varchar')
  50. c2f2 = cls2.new_field('testfield2', data_handler = 'varchar')
  51. grp1 = emmodel.new_group('testgroup1')
  52. grp1.add_components((cls1, c1f1))
  53. grp2 = emmodel.new_group('testgroup2')
  54. grp2.add_components((cls2, c1f2, c2f1, c2f2))
  55. grp2.add_dependency(grp1)
  56. f_tmp, file_name = tempfile.mkstemp()
  57. os.close(f_tmp)
  58. emmodel.save(xmlfile, filename=file_name)
  59. new_model = EditorialModel.load(xmlfile, filename=file_name)
  60. f_tmp, fname = tempfile.mkstemp()
  61. os.close(f_tmp)
  62. new_model.save(xmlfile, filename=fname)
  63. os.unlink(file_name)
  64. os.unlink(fname)
  65. self.assertNotEqual(id(new_model), id(emmodel))
  66. self.assertEqual(new_model.d_hash(), emmodel.d_hash())
  67. def test_abstract_classes(self):
  68. """ Testing xmlfile abtract class handling """
  69. emmodel = EditorialModel("em_test", description = "Test model")
  70. cls1 = emmodel.new_class( 'testclass1',
  71. display_name = "test class 1",
  72. abstract = True)
  73. emmodel.save(xmlfile, filename=self.tmpfile)
  74. emmodel_loaded = xmlfile.load(self.tmpfile)
  75. self.assertEqual( emmodel.d_hash(),
  76. emmodel_loaded.d_hash())
  77. def test_groups(self):
  78. """ Testing xmlfile groups handling """
  79. emmodel = EditorialModel("em_test", description = "test model")
  80. emmodel.new_group( 'test_grp',
  81. display_name = "Test group")
  82. emmodel.save(xmlfile, filename=self.tmpfile)
  83. emmodel_loaded = xmlfile.load(self.tmpfile)
  84. self.assertEqual( emmodel.d_hash(),
  85. emmodel_loaded.d_hash())
  86. def test_groups_population(self):
  87. """ Testing xmlfile groups population handling """
  88. emmodel = EditorialModel("em_test", description = "test model")
  89. cls1 = emmodel.new_class( 'testclass1',
  90. display_name = "test class 1")
  91. cls2 = emmodel.new_class( 'testclass2',
  92. display_name = "test class 2")
  93. cls1f1 = cls1.new_field( 'testfield1',
  94. data_handler = 'varchar')
  95. cls2f1 = cls2.new_field( 'testfield2',
  96. data_handler = 'varchar')
  97. cls2f2 = cls2.new_field( 'testfield3',
  98. data_handler = 'varchar')
  99. grp1 = emmodel.new_group( 'test_grp',
  100. display_name = "Test group")
  101. grp2 = emmodel.new_group( 'test_grp2',
  102. display_name = "Test group2")
  103. grp1.add_components([cls1,cls2])
  104. grp2.add_components([cls1f1,cls2f1, cls2f2])
  105. emmodel.save(xmlfile, filename=self.tmpfile)
  106. emmodel_loaded = xmlfile.load(self.tmpfile)
  107. self.assertEqual( emmodel.d_hash(),
  108. emmodel_loaded.d_hash())
  109. def test_groups_dependencies(self):
  110. """ Testing xmlfile groups population dependencies """
  111. emmodel = EditorialModel("em_test", description = "test model")
  112. grp1 = emmodel.new_group( 'test_grp',
  113. display_name = "Test group")
  114. grp2 = emmodel.new_group( 'test_grp2',
  115. display_name = "Test group2")
  116. grp3 = emmodel.new_group( 'test_grp3',
  117. display_name = "Test group3",
  118. depends = [grp1, grp2])
  119. emmodel.save(xmlfile, filename=self.tmpfile)
  120. emmodel_loaded = xmlfile.load(self.tmpfile)
  121. self.assertEqual( emmodel.d_hash(),
  122. emmodel_loaded.d_hash())
  123. def test_emfield_with_prop_bool(self):
  124. """ Testing xmlfile with bool as property for datahandler """
  125. emmodel = EditorialModel("em_test", description = "test model")
  126. cls1 = emmodel.new_class( 'testclass1',
  127. display_name = "test class 1")
  128. cls1f1 = cls1.new_field( 'testfield1',
  129. data_handler = 'varchar',
  130. nullable = True)
  131. emmodel.save(xmlfile, filename=self.tmpfile)
  132. emmodel_loaded = xmlfile.load(self.tmpfile)
  133. self.assertEqual( emmodel.d_hash(),
  134. emmodel_loaded.d_hash())
  135. def test_emfield_with_prop_tuple(self):
  136. """ Testing xmlfile with iterable as property for datahandler """
  137. emmodel = EditorialModel("em_test", description = "test model")
  138. cls1 = emmodel.new_class( 'testclass1',
  139. display_name = "test class 1")
  140. cls2 = emmodel.new_class( 'testclass2',
  141. display_name = "test class 2")
  142. cls1f1 = cls1.new_field( 'testfield1',
  143. data_handler = 'varchar')
  144. cls2f1 = cls2.new_field( 'testfield2',
  145. data_handler = 'list',
  146. allowed_classes = [cls1, cls2])
  147. cls2f1 = cls2.new_field( 'testfield3',
  148. data_handler = 'varchar',
  149. back_reference = ( 'testclass2',
  150. 'testfield1'))
  151. emmodel.save(xmlfile, filename=self.tmpfile)
  152. emmodel_loaded = xmlfile.load(self.tmpfile)
  153. self.assertEqual( emmodel.d_hash(),
  154. emmodel_loaded.d_hash())
  155. def test_emclass_with_ml_strings(self):
  156. """ Testing xmlfile mlstring handling in classes"""
  157. emmodel = EditorialModel("em_test", description = "test model")
  158. cls1 = emmodel.new_class( 'testclass1',
  159. display_name = { 'eng': "test class 1",
  160. 'fre': 'classe de test 1'})
  161. emmodel.save(xmlfile, filename=self.tmpfile)
  162. emmodel_loaded = xmlfile.load(self.tmpfile)
  163. self.assertEqual( emmodel.d_hash(),
  164. emmodel_loaded.d_hash())
  165. def test_emfield_with_ml_strings(self):
  166. """ Testing xmlfile mlstring handling in data handlers """
  167. emmodel = EditorialModel("em_test", description = "test model")
  168. cls1 = emmodel.new_class( 'testclass1',
  169. display_name = "test class 1")
  170. cls1f1 = cls1.new_field( 'testfield1',
  171. display_name = { 'eng': 'test1',
  172. 'fre': 'test1'},
  173. data_handler = 'varchar')
  174. emmodel.save(xmlfile, filename=self.tmpfile)
  175. emmodel_loaded = xmlfile.load(self.tmpfile)
  176. self.assertEqual( emmodel.d_hash(),
  177. emmodel_loaded.d_hash())
  178. def test_groups_with_ml_strings(self):
  179. """ Testing xmlfile groups handling """
  180. emmodel = EditorialModel("em_test", description = "test model")
  181. emmodel.new_group( 'test_grp',
  182. display_name = { 'eng': "Test group",
  183. 'fre': "Groupe de test"})
  184. emmodel.save(xmlfile, filename=self.tmpfile)
  185. emmodel_loaded = xmlfile.load(self.tmpfile)
  186. self.assertEqual( emmodel.d_hash(),
  187. emmodel_loaded.d_hash())
  188. def test_em_test(self):
  189. """ Testing xmlfile with the test editorial model """
  190. emmodel = picklefile.load(Settings.editorialmodel.emfile)
  191. emmodel.save(xmlfile, filename=self.tmpfile)
  192. emmodel.save(xmlfile, filename = 'empick.xml')
  193. emmodel_loaded = xmlfile.load(self.tmpfile)
  194. emmodel_loaded.save(xmlfile, filename = 'empick2.xml')
  195. #self.assertEqual( emmodel.d_hash(),
  196. # emmodel_loaded.d_hash())