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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 random
  21. import tests.loader_utils
  22. from lodel.editorial_model.model import EditorialModel
  23. from lodel.leapi import lefactory
  24. class LeFactoryTestCase(unittest.TestCase):
  25. model = None
  26. @classmethod
  27. def setUpClass(cls):
  28. """ Generate an example model for this TestCase """
  29. model = EditorialModel(
  30. "test_model",
  31. description = "Model for LeFactoryTestCase"
  32. )
  33. cls1 = model.new_class('testclass1')
  34. cls2 = model.new_class('testclass2')
  35. cls3 = model.new_class('testclass3', parents = [cls2])
  36. cls4 = model.new_class('testclass4', parents = [cls1, cls3])
  37. cls5 = model.new_class('testclass5', parents = [cls4])
  38. cls6 = model.new_class('testclass6', parents = [cls5])
  39. cls1.new_field('testfield1', data_handler='varchar')
  40. cls1.new_field('testfield2', data_handler='varchar', nullable = True)
  41. cls1.new_field('testfield3', data_handler='varchar', max_length=64)
  42. cls1.new_field('testfield4', data_handler='varchar', max_length=64, nullable=False, uniq=True)
  43. cls1.new_field('id', data_handler='integer', primary_key = True)
  44. cls5.new_field('id2', data_handler='varchar', primary_key = True)
  45. cls.model = model
  46. def test_emclass_sorted_by_deps(self):
  47. """ Test the function that sort EmClass by dependencies """
  48. cls_list = self.model.classes()
  49. for _ in range(100): #Bad sorts algorithm can have random behavior
  50. random.shuffle(cls_list)
  51. sorted_cls = lefactory.emclass_sorted_by_deps(cls_list)
  52. seen = set() # Stores the EmClass allready seen will walking through array
  53. for emclass in sorted_cls:
  54. for parent in emclass.parents_recc:
  55. self.assertIn(parent, seen)
  56. seen |= set((emclass,))
  57. def test_generated_code_syntax(self):
  58. """ Test the function that generate LeObject childs classes code"""
  59. pycode = lefactory.dyncode_from_em(self.model)
  60. pycomp_code = compile(pycode, "dyn.py", 'exec')
  61. exec(pycomp_code, globals())