test_reason.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from taipy.core.reason import Reasons
  12. def test_create_reason():
  13. reason = Reasons("entity_id")
  14. assert reason.entity_id == "entity_id"
  15. assert reason._reasons == {}
  16. assert reason
  17. assert not reason._entity_id_exists_in_reason("entity_id")
  18. assert reason.reasons == ""
  19. def test_add_and_remove_reason():
  20. reason = Reasons("entity_id")
  21. reason._add_reason("entity_id_1", "Some reason")
  22. assert reason._reasons == {"entity_id_1": {"Some reason"}}
  23. assert not reason
  24. assert reason._entity_id_exists_in_reason("entity_id_1")
  25. assert reason.reasons == "Some reason."
  26. reason._add_reason("entity_id_1", "Another reason")
  27. reason._add_reason("entity_id_2", "Some more reason")
  28. assert reason._reasons == {"entity_id_1": {"Some reason", "Another reason"}, "entity_id_2": {"Some more reason"}}
  29. assert not reason
  30. assert reason._entity_id_exists_in_reason("entity_id_1")
  31. assert reason._entity_id_exists_in_reason("entity_id_2")
  32. reason._remove_reason("entity_id_1", "Some reason")
  33. assert reason._reasons == {"entity_id_1": {"Another reason"}, "entity_id_2": {"Some more reason"}}
  34. assert not reason
  35. assert reason._entity_id_exists_in_reason("entity_id_1")
  36. assert reason._entity_id_exists_in_reason("entity_id_2")
  37. reason._remove_reason("entity_id_2", "Some more reason")
  38. assert reason._reasons == {"entity_id_1": {"Another reason"}}
  39. assert not reason
  40. assert reason._entity_id_exists_in_reason("entity_id_1")
  41. assert not reason._entity_id_exists_in_reason("entity_id_2")
  42. reason._remove_reason("entity_id_1", "Another reason")
  43. assert reason._reasons == {}
  44. assert reason
  45. assert not reason._entity_id_exists_in_reason("entity_id_1")
  46. def test_get_reason_string_from_reason():
  47. reason = Reasons("entity_id")
  48. reason._add_reason("entity_id_1", "Some reason")
  49. assert reason.reasons == "Some reason."
  50. reason._add_reason("entity_id_2", "Some more reason")
  51. assert reason.reasons == "Some reason; Some more reason."
  52. reason._add_reason("entity_id_1", "Another reason")
  53. assert reason.reasons.count(";") == 2
  54. assert "Some reason" in reason.reasons
  55. assert "Another reason" in reason.reasons
  56. assert "Some more reason" in reason.reasons