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_fieldgroups.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import os
  2. import logging
  3. from unittest import TestCase
  4. from EditorialModel.fieldgroups import EmFieldGroup
  5. from EditorialModel.classes import EmClass
  6. from EditorialModel.types import EmType
  7. from EditorialModel.fields import EmField
  8. from Lodel.utils.mlstring import MlString
  9. from EditorialModel.model import Model
  10. from EditorialModel.backend.json_backend import EmBackendJson
  11. #=###########=#
  12. # TESTS SETUP #
  13. #=###########=#
  14. TEST_FIELDGROUP_DBNAME = 'test_em_fieldgroup_db.sqlite'
  15. EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
  16. EM_TEST_OBJECT = None
  17. def setUpModule():
  18. global EM_TEST_OBJECT
  19. EM_TEST_OBJECT = Model(EmBackendJson(EM_TEST))
  20. logging.basicConfig(level=logging.CRITICAL)
  21. def tearDownModule():
  22. pass
  23. class FieldGroupTestCase(TestCase):
  24. def setUp(self):
  25. pass
  26. #======================#
  27. # EmFielgroup.__init__ #
  28. #======================#
  29. class TestInit(FieldGroupTestCase):
  30. def setUp(self):
  31. super(TestInit, self).setUp()
  32. self.tfgs = [
  33. {"name": "testfg1", "string": MlString({"fre": "Gens"}), "help_text": MlString({}), "class_id": 1},
  34. {"name": "testfg2", "string": MlString({"fre": "Gens"}), "help_text": MlString({}), "class_id": 1},
  35. {"name": "testfg3", "string": MlString({"fre": "Civilité"}), "help_text": MlString({}), "class_id": 2}
  36. ]
  37. for tfg in self.tfgs:
  38. fieldgroup = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, tfg)
  39. def test_init(self):
  40. """ Test that EmFieldgroup are correctly instanciated compare to self.tfg """
  41. for tfg in self.tfgs:
  42. fieldgroup = EM_TEST_OBJECT.component(tfg['uid'])
  43. for attr in tfg:
  44. if attr != 'uid':
  45. v = tfg[attr]
  46. self.assertEqual(getattr(fieldgroup, attr), v, "The '" + attr + "' property fetched from backend doesn't match the excepted value")
  47. def test_init_badargs(self):
  48. """ Tests that EmFieldGroup init fails when bad arguments are given"""
  49. baduid = self.tfgs[2]['uid'] + 4096
  50. badname = 'NonExistingName'
  51. # TODO Voir si on garde le return False de Model.component() ou si on utilise plutôt une exception EmComponentNotExistError en modifiant le reste du code source pour gérer ce cas
  52. self.assertFalse(EM_TEST_OBJECT.component(baduid), msg="Should be False because fieldgroup with id " + str(baduid) + " should not exist")
  53. self.assertFalse(EM_TEST_OBJECT.component(badname), msg="Should be False because fieldgroup with id " + str(badname) + " should not exist")
  54. self.assertFalse(EM_TEST_OBJECT.component(print), msg="Should be False when a function name is given as argument")
  55. with self.assertRaises(TypeError, msg="Should raise when crazy arguments are given"):
  56. fieldgroup = EM_TEST_OBJECT.component(['hello', 'world'])
  57. #=====================#
  58. # EmFieldgroup.create #
  59. #=====================#
  60. class TestCreate(FieldGroupTestCase):
  61. def test_create(self):
  62. """Does create actually create a fieldgroup ?"""
  63. params = {
  64. 'EmClass entity instance': EM_TEST_OBJECT.component(1),
  65. 'EmClass entry instance': EM_TEST_OBJECT.component(2)
  66. }
  67. for i, param_name in enumerate(params):
  68. arg = params[param_name]
  69. if isinstance(arg, EmClass):
  70. cl = arg
  71. else:
  72. cl = EM_TEST_OBJECT.component(arg)
  73. fieldgroup_name = 'new_fg' + str(i)
  74. fieldgroup = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, {'name': fieldgroup_name, 'class_id': arg.uid})
  75. self.assertEqual(fieldgroup.name, fieldgroup_name, "Model.create_component() doesn't instanciate name correctly")
  76. self.assertEqual(fieldgroup.class_id, cl.uid, "Model.create_component() doesn't instanciate class_id correctly")
  77. nfg = EM_TEST_OBJECT.component(fieldgroup.uid)
  78. # Checking object property
  79. for fname in fieldgroup.__dict__:
  80. self.assertEqual(getattr(nfg, fname), getattr(fieldgroup, fname), "Msg inconsistency when a created fieldgroup is fetched from the backend (in " + fname + " property)")
  81. def test_create_badargs(self):
  82. """ Does create fails when badargs given ? """
  83. badargs = {
  84. 'EmClass type (not an instance)': EmClass,
  85. 'Non Existing id': 9000,
  86. 'Another component instance': EM_TEST_OBJECT.create_component(EmType.__name__, {'name': 'fooType', 'class_id': EM_TEST_OBJECT.component(1).uid}),
  87. 'A function': print
  88. }
  89. for i, badarg_name in enumerate(badargs):
  90. with self.assertRaises(TypeError, msg="Should raise because trying to give " + badarg_name + " an em_class object as value"):
  91. fieldgroup = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, {'name': 'new_fg' + i, 'class_id': badargs[badarg_name].uid})
  92. # Creating a fieldgroup to test duplicate name
  93. exfg = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, {'name': 'existingfg', 'class_id': EM_TEST_OBJECT.component(1).uid})
  94. badargs = {
  95. 'an integer': (42, AttributeError),
  96. 'a function': (print, AttributeError),
  97. 'an EmClass': (EM_TEST_OBJECT.component(2), AttributeError)
  98. }
  99. for badarg_name in badargs:
  100. (badarg, expt) = badargs[badarg_name]
  101. with self.assertRaises(expt, msg="Should raise because trying to give " + badarg_name + " as first argument"):
  102. fieldgroup = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, {'name': badarg, 'class_id': EM_TEST_OBJECT.component(1).uid})
  103. #=====================#
  104. # EmFieldgroup.fields #
  105. #=====================#
  106. class TestFields(FieldGroupTestCase):
  107. def setUp(self):
  108. super(TestFields, self).setUp()
  109. self.fg1 = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, {'name': 'testfg1', 'class_id': EM_TEST_OBJECT.component(1).uid})
  110. self.fg2 = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, {'name': 'testfg2', 'class_id': EM_TEST_OBJECT.component(2).uid})
  111. self.fg3 = EM_TEST_OBJECT.create_component(EmFieldGroup.__name__, {'name': 'testfg3', 'class_id': EM_TEST_OBJECT.component(1).uid})
  112. def test_fields(self):
  113. """ Does it returns actually associated fields ? """
  114. # Creating fields
  115. test_fields1 = [
  116. {'name': 'field1', 'fieldgroup_id': self.fg1.uid, 'fieldtype': 'integer'},
  117. {'name': 'field2', 'fieldgroup_id': self.fg1.uid, 'fieldtype': 'integer'},
  118. {'name': 'field4', 'fieldgroup_id': self.fg1.uid, 'fieldtype': 'integer'}
  119. ]
  120. test_fields2 = [
  121. {'name': 'field3', 'fieldgroup_id': self.fg2.uid, 'fieldtype': 'integer'}
  122. ]
  123. expected1 = []
  124. for finfo in test_fields1:
  125. field = EM_TEST_OBJECT.create_component(EmField.__name__, finfo)
  126. expected1.append(field.uid)
  127. for finfo in test_fields2:
  128. field = EM_TEST_OBJECT.create_component(EmField.__name__, finfo)
  129. expected1 = set(expected1)
  130. tests = {
  131. 'newly': EM_TEST_OBJECT.component(self.fg1.uid),
  132. 'old': self.fg1
  133. }
  134. for name in tests:
  135. fieldgroup = tests[name]
  136. flist = fieldgroup.fields()
  137. res = []
  138. for field in flist:
  139. res.append(field.uid)
  140. self.assertEqual(set(res), set(expected1))
  141. def test_empty_fields(self):
  142. """ Testing fields method on an empty fieldgroup """
  143. fieldgroup = self.fg3
  144. fields_list = fieldgroup.fields()
  145. self.assertEqual(len(fields_list), 0)