wexample-orm 0.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.1
2
+ Name: wexample-orm
3
+ Version: 0.0.6
4
+ Summary: Generic ORM abstractions on top of SQLAlchemy.
5
+ Author-Email: weeger <contact@wexample.com>
6
+ License: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Project-URL: homepage, https://github.com/wexample/python-orm
11
+ Requires-Python: >=3.10
12
+ Requires-Dist: attrs>=23.1.0
13
+ Requires-Dist: sqlalchemy<3,>=2
14
+ Requires-Dist: wexample-helpers>=13.0.0
15
+ Provides-Extra: dev
16
+ Requires-Dist: pytest; extra == "dev"
17
+ Requires-Dist: pytest-cov; extra == "dev"
18
+ Description-Content-Type: text/markdown
19
+
20
+ # orm
21
+
22
+ Version: 0.0.6
23
+
24
+ Generic ORM abstractions on top of SQLAlchemy.
25
+
26
+ ## Table of Contents
27
+
28
+ - [Tests](#tests)
29
+ - [Suite Integration](#suite-integration)
30
+ - [Dependencies](#dependencies)
31
+ - [Versioning](#versioning)
32
+ - [License](#license)
33
+ - [Suite Integration](#suite-integration)
34
+ - [Suite Signature](#suite-signature)
35
+ - [Introduction](#introduction)
36
+ - [Roadmap](#roadmap)
37
+ - [Status Compatibility](#status-compatibility)
38
+ - [Useful Links](#useful-links)
39
+ - [Migration Notes](#migration-notes)
40
+
41
+ ## Tests
42
+
43
+ This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
44
+
45
+ ### Installation
46
+
47
+ First, install the required testing dependencies:
48
+ ```bash
49
+ .venv/bin/python -m pip install pytest pytest-cov
50
+ ```
51
+
52
+ ### Basic Usage
53
+
54
+ Run all tests with coverage:
55
+ ```bash
56
+ .venv/bin/python -m pytest --cov --cov-report=html
57
+ ```
58
+
59
+ ### Common Commands
60
+ ```bash
61
+ # Run tests with coverage for a specific module
62
+ .venv/bin/python -m pytest --cov=your_module
63
+
64
+ # Show which lines are not covered
65
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
66
+
67
+ # Generate an HTML coverage report
68
+ .venv/bin/python -m pytest --cov=your_module --cov-report=html
69
+
70
+ # Combine terminal and HTML reports
71
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
72
+
73
+ # Run specific test file with coverage
74
+ .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
75
+ ```
76
+
77
+ ### Viewing HTML Reports
78
+
79
+ After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
80
+
81
+ ### Coverage Threshold
82
+
83
+ To enforce a minimum coverage percentage:
84
+ ```bash
85
+ .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
86
+ ```
87
+
88
+ This will cause the test suite to fail if coverage drops below 80%.
89
+
90
+ ## Integration in the Suite
91
+
92
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
93
+
94
+ ### Related Packages
95
+
96
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
97
+
98
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
99
+
100
+ ## Dependencies
101
+
102
+ - attrs: >=23.1.0
103
+ - sqlalchemy: <3,>=2
104
+ - wexample-helpers: >=13.0.0
105
+
106
+ ## Versioning & Compatibility Policy
107
+
108
+ Wexample packages follow **Semantic Versioning** (SemVer):
109
+
110
+ - **MAJOR**: Breaking changes
111
+ - **MINOR**: New features, backward compatible
112
+ - **PATCH**: Bug fixes, backward compatible
113
+
114
+ We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.
115
+
116
+ ## License
117
+
118
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
119
+
120
+ Free to use in both personal and commercial projects.
121
+
122
+ ## Integration in the Suite
123
+
124
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
125
+
126
+ ### Related Packages
127
+
128
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
129
+
130
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
131
+
132
+ # About us
133
+
134
+ [Wexample](https://wexample.com) stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.
135
+
136
+ This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.
137
+
138
+ Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.
139
+
140
+ Python Doctrine inspired ORM
141
+
142
+ ## Known Limitations & Roadmap
143
+
144
+ Current limitations and planned features are tracked in the GitHub issues.
145
+
146
+ See the [project roadmap](https://github.com/wexample/python-orm/issues) for upcoming features and improvements.
147
+
148
+ ## Status & Compatibility
149
+
150
+ **Maturity**: Production-ready
151
+
152
+ **Python Support**: >=3.10
153
+
154
+ **OS Support**: Linux, macOS, Windows
155
+
156
+ **Status**: Actively maintained
157
+
158
+ ## Useful Links
159
+
160
+ - **Homepage**: https://github.com/wexample/python-orm
161
+ - **Documentation**: [docs.wexample.com](https://docs.wexample.com)
162
+ - **Issue Tracker**: https://github.com/wexample/python-orm/issues
163
+ - **Discussions**: https://github.com/wexample/python-orm/discussions
164
+ - **PyPI**: [pypi.org/project/orm](https://pypi.org/project/orm/)
165
+
166
+ ## Migration Notes
167
+
168
+ When upgrading between major versions, refer to the migration guides in the documentation.
169
+
170
+ Breaking changes are clearly documented with upgrade paths and examples.
@@ -0,0 +1,151 @@
1
+ # orm
2
+
3
+ Version: 0.0.6
4
+
5
+ Generic ORM abstractions on top of SQLAlchemy.
6
+
7
+ ## Table of Contents
8
+
9
+ - [Tests](#tests)
10
+ - [Suite Integration](#suite-integration)
11
+ - [Dependencies](#dependencies)
12
+ - [Versioning](#versioning)
13
+ - [License](#license)
14
+ - [Suite Integration](#suite-integration)
15
+ - [Suite Signature](#suite-signature)
16
+ - [Introduction](#introduction)
17
+ - [Roadmap](#roadmap)
18
+ - [Status Compatibility](#status-compatibility)
19
+ - [Useful Links](#useful-links)
20
+ - [Migration Notes](#migration-notes)
21
+
22
+ ## Tests
23
+
24
+ This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
25
+
26
+ ### Installation
27
+
28
+ First, install the required testing dependencies:
29
+ ```bash
30
+ .venv/bin/python -m pip install pytest pytest-cov
31
+ ```
32
+
33
+ ### Basic Usage
34
+
35
+ Run all tests with coverage:
36
+ ```bash
37
+ .venv/bin/python -m pytest --cov --cov-report=html
38
+ ```
39
+
40
+ ### Common Commands
41
+ ```bash
42
+ # Run tests with coverage for a specific module
43
+ .venv/bin/python -m pytest --cov=your_module
44
+
45
+ # Show which lines are not covered
46
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
47
+
48
+ # Generate an HTML coverage report
49
+ .venv/bin/python -m pytest --cov=your_module --cov-report=html
50
+
51
+ # Combine terminal and HTML reports
52
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
53
+
54
+ # Run specific test file with coverage
55
+ .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
56
+ ```
57
+
58
+ ### Viewing HTML Reports
59
+
60
+ After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
61
+
62
+ ### Coverage Threshold
63
+
64
+ To enforce a minimum coverage percentage:
65
+ ```bash
66
+ .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
67
+ ```
68
+
69
+ This will cause the test suite to fail if coverage drops below 80%.
70
+
71
+ ## Integration in the Suite
72
+
73
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
74
+
75
+ ### Related Packages
76
+
77
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
78
+
79
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
80
+
81
+ ## Dependencies
82
+
83
+ - attrs: >=23.1.0
84
+ - sqlalchemy: <3,>=2
85
+ - wexample-helpers: >=13.0.0
86
+
87
+ ## Versioning & Compatibility Policy
88
+
89
+ Wexample packages follow **Semantic Versioning** (SemVer):
90
+
91
+ - **MAJOR**: Breaking changes
92
+ - **MINOR**: New features, backward compatible
93
+ - **PATCH**: Bug fixes, backward compatible
94
+
95
+ We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.
96
+
97
+ ## License
98
+
99
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
100
+
101
+ Free to use in both personal and commercial projects.
102
+
103
+ ## Integration in the Suite
104
+
105
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
106
+
107
+ ### Related Packages
108
+
109
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
110
+
111
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
112
+
113
+ # About us
114
+
115
+ [Wexample](https://wexample.com) stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.
116
+
117
+ This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.
118
+
119
+ Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.
120
+
121
+ Python Doctrine inspired ORM
122
+
123
+ ## Known Limitations & Roadmap
124
+
125
+ Current limitations and planned features are tracked in the GitHub issues.
126
+
127
+ See the [project roadmap](https://github.com/wexample/python-orm/issues) for upcoming features and improvements.
128
+
129
+ ## Status & Compatibility
130
+
131
+ **Maturity**: Production-ready
132
+
133
+ **Python Support**: >=3.10
134
+
135
+ **OS Support**: Linux, macOS, Windows
136
+
137
+ **Status**: Actively maintained
138
+
139
+ ## Useful Links
140
+
141
+ - **Homepage**: https://github.com/wexample/python-orm
142
+ - **Documentation**: [docs.wexample.com](https://docs.wexample.com)
143
+ - **Issue Tracker**: https://github.com/wexample/python-orm/issues
144
+ - **Discussions**: https://github.com/wexample/python-orm/discussions
145
+ - **PyPI**: [pypi.org/project/orm](https://pypi.org/project/orm/)
146
+
147
+ ## Migration Notes
148
+
149
+ When upgrading between major versions, refer to the migration guides in the documentation.
150
+
151
+ Breaking changes are clearly documented with upgrade paths and examples.
@@ -0,0 +1,86 @@
1
+ [build-system]
2
+ requires = [
3
+ "pdm-backend",
4
+ ]
5
+ build-backend = "pdm.backend"
6
+
7
+ [project]
8
+ name = "wexample-orm"
9
+ version = "0.0.6"
10
+ description = "Generic ORM abstractions on top of SQLAlchemy."
11
+ authors = [
12
+ { name = "weeger", email = "contact@wexample.com" },
13
+ ]
14
+ requires-python = ">=3.10"
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ ]
20
+ dependencies = [
21
+ "attrs>=23.1.0",
22
+ "sqlalchemy>=2,<3",
23
+ "wexample-helpers>=13.0.0",
24
+ ]
25
+
26
+ [project.readme]
27
+ file = "README.md"
28
+ content-type = "text/markdown"
29
+
30
+ [project.license]
31
+ text = "MIT"
32
+
33
+ [project.urls]
34
+ homepage = "https://github.com/wexample/python-orm"
35
+
36
+ [project.optional-dependencies]
37
+ dev = [
38
+ "pytest",
39
+ "pytest-cov",
40
+ ]
41
+
42
+ [tool.setuptools.packages.find]
43
+ include = [
44
+ "*",
45
+ ]
46
+ exclude = [
47
+ "wexample_orm.testing*",
48
+ ]
49
+
50
+ [tool.pdm]
51
+ distribution = true
52
+
53
+ [tool.pdm.build]
54
+ package-dir = "src"
55
+ packages = [
56
+ { include = "wexample_orm", from = "src" },
57
+ ]
58
+
59
+ [tool.pytest.ini_options]
60
+ testpaths = [
61
+ "tests",
62
+ ]
63
+ pythonpath = [
64
+ "src",
65
+ ]
66
+
67
+ [tool.coverage.run]
68
+ source = [
69
+ "wexample_orm",
70
+ ]
71
+ omit = [
72
+ "*/tests/*",
73
+ "*/.venv/*",
74
+ "*/venv/*",
75
+ ]
76
+
77
+ [tool.coverage.report]
78
+ exclude_lines = [
79
+ "pragma: no cover",
80
+ "def __repr__",
81
+ "raise AssertionError",
82
+ "raise NotImplementedError",
83
+ "if __name__ == .__main__.:",
84
+ "if TYPE_CHECKING:",
85
+ "@abstractmethod",
86
+ ]
@@ -0,0 +1,13 @@
1
+ from __future__ import annotations
2
+
3
+ from wexample_orm.common.abstract_repositories_manager import (
4
+ AbstractRepositoriesManager,
5
+ )
6
+ from wexample_orm.entity.abstract_entity import AbstractEntity
7
+ from wexample_orm.repository.abstract_repository import AbstractRepository
8
+
9
+ __all__ = [
10
+ "AbstractEntity",
11
+ "AbstractRepositoriesManager",
12
+ "AbstractRepository",
13
+ ]
File without changes
@@ -0,0 +1,54 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any
4
+
5
+ from wexample_helpers.classes.base_class import BaseClass
6
+ from wexample_helpers.classes.field import public_field
7
+ from wexample_helpers.classes.private_field import private_field
8
+ from wexample_helpers.decorator.base_class import base_class
9
+
10
+ if TYPE_CHECKING:
11
+ from wexample_orm.entity.abstract_entity import AbstractEntity
12
+ from wexample_orm.repository.abstract_repository import AbstractRepository
13
+
14
+
15
+ @base_class
16
+ class AbstractRepositoriesManager(BaseClass):
17
+ repository_classes: list[type[AbstractRepository]] = public_field(
18
+ description="Repository classes this manager can instantiate, keyed by their entity type.",
19
+ factory=list,
20
+ )
21
+ session: Any | None = public_field(
22
+ description="SQLAlchemy session shared by all repositories built by this manager.",
23
+ default=None,
24
+ )
25
+ _instances: dict[type[AbstractEntity], AbstractRepository] = private_field(
26
+ description="Cache of repository instances keyed by entity type.",
27
+ factory=dict,
28
+ )
29
+
30
+ def get(self, entity_type: type[AbstractEntity]) -> AbstractRepository:
31
+ if entity_type in self._instances:
32
+ return self._instances[entity_type]
33
+
34
+ for repository_class in self.repository_classes:
35
+ if repository_class.get_entity_type() is entity_type:
36
+ repository = repository_class(session=self.session)
37
+ self._instances[entity_type] = repository
38
+ return repository
39
+
40
+ from wexample_orm.exception.unknown_repository_exception import (
41
+ UnknownRepositoryException,
42
+ )
43
+
44
+ raise UnknownRepositoryException.for_entity(entity_type)
45
+
46
+ def get_by_table_name(self, table_name: str) -> AbstractRepository | None:
47
+ for repository_class in self.repository_classes:
48
+ entity_type = repository_class.get_entity_type()
49
+ if entity_type.get_entity_name() == table_name:
50
+ return self.get(entity_type)
51
+ return None
52
+
53
+ def get_classes(self) -> list[type[AbstractRepository]]:
54
+ return list(self.repository_classes)
File without changes
@@ -0,0 +1,31 @@
1
+ from __future__ import annotations
2
+
3
+ from sqlalchemy import Integer, Sequence
4
+ from sqlalchemy.orm import Mapped, as_declarative, declared_attr, mapped_column
5
+ from wexample_helpers.helpers.string import string_to_snake_case
6
+
7
+
8
+ @as_declarative()
9
+ class AbstractEntity:
10
+ __abstract__ = True
11
+
12
+ @declared_attr
13
+ def __tablename__(cls) -> str:
14
+ return cls.get_entity_name()
15
+
16
+ @classmethod
17
+ def get_entity_name(cls) -> str:
18
+ return string_to_snake_case(cls.__name__)
19
+
20
+ @classmethod
21
+ def get_sequence(cls) -> Sequence:
22
+ return Sequence(f"{cls.get_entity_name()}_id_seq")
23
+
24
+ @declared_attr
25
+ def id(cls) -> Mapped[int]:
26
+ return mapped_column(
27
+ Integer,
28
+ cls.get_sequence(),
29
+ primary_key=True,
30
+ autoincrement=True,
31
+ )
@@ -0,0 +1,16 @@
1
+ from __future__ import annotations
2
+
3
+ from wexample_helpers.exception.undefined_exception import UndefinedException
4
+
5
+
6
+ class RepositorySessionMissingException(UndefinedException):
7
+ error_code = "ORM_REPOSITORY_SESSION_MISSING"
8
+
9
+ def __init__(self, repository_class_name: str) -> None:
10
+ super().__init__(
11
+ message=(
12
+ f"Repository '{repository_class_name}' has no session configured. "
13
+ "Pass `session=` when instantiating the manager or repository."
14
+ ),
15
+ data={"repository_class": repository_class_name},
16
+ )
@@ -0,0 +1,21 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from wexample_helpers.exception.undefined_exception import UndefinedException
6
+
7
+ if TYPE_CHECKING:
8
+ from wexample_orm.entity.abstract_entity import AbstractEntity
9
+
10
+
11
+ class UnknownRepositoryException(UndefinedException):
12
+ error_code = "ORM_UNKNOWN_REPOSITORY"
13
+
14
+ @classmethod
15
+ def for_entity(
16
+ cls, entity_type: type[AbstractEntity]
17
+ ) -> UnknownRepositoryException:
18
+ return cls(
19
+ message=f"No repository registered for entity type: {entity_type.__name__}",
20
+ data={"entity_type": entity_type.__name__},
21
+ )
File without changes
@@ -0,0 +1,63 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any
4
+
5
+ from wexample_helpers.classes.base_class import BaseClass
6
+ from wexample_helpers.classes.field import public_field
7
+ from wexample_helpers.decorator.base_class import base_class
8
+
9
+ if TYPE_CHECKING:
10
+ from sqlalchemy.orm import Query, Session
11
+
12
+ from wexample_orm.entity.abstract_entity import AbstractEntity
13
+
14
+
15
+ @base_class
16
+ class AbstractRepository(BaseClass):
17
+ session: Any | None = public_field(
18
+ description="SQLAlchemy session used by this repository.",
19
+ default=None,
20
+ )
21
+
22
+ @classmethod
23
+ def get_entity_type(cls) -> type[AbstractEntity]:
24
+ cls._raise_not_implemented_error()
25
+
26
+ def find(self, entity_id: int) -> AbstractEntity | None:
27
+ if self.session is None:
28
+ return None
29
+ return self.session.get(self.get_entity_type(), entity_id)
30
+
31
+ def find_one_by(self, filter_clause: Any) -> AbstractEntity | None:
32
+ return self.query().filter(filter_clause).first()
33
+
34
+ def query(self) -> Query:
35
+ self._require_session()
36
+ return self.session.query(self.get_entity_type())
37
+
38
+ def save(
39
+ self,
40
+ entity: AbstractEntity,
41
+ *,
42
+ flush: bool = True,
43
+ refresh: bool = False,
44
+ ) -> AbstractEntity:
45
+ self._require_session()
46
+
47
+ self.session.add(entity)
48
+ if flush:
49
+ self.session.commit()
50
+ if refresh:
51
+ self.session.refresh(entity)
52
+ return entity
53
+
54
+ def _require_session(self) -> Session:
55
+ if self.session is None:
56
+ from wexample_orm.exception.repository_session_missing_exception import (
57
+ RepositorySessionMissingException,
58
+ )
59
+
60
+ raise RepositorySessionMissingException(
61
+ repository_class_name=self.__class__.__name__
62
+ )
63
+ return self.session
@@ -0,0 +1,70 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any
4
+
5
+ from wexample_helpers.classes.base_class import BaseClass
6
+ from wexample_helpers.classes.field import public_field
7
+ from wexample_helpers.classes.private_field import private_field
8
+ from wexample_helpers.decorator.base_class import base_class
9
+
10
+ if TYPE_CHECKING:
11
+ from sqlalchemy.engine import Engine
12
+ from sqlalchemy.orm import Session
13
+
14
+
15
+ @base_class
16
+ class AbstractSessionFactory(BaseClass):
17
+ """Build and cache a SQLAlchemy engine + sessionmaker from a DSN.
18
+
19
+ Subclasses can override `_engine_kwargs` / `_session_kwargs` to inject
20
+ project-specific options (echo, pool_pre_ping, expire_on_commit, ...).
21
+ """
22
+
23
+ dsn: str = public_field(
24
+ description="SQLAlchemy URL (e.g. 'postgresql+psycopg://user:pwd@host/db', 'sqlite:///:memory:').",
25
+ )
26
+ engine_options: dict[str, Any] = public_field(
27
+ description="Extra kwargs forwarded to sqlalchemy.create_engine().",
28
+ factory=dict,
29
+ )
30
+ session_options: dict[str, Any] = public_field(
31
+ description="Extra kwargs forwarded to sqlalchemy.orm.sessionmaker().",
32
+ factory=dict,
33
+ )
34
+ _engine: Engine | None = private_field(
35
+ description="Cached SQLAlchemy engine.",
36
+ default=None,
37
+ )
38
+ _sessionmaker: Any | None = private_field(
39
+ description="Cached sessionmaker bound to the engine.",
40
+ default=None,
41
+ )
42
+
43
+ def create_session(self) -> Session:
44
+ if self._sessionmaker is None:
45
+ from sqlalchemy.orm import sessionmaker
46
+
47
+ self._sessionmaker = sessionmaker(
48
+ bind=self.get_engine(), **self._session_kwargs()
49
+ )
50
+ return self._sessionmaker()
51
+
52
+ def dispose(self) -> None:
53
+ """Release the engine's connection pool. Useful in tests and teardown."""
54
+ if self._engine is not None:
55
+ self._engine.dispose()
56
+ self._engine = None
57
+ self._sessionmaker = None
58
+
59
+ def get_engine(self) -> Engine:
60
+ if self._engine is None:
61
+ from sqlalchemy import create_engine
62
+
63
+ self._engine = create_engine(self.dsn, **self._engine_kwargs())
64
+ return self._engine
65
+
66
+ def _engine_kwargs(self) -> dict[str, Any]:
67
+ return dict(self.engine_options)
68
+
69
+ def _session_kwargs(self) -> dict[str, Any]:
70
+ return dict(self.session_options)
@@ -0,0 +1,11 @@
1
+ from __future__ import annotations
2
+
3
+ from wexample_orm.testing.sqlite import (
4
+ in_memory_engine,
5
+ in_memory_session,
6
+ )
7
+
8
+ __all__ = [
9
+ "in_memory_engine",
10
+ "in_memory_session",
11
+ ]
@@ -0,0 +1,59 @@
1
+ """Testing helpers — spin up an in-memory SQLite session in 1 call.
2
+
3
+ Used by downstream packages that want to test repositories/managers without
4
+ standing up a real Postgres. Mirrors the spirit of `wexample_cli.testing.kernel`.
5
+
6
+ Typical use::
7
+
8
+ from wexample_orm.testing import in_memory_session
9
+ from wexample_orm.entity.abstract_entity import AbstractEntity
10
+
11
+ def test_user_save():
12
+ with in_memory_session(AbstractEntity) as session:
13
+ repo = UserRepository(session=session)
14
+ repo.save(User(name="alice"))
15
+ assert repo.find(1).name == "alice"
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ from collections.abc import Iterator
21
+ from contextlib import contextmanager
22
+ from typing import TYPE_CHECKING
23
+
24
+ if TYPE_CHECKING:
25
+ from sqlalchemy.engine import Engine
26
+ from sqlalchemy.orm import Session
27
+
28
+ from wexample_orm.entity.abstract_entity import AbstractEntity
29
+
30
+
31
+ def in_memory_engine() -> Engine:
32
+ """Create a fresh in-memory SQLite engine."""
33
+ from sqlalchemy import create_engine
34
+
35
+ return create_engine("sqlite:///:memory:")
36
+
37
+
38
+ @contextmanager
39
+ def in_memory_session(
40
+ declarative_base: type[AbstractEntity],
41
+ ) -> Iterator[Session]:
42
+ """Yield a session bound to a fresh in-memory SQLite engine.
43
+
44
+ `declarative_base` must be a class decorated with `@as_declarative()`
45
+ (typically `AbstractEntity` or a project subclass). All tables declared
46
+ against its metadata are created automatically.
47
+ """
48
+ from sqlalchemy.orm import sessionmaker
49
+
50
+ engine = in_memory_engine()
51
+ declarative_base.metadata.create_all(engine)
52
+
53
+ session_cls = sessionmaker(bind=engine)
54
+ session = session_cls()
55
+ try:
56
+ yield session
57
+ finally:
58
+ session.close()
59
+ engine.dispose()
File without changes
@@ -0,0 +1,93 @@
1
+ from __future__ import annotations
2
+
3
+ import pytest
4
+ from sqlalchemy.orm import Mapped, mapped_column
5
+
6
+ from wexample_orm.common.abstract_repositories_manager import (
7
+ AbstractRepositoriesManager,
8
+ )
9
+ from wexample_orm.entity.abstract_entity import AbstractEntity
10
+ from wexample_orm.exception.repository_session_missing_exception import (
11
+ RepositorySessionMissingException,
12
+ )
13
+ from wexample_orm.exception.unknown_repository_exception import (
14
+ UnknownRepositoryException,
15
+ )
16
+ from wexample_orm.repository.abstract_repository import AbstractRepository
17
+ from wexample_orm.testing import in_memory_session
18
+
19
+
20
+ def test_entity_name_is_snake_case_of_class_name() -> None:
21
+ assert User.get_entity_name() == "user"
22
+ assert User.__tablename__ == "user"
23
+
24
+
25
+ def test_manager_caches_repository_instances() -> None:
26
+ with in_memory_session(AbstractEntity) as session:
27
+ manager = ProjectRepositoriesManager(
28
+ session=session,
29
+ repository_classes=[UserRepository],
30
+ )
31
+ repo_a = manager.get(User)
32
+ repo_b = manager.get(User)
33
+ assert repo_a is repo_b
34
+
35
+
36
+ def test_manager_get_by_table_name() -> None:
37
+ with in_memory_session(AbstractEntity) as session:
38
+ manager = ProjectRepositoriesManager(
39
+ session=session,
40
+ repository_classes=[UserRepository],
41
+ )
42
+ repo = manager.get_by_table_name("user")
43
+ assert repo is not None
44
+ assert repo.get_entity_type() is User
45
+ assert manager.get_by_table_name("nonexistent") is None
46
+
47
+
48
+ def test_manager_raises_for_unknown_entity_type() -> None:
49
+ manager = ProjectRepositoriesManager(repository_classes=[UserRepository])
50
+ with pytest.raises(UnknownRepositoryException) as exc_info:
51
+ manager.get(Untracked)
52
+ assert exc_info.value.error_code == "ORM_UNKNOWN_REPOSITORY"
53
+ assert exc_info.value.data["entity_type"] == "Untracked"
54
+
55
+
56
+ def test_repository_without_session_find_returns_none() -> None:
57
+ repo = UserRepository()
58
+ assert repo.find(42) is None
59
+
60
+
61
+ def test_repository_without_session_raises_on_query() -> None:
62
+ repo = UserRepository()
63
+ with pytest.raises(RepositorySessionMissingException):
64
+ repo.query()
65
+
66
+
67
+ def test_save_and_find_roundtrip() -> None:
68
+ with in_memory_session(AbstractEntity) as session:
69
+ repo = UserRepository(session=session)
70
+ repo.save(User(name="alice"))
71
+ repo.save(User(name="bob"))
72
+
73
+ first = repo.find(1)
74
+ assert first is not None
75
+ assert first.name == "alice"
76
+
77
+
78
+ class User(AbstractEntity):
79
+ name: Mapped[str] = mapped_column()
80
+
81
+
82
+ class Untracked(AbstractEntity):
83
+ label: Mapped[str] = mapped_column()
84
+
85
+
86
+ class UserRepository(AbstractRepository):
87
+ @classmethod
88
+ def get_entity_type(cls) -> type[AbstractEntity]:
89
+ return User
90
+
91
+
92
+ class ProjectRepositoriesManager(AbstractRepositoriesManager):
93
+ pass