test_cycle_repositories.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. import os
  12. import pytest
  13. from taipy.core.cycle._cycle_fs_repository import _CycleFSRepository
  14. from taipy.core.cycle.cycle import Cycle, CycleId
  15. from taipy.core.exceptions import ModelNotFound
  16. class TestCycleRepositories:
  17. def test_save_and_load(self, cycle: Cycle):
  18. repository = _CycleFSRepository()
  19. repository._save(cycle)
  20. loaded_cycle = repository._load(cycle.id)
  21. assert isinstance(loaded_cycle, Cycle)
  22. assert cycle._frequency == loaded_cycle._frequency
  23. assert cycle._creation_date == loaded_cycle._creation_date
  24. assert cycle._start_date == loaded_cycle._start_date
  25. assert cycle._end_date == loaded_cycle._end_date
  26. assert cycle._name == loaded_cycle._name
  27. assert cycle.id == loaded_cycle.id
  28. assert cycle._properties == loaded_cycle._properties
  29. def test_exists(self, cycle):
  30. repository = _CycleFSRepository()
  31. repository._save(cycle)
  32. assert repository._exists(cycle.id)
  33. assert not repository._exists("not-existed-cycle")
  34. def test_load_all(self, cycle):
  35. repository = _CycleFSRepository()
  36. for i in range(10):
  37. cycle.id = CycleId(f"cycle-{i}")
  38. repository._save(cycle)
  39. data_nodes = repository._load_all()
  40. assert len(data_nodes) == 10
  41. def test_load_all_with_filters(self, cycle):
  42. repository = _CycleFSRepository()
  43. for i in range(10):
  44. cycle.id = CycleId(f"cycle-{i}")
  45. cycle._name = f"cycle-{i}"
  46. repository._save(cycle)
  47. objs = repository._load_all(filters=[{"id": "cycle-2"}])
  48. assert len(objs) == 1
  49. def test_delete(self, cycle):
  50. repository = _CycleFSRepository()
  51. repository._save(cycle)
  52. repository._delete(cycle.id)
  53. with pytest.raises(ModelNotFound):
  54. repository._load(cycle.id)
  55. def test_delete_all(self, cycle):
  56. repository = _CycleFSRepository()
  57. for i in range(10):
  58. cycle.id = CycleId(f"cycle-{i}")
  59. repository._save(cycle)
  60. assert len(repository._load_all()) == 10
  61. repository._delete_all()
  62. assert len(repository._load_all()) == 0
  63. def test_delete_many(self, cycle):
  64. repository = _CycleFSRepository()
  65. for i in range(10):
  66. cycle.id = CycleId(f"cycle-{i}")
  67. repository._save(cycle)
  68. objs = repository._load_all()
  69. assert len(objs) == 10
  70. ids = [x.id for x in objs[:3]]
  71. repository._delete_many(ids)
  72. assert len(repository._load_all()) == 7
  73. def test_search(self, cycle):
  74. repository = _CycleFSRepository()
  75. for i in range(10):
  76. cycle.id = CycleId(f"cycle-{i}")
  77. repository._save(cycle)
  78. cycle.name = f"cycle-{i}"
  79. assert len(repository._load_all()) == 10
  80. objs = repository._search("name", "cycle-2")
  81. assert len(objs) == 1
  82. assert isinstance(objs[0], Cycle)
  83. def test_export(self, tmpdir, cycle):
  84. repository = _CycleFSRepository()
  85. repository._save(cycle)
  86. repository._export(cycle.id, tmpdir.strpath)
  87. dir_path = repository.dir_path
  88. assert os.path.exists(os.path.join(dir_path, f"{cycle.id}.json"))