test_reason.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright 2021-2025 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 ReasonCollection
  12. def test_create_reason():
  13. reason_collection = ReasonCollection()
  14. assert reason_collection._reasons == {}
  15. assert reason_collection
  16. assert not reason_collection._entity_id_exists_in_reason("entity_id")
  17. assert reason_collection.reasons == ""
  18. def test_add_and_remove_reason():
  19. reason_collection = ReasonCollection()
  20. reason_collection._add_reason("entity_id_1", "Some reason")
  21. assert reason_collection._reasons == {"entity_id_1": {"Some reason"}}
  22. assert not reason_collection
  23. assert reason_collection._entity_id_exists_in_reason("entity_id_1")
  24. assert reason_collection.reasons == "Some reason."
  25. reason_collection._add_reason("entity_id_1", "Another reason")
  26. reason_collection._add_reason("entity_id_2", "Some more reason")
  27. assert reason_collection._reasons == {
  28. "entity_id_1": {"Some reason", "Another reason"},
  29. "entity_id_2": {"Some more reason"},
  30. }
  31. assert not reason_collection
  32. assert reason_collection._entity_id_exists_in_reason("entity_id_1")
  33. assert reason_collection._entity_id_exists_in_reason("entity_id_2")
  34. reason_collection._remove_reason("entity_id_1", "Some reason")
  35. assert reason_collection._reasons == {"entity_id_1": {"Another reason"}, "entity_id_2": {"Some more reason"}}
  36. assert not reason_collection
  37. assert reason_collection._entity_id_exists_in_reason("entity_id_1")
  38. assert reason_collection._entity_id_exists_in_reason("entity_id_2")
  39. reason_collection._remove_reason("entity_id_2", "Some more reason")
  40. assert reason_collection._reasons == {"entity_id_1": {"Another reason"}}
  41. assert not reason_collection
  42. assert reason_collection._entity_id_exists_in_reason("entity_id_1")
  43. assert not reason_collection._entity_id_exists_in_reason("entity_id_2")
  44. reason_collection._remove_reason("entity_id_1", "Another reason")
  45. assert reason_collection._reasons == {}
  46. assert reason_collection
  47. assert not reason_collection._entity_id_exists_in_reason("entity_id_1")
  48. def test_get_reason_string_from_reason():
  49. reason_collection = ReasonCollection()
  50. reason_collection._add_reason("entity_id_1", "Some reason")
  51. assert reason_collection.reasons == "Some reason."
  52. reason_collection._add_reason("entity_id_2", "Some more reason")
  53. assert reason_collection.reasons == "Some reason; Some more reason."
  54. reason_collection._add_reason("entity_id_1", "Another reason")
  55. assert reason_collection.reasons.count(";") == 2
  56. assert "Some reason" in reason_collection.reasons
  57. assert "Another reason" in reason_collection.reasons
  58. assert "Some more reason" in reason_collection.reasons