mocks.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import dataclasses
  12. import pathlib
  13. from dataclasses import dataclass
  14. from typing import Any, Dict, Optional
  15. from taipy.common.config import Config
  16. from taipy.core._repository._abstract_converter import _AbstractConverter
  17. from taipy.core._repository._filesystem_repository import _FileSystemRepository
  18. from taipy.core._version._version_manager import _VersionManager
  19. @dataclass
  20. class MockObj:
  21. def __init__(self, id: str, name: str, version: Optional[str] = None) -> None:
  22. self.id = id
  23. self.name = name
  24. if version:
  25. self._version = version
  26. else:
  27. self._version = _VersionManager._get_latest_version()
  28. @dataclass
  29. class MockModel: # type: ignore
  30. id: str
  31. name: str
  32. version: str
  33. def to_dict(self):
  34. return dataclasses.asdict(self)
  35. @staticmethod
  36. def from_dict(data: Dict[str, Any]):
  37. return MockModel(id=data["id"], name=data["name"], version=data["version"])
  38. def _to_entity(self):
  39. return MockObj(id=self.id, name=self.name, version=self.version)
  40. @classmethod
  41. def _from_entity(cls, entity: MockObj):
  42. return MockModel(id=entity.id, name=entity.name, version=entity._version)
  43. def to_list(self):
  44. return [self.id, self.name, self.version]
  45. class MockConverter(_AbstractConverter):
  46. @classmethod
  47. def _entity_to_model(cls, entity):
  48. return MockModel(id=entity.id, name=entity.name, version=entity._version)
  49. @classmethod
  50. def _model_to_entity(cls, model):
  51. return MockObj(id=model.id, name=model.name, version=model.version)
  52. class MockFSRepository(_FileSystemRepository):
  53. def __init__(self, **kwargs):
  54. super().__init__(**kwargs)
  55. @property
  56. def _storage_folder(self) -> pathlib.Path:
  57. return pathlib.Path(Config.core.storage_folder) # type: ignore