暫無描述
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_types.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import unittest
  2. from EditorialModel.model import Model
  3. from EditorialModel.classtypes import EmNature
  4. from EditorialModel.backend.json_backend import EmBackendJson
  5. class TypeTestCase(unittest.TestCase):
  6. def setUp(self):
  7. self.model = Model(EmBackendJson('EditorialModel/test/me.json'))
  8. self.rubrique = self.model.component(14)
  9. self.article = self.model.component(5)
  10. self.personne = self.model.component(6)
  11. self.soustitre_field = self.model.component(7)
  12. self.age_field = self.model.component(18)
  13. self.nom_field = self.model.component(9)
  14. self.numero = self.model.component(19)
  15. self.gens_group = self.model.component(17)
  16. self.info_group = self.model.component(3)
  17. self.couleur_group = self.model.component(20)
  18. self.couleur_field = self.model.component(21)
  19. class TestSelectField(TypeTestCase):
  20. def test_select_field(self):
  21. """ Testing optionnal field selection """
  22. self.personne.select_field(self.age_field)
  23. self.assertIn(self.age_field, self.personne.selected_fields())
  24. self.assertIn(self.age_field.uid, self.personne.fields_list)
  25. def test_unselect_field(self):
  26. """ Testing optionnal field unselection """
  27. self.article.unselect_field(self.soustitre_field)
  28. self.assertNotIn(self.soustitre_field, self.article.selected_fields())
  29. self.assertNotIn(self.soustitre_field.uid, self.article.fields_list)
  30. def test_select_field_invalid(self):
  31. """ Testing optionnal field selection with invalid fields """
  32. with self.assertRaises(ValueError):
  33. self.personne.select_field(self.nom_field)
  34. with self.assertRaises(ValueError):
  35. self.personne.select_field(self.soustitre_field)
  36. class TestTypeHierarchy(TypeTestCase):
  37. def test_add_superior(self):
  38. """ Testing add_superior() """
  39. self.numero.add_superior(self.rubrique, EmNature.PARENT)
  40. self.assertIn(self.rubrique, self.numero.superiors()[EmNature.PARENT])
  41. self.assertIn(self.rubrique.uid, self.numero.superiors_list[EmNature.PARENT])
  42. self.assertIn(self.numero, self.rubrique.subordinates()[EmNature.PARENT])
  43. # add it twice, it should not be listed twice
  44. self.numero.add_superior(self.rubrique, EmNature.PARENT)
  45. self.assertEqual(1, len(self.numero.superiors()[EmNature.PARENT]))
  46. def test_del_superior(self):
  47. """ Testing del_superior() """
  48. # rubrique should be a superior of article
  49. self.assertIn(self.rubrique, self.article.superiors()[EmNature.PARENT])
  50. # article should be in rubrique subordinates
  51. self.assertIn(self.article, self.rubrique.subordinates()[EmNature.PARENT])
  52. self.article.del_superior(self.rubrique, EmNature.PARENT)
  53. # article should not have EmNature.PARENT superior anymore
  54. with self.assertRaises(KeyError):
  55. _ = self.article.superiors()[EmNature.PARENT]
  56. # article should not be in rubrique subordinates
  57. self.assertNotIn(self.article, self.rubrique.subordinates()[EmNature.PARENT])
  58. # but rubrique should still be a subordinate of itself
  59. self.assertIn(self.rubrique, self.rubrique.subordinates()[EmNature.PARENT])
  60. # test preservation of superiors of other nature
  61. def test_bad_hierarchy(self):
  62. """ testing bad use of hierarchy """
  63. # add a superior of different classtype
  64. with self.assertRaises(ValueError):
  65. self.numero.add_superior(self.personne, EmNature.PARENT)
  66. # add a superior with bad nature
  67. with self.assertRaises(ValueError):
  68. self.numero.add_superior(self.rubrique, EmNature.IDENTITY)
  69. # delete an invalid superior
  70. self.article.del_superior(self.numero, EmNature.PARENT)
  71. class TestDeleteTypes(TypeTestCase):
  72. def test_delete_types(self):
  73. """ Testing EmType deletion """
  74. # should be okay to delete article
  75. article_id = self.article.uid
  76. self.assertTrue(self.model.delete_component(self.article.uid))
  77. # and it should not exist anymore
  78. self.assertFalse(self.model.component(article_id))
  79. # relations with other type should be deleted
  80. self.assertNotIn(self.article, self.rubrique.subordinates()[EmNature.PARENT])
  81. # rubrique has subordinates, should not be okay to delete
  82. self.assertFalse(self.model.delete_component(self.rubrique.uid))