mocks.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 sqlalchemy import Column, String, Table
  16. from sqlalchemy.dialects import sqlite
  17. from sqlalchemy.orm import declarative_base, registry
  18. from sqlalchemy.schema import CreateTable
  19. from taipy.config.config import Config
  20. from taipy.core._repository._abstract_converter import _AbstractConverter
  21. from taipy.core._repository._filesystem_repository import _FileSystemRepository
  22. from taipy.core._repository._sql_repository import _SQLRepository
  23. from taipy.core._version._version_manager import _VersionManager
  24. class Base:
  25. __allow_unmapped__ = True
  26. Base = declarative_base(cls=Base) # type: ignore
  27. mapper_registry = registry()
  28. @dataclass
  29. class MockObj:
  30. def __init__(self, id: str, name: str, version: Optional[str] = None) -> None:
  31. self.id = id
  32. self.name = name
  33. if version:
  34. self._version = version
  35. else:
  36. self._version = _VersionManager._get_latest_version()
  37. @dataclass
  38. class MockModel(Base): # type: ignore
  39. __table__ = Table(
  40. "mock_model",
  41. mapper_registry.metadata,
  42. Column("id", String(200), primary_key=True),
  43. Column("name", String(200)),
  44. Column("version", String(200)),
  45. )
  46. id: str
  47. name: str
  48. version: str
  49. def to_dict(self):
  50. return dataclasses.asdict(self)
  51. @staticmethod
  52. def from_dict(data: Dict[str, Any]):
  53. return MockModel(id=data["id"], name=data["name"], version=data["version"])
  54. def _to_entity(self):
  55. return MockObj(id=self.id, name=self.name, version=self.version)
  56. @classmethod
  57. def _from_entity(cls, entity: MockObj):
  58. return MockModel(id=entity.id, name=entity.name, version=entity._version)
  59. def to_list(self):
  60. return [self.id, self.name, self.version]
  61. class MockConverter(_AbstractConverter):
  62. @classmethod
  63. def _entity_to_model(cls, entity):
  64. return MockModel(id=entity.id, name=entity.name, version=entity._version)
  65. @classmethod
  66. def _model_to_entity(cls, model):
  67. return MockObj(id=model.id, name=model.name, version=model.version)
  68. class MockFSRepository(_FileSystemRepository):
  69. def __init__(self, **kwargs):
  70. super().__init__(**kwargs)
  71. @property
  72. def _storage_folder(self) -> pathlib.Path:
  73. return pathlib.Path(Config.core.storage_folder) # type: ignore
  74. class MockSQLRepository(_SQLRepository):
  75. def __init__(self, **kwargs):
  76. super().__init__(**kwargs)
  77. self.db.execute(str(CreateTable(MockModel.__table__, if_not_exists=True).compile(dialect=sqlite.dialect())))