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

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