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_model.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import unittest
  2. from EditorialModel.model import Model
  3. from EditorialModel.classes import EmClass
  4. from EditorialModel.types import EmType
  5. from EditorialModel.fieldgroups import EmFieldGroup
  6. from EditorialModel.fields import EmField
  7. from EditorialModel.backend.json_backend import EmBackendJson
  8. #from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
  9. from EditorialModel.migrationhandler.django import DjangoMigrationHandler
  10. class TestModel(unittest.TestCase):
  11. def setUp(self):
  12. self.me = Model(EmBackendJson('EditorialModel/test/me.json'))
  13. def test_init(self):
  14. """ Instanciation test """
  15. model = Model(EmBackendJson('EditorialModel/test/me.json'))
  16. self.assertTrue(isinstance(model, Model))
  17. model = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DjangoMigrationHandler('test',debug=True))
  18. self.assertTrue(isinstance(model, Model))
  19. def test_components(self):
  20. """ Test components fetching """
  21. for comp_class in [EmClass, EmType, EmField, EmFieldGroup]:
  22. comp_l = self.me.components(comp_class)
  23. for component in comp_l:
  24. self.assertTrue(isinstance(component, comp_class), "Model.components method doesn't return EmComponent of the right type. Asked for {} but got {}".format(type(comp_class), type(component)))
  25. def test_sort_components(self):
  26. """ Test that Model.sort_components method actually sort components """
  27. # disordering an EmClass
  28. cl_l = self.me.components(EmClass)
  29. last_class = cl_l[0]
  30. last_class.rank = 10000
  31. self.me.sort_components(EmClass)
  32. self.assertEqual(self.me._components['EmClass'][-1].uid, last_class.uid, "The sort_components method doesn't really sort by rank")
  33. def test_new_uid(self):
  34. """ Test that model.new_uid return a new uniq uid """
  35. new_uid = self.me.new_uid()
  36. self.assertNotIn(new_uid, self.me._components['uids'].keys())
  37. def test_hash(self):
  38. """ Test that __hash__ and __eq__ work properly on models """
  39. me1 = Model(EmBackendJson('EditorialModel/test/me.json'))
  40. me2 = Model(EmBackendJson('EditorialModel/test/me.json'), migration_handler=DjangoMigrationHandler('test', debug=True))
  41. self.assertEqual(hash(me1), hash(me2), "When instanciate from the same backend & file but with another migration handler the hashes differs")
  42. self.assertTrue(me1, me2)
  43. cl_l = me1.classes()
  44. cl_l[0].modify_rank(1)
  45. self.assertNotEqual(hash(me1), hash(me2), "After a class rank modification the hashes are the same")
  46. self.assertFalse(me1, me2)
  47. cl_l = me2.classes()
  48. cl_l[0].modify_rank(1)
  49. self.assertEqual(hash(me), hash(me2), "After doing sames modifications in the two models the hashes differs")
  50. self.assertTrue(me1, me2)