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_component.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import unittest
  2. from EditorialModel.model import Model
  3. from EditorialModel.components import EmComponent
  4. from EditorialModel.classes import EmClass
  5. from EditorialModel.types import EmType
  6. from EditorialModel.fieldgroups import EmFieldGroup
  7. from EditorialModel.fields import EmField
  8. from Lodel.utils.mlstring import MlString
  9. from EditorialModel.backend.json_backend import EmBackendJson
  10. from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
  11. class TestEmComponent(unittest.TestCase):
  12. def setUp(self):
  13. self.model = Model(EmBackendJson('EditorialModel/test/me.json'))
  14. def test_hashes(self):
  15. """ Testing __hash__ and __eq__ methods """
  16. me1 = Model(EmBackendJson('EditorialModel/test/me.json'))
  17. me2 = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DummyMigrationHandler())
  18. for comp_class in [EmClass, EmType, EmField, EmFieldGroup]:
  19. comp_l1 = me1.components(comp_class)
  20. comp_l2 = me2.components(comp_class)
  21. for i, comp1 in enumerate(comp_l1):
  22. comp2 = comp_l2[i]
  23. self.assertEqual(hash(comp1), hash(comp2), "hashes differs for two EmComponent({}) instanciated from the same backend and files".format(comp_class.__name__))
  24. self.assertTrue(comp1 == comp2)
  25. if not comp1.modify_rank(1):
  26. continue #modification not made, drop this test
  27. self.assertNotEqual(hash(comp1), hash(comp2), "hashes are the same after a modification of rank on one of the two components")
  28. self.assertFalse(comp1 == comp2)
  29. comp2.modify_rank(1)
  30. self.assertEqual(hash(comp1), hash(comp2), "hashes differs for two EmComponent({}) after applying the same modifications on both".format(comp_class.__name__))
  31. self.assertTrue(comp1 == comp2)
  32. def test_virtual_methods(self):
  33. """ Testing virtual methods """
  34. with self.assertRaises(NotImplementedError):
  35. _ = EmComponent(self.model, self.model.new_uid(), 'Invalide')
  36. def test_modify_rank(self):
  37. """ Testing modify_rank and set_rank method """
  38. cls = self.model.classes()[0]
  39. orig_rank = cls.rank
  40. cls.modify_rank(1)
  41. self.assertEqual(orig_rank, cls.rank - 1)
  42. cls.modify_rank(-1)
  43. self.assertEqual(orig_rank, cls.rank)
  44. cls.set_rank(1)
  45. self.assertEqual(cls.rank, 1)
  46. cls.set_rank(2)
  47. self.assertEqual(cls.rank, 2)
  48. max_rank = cls.get_max_rank()
  49. cls.set_rank(max_rank)
  50. self.assertEqual(cls.rank, max_rank)
  51. self.assertFalse(cls.modify_rank(1))
  52. self.assertFalse(cls.modify_rank(-10))
  53. with self.assertRaises(ValueError):
  54. cls.set_rank(0)
  55. with self.assertRaises(ValueError):
  56. cls.set_rank(10)
  57. with self.assertRaises(ValueError):
  58. cls.set_rank(-10)
  59. def test_check(self):
  60. """ Testing check method """
  61. cls = self.model.classes()[0]
  62. cls.rank = 10000
  63. cls.check()
  64. self.assertEqual(cls.rank, cls.get_max_rank())
  65. cls.rank = -1000
  66. cls.check()
  67. self.assertEqual(cls.rank, 1)
  68. def test_dump(self):
  69. """ Testing dump methods """
  70. for comp in self.model.components():
  71. dmp = comp.attr_dump()
  72. self.assertNotIn('uid', dmp)
  73. self.assertNotIn('model', dmp)
  74. self.assertTrue(dmp['help_text'] is None or isinstance(dmp['help_text'], MlString))
  75. self.assertTrue(dmp['string'] is None or isinstance(dmp['string'], MlString))
  76. for dmp_f in dmp:
  77. self.assertFalse(dmp_f.startswith('_'))
  78. def test_uniq_name(self):
  79. """ Testing uniq_name method """
  80. names_l = []
  81. for comp in self.model.components():
  82. #Should be uniq only for types and classes
  83. if isinstance(comp, EmType) or isinstance(comp, EmClass):
  84. self.assertNotIn(comp.uniq_name, names_l)
  85. names_l.append(comp.uniq_name)