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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 EditorialModel.backend.json_backend import EmBackendJson
  9. from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
  10. class TestEmComponent(unittest.TestCase):
  11. def setUp(self):
  12. self.me = Model(EmBackendJson('EditorialModel/test/me.json'))
  13. def test_init(self):
  14. """ Testing that __init__ is abstract for an EmComponent """
  15. with self.assertRaises(NotImplementedError):
  16. foo = EmComponent(self.me, self.me.new_uid(), 'invalid instanciation')
  17. def test_hashes(self):
  18. """ Testing __hash__ and __eq__ methos """
  19. me1 = Model(EmBackendJson('EditorialModel/test/me.json'))
  20. me2 = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler = DummyMigrationHandler(True))
  21. for comp_class in [EmClass, EmType, EmField, EmFieldGroup]:
  22. comp_l1 = me1.components(comp_class)
  23. comp_l2 = me2.components(comp_class)
  24. for i, comp1 in enumerate(comp_l1):
  25. comp2 = comp_l2[i]
  26. self.assertEqual(hash(comp1), hash(comp2), "hashes differs for two EmComponent({}) instanciated from the same backend and files".format(comp_class.__name__))
  27. self.assertTrue(comp1 == comp2)
  28. comp1.modify_rank(1)
  29. self.assertNotEqual(hash(comp1), hash(comp2), "hashes are the same after a modification of rank on one of the two components")
  30. self.assertFalse(comp1 == comp2)
  31. comp2.modify_rank(2)
  32. self.assertEqual(hash(comp1), hash(comp2), "hashes differs for two EmComponent({}) after applying the same modifications on both".format(comp_class.__name__))
  33. self.assertTrue(comp1 == comp2)
  34. def test_modify_rank(self):
  35. """ Testing modify_rank and set_rank method """
  36. cls = self.me.classes()[0]
  37. orig_rank = cls.rank
  38. cls.modify_rank(1)
  39. self.assertEqual(orig_rank, cls.rank - 1)
  40. cls.modify_rank(-1)
  41. self.assertEqual(orig_rank, cls.rank)
  42. cls.set_rank(1)
  43. self.assertEqual(cls.rank, 1)
  44. cls.set_rank(2)
  45. self.assertEqual(cls.rank, 2)
  46. max_rank = cls.get_max_rank()
  47. cls.set_rank(max_rank)
  48. self.assertEqual(cls.rank, max_rank)
  49. with self.assertRaises(ValueError):
  50. cls.modify_rank(1)
  51. with self.assertRaises(ValueError):
  52. 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.me.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)