codecraftai-testbase 1.3.0__py3-none-any.whl
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.
- ccai/factories/py.typed +0 -0
- ccai/factories/testbasedefaulttypefactory.py +25 -0
- ccai/testbase/__init__.py +63 -0
- ccai/testbase/breakpoints.py +110 -0
- ccai/testbase/cli/__init__.py +13 -0
- ccai/testbase/cli/cmdtree/agents/__init__.py +28 -0
- ccai/testbase/cli/cmdtree/agents/config/__init__.py +24 -0
- ccai/testbase/cli/cmdtree/agents/config/extract.py +94 -0
- ccai/testbase/cli/cmdtree/agents/formatting.py +71 -0
- ccai/testbase/cli/cmdtree/agents/jsosparser.py +119 -0
- ccai/testbase/cli/cmdtree/agents/pathresolver.py +34 -0
- ccai/testbase/cli/cmdtree/agents/summary/__init__.py +26 -0
- ccai/testbase/cli/cmdtree/agents/summary/compare.py +128 -0
- ccai/testbase/cli/cmdtree/agents/summary/extract.py +91 -0
- ccai/testbase/cli/cmdtree/agents/testresult/__init__.py +28 -0
- ccai/testbase/cli/cmdtree/agents/testresult/extract.py +77 -0
- ccai/testbase/cli/cmdtree/agents/testresult/failures.py +73 -0
- ccai/testbase/cli/cmdtree/agents/testresult/list.py +63 -0
- ccai/testbase/cli/cmdtree/services/__init__.py +27 -0
- ccai/testbase/cli/cmdtree/services/results.py +66 -0
- ccai/testbase/cli/cmdtree/testing/__init__.py +30 -0
- ccai/testbase/cli/cmdtree/testing/jobs.py +175 -0
- ccai/testbase/cli/cmdtree/testing/query.py +131 -0
- ccai/testbase/cli/cmdtree/testing/run.py +263 -0
- ccai/testbase/cli/cmdtree/utilities/__init__.py +28 -0
- ccai/testbase/cli/cmdtree/utilities/timestamp.py +33 -0
- ccai/testbase/cli/options.py +370 -0
- ccai/testbase/cli/testbase_command.py +64 -0
- ccai/testbase/cli/utilities.py +31 -0
- ccai/testbase/configuration.py +324 -0
- ccai/testbase/constraints.py +998 -0
- ccai/testbase/coupling/__init__.py +569 -0
- ccai/testbase/coupling/basecoupling.py +69 -0
- ccai/testbase/coupling/configurationcoupling.py +138 -0
- ccai/testbase/coupling/integrationcoupling.py +842 -0
- ccai/testbase/coupling/validatorcoupling.py +127 -0
- ccai/testbase/debuggingext.py +281 -0
- ccai/testbase/debuglogging.py +407 -0
- ccai/testbase/debugports.py +469 -0
- ccai/testbase/declaration/__init__.py +13 -0
- ccai/testbase/declaration/attachments.py +222 -0
- ccai/testbase/declaration/originations.py +217 -0
- ccai/testbase/declaration/parameterization.py +160 -0
- ccai/testbase/declaration/registrations.py +476 -0
- ccai/testbase/diagnostic_level.py +197 -0
- ccai/testbase/diagnostic_streaming.py +1534 -0
- ccai/testbase/diagnostics.py +1685 -0
- ccai/testbase/entrypoints.py +485 -0
- ccai/testbase/environment.py +162 -0
- ccai/testbase/errors/testruntimeouterror.py +50 -0
- ccai/testbase/expressions.py +355 -0
- ccai/testbase/factories.py +277 -0
- ccai/testbase/factoryprotocols.py +35 -0
- ccai/testbase/features.py +913 -0
- ccai/testbase/initialize.py +210 -0
- ccai/testbase/injection/__init__.py +25 -0
- ccai/testbase/injection/injectiongroup.py +493 -0
- ccai/testbase/injection/injectionmoment.py +155 -0
- ccai/testbase/injection/injectionregistry.py +823 -0
- ccai/testbase/injection/injectionscope.py +2589 -0
- ccai/testbase/injection/introspection.py +514 -0
- ccai/testbase/markers.py +665 -0
- ccai/testbase/mocking.py +4856 -0
- ccai/testbase/origins/__init__.py +13 -0
- ccai/testbase/origins/resourceorigin.py +110 -0
- ccai/testbase/origins/validatororigin.py +111 -0
- ccai/testbase/output/__init__.py +17 -0
- ccai/testbase/output/outputcontext.py +201 -0
- ccai/testbase/output/runmetadata.py +184 -0
- ccai/testbase/properties/__init__.py +40 -0
- ccai/testbase/properties/assumptions.py +56 -0
- ccai/testbase/properties/decorators.py +173 -0
- ccai/testbase/properties/propertyrunner.py +449 -0
- ccai/testbase/properties/strategies.py +1592 -0
- ccai/testbase/properties/unittestsupport.py +87 -0
- ccai/testbase/py.typed +0 -0
- ccai/testbase/queries.py +95 -0
- ccai/testbase/resourcelifespan.py +24 -0
- ccai/testbase/results/__init__.py +35 -0
- ccai/testbase/results/exceptions.py +60 -0
- ccai/testbase/results/filtering/__init__.py +26 -0
- ccai/testbase/results/filtering/resultfilter.py +570 -0
- ccai/testbase/results/model/__init__.py +32 -0
- ccai/testbase/results/model/buildinfo.py +54 -0
- ccai/testbase/results/model/checkresult.py +183 -0
- ccai/testbase/results/model/forwardinginfo.py +30 -0
- ccai/testbase/results/model/jobcontainer.py +32 -0
- ccai/testbase/results/model/jobinfo.py +91 -0
- ccai/testbase/results/model/nestedprogressinfo.py +211 -0
- ccai/testbase/results/model/pipelineinfo.py +51 -0
- ccai/testbase/results/model/progressaggregator.py +551 -0
- ccai/testbase/results/model/progresscode.py +29 -0
- ccai/testbase/results/model/progressdelivery.py +51 -0
- ccai/testbase/results/model/progressinfo.py +84 -0
- ccai/testbase/results/model/progresstype.py +24 -0
- ccai/testbase/results/model/renderinfo.py +30 -0
- ccai/testbase/results/model/resultcode.py +28 -0
- ccai/testbase/results/model/resultcontainer.py +95 -0
- ccai/testbase/results/model/resultnode.py +258 -0
- ccai/testbase/results/model/resulttype.py +34 -0
- ccai/testbase/results/model/streaminginfo.py +111 -0
- ccai/testbase/results/model/taskinggroup.py +85 -0
- ccai/testbase/results/model/taskingresult.py +297 -0
- ccai/testbase/results/model/testcontainer.py +35 -0
- ccai/testbase/results/model/testresult.py +170 -0
- ccai/testbase/results/recorders/__init__.py +31 -0
- ccai/testbase/results/recorders/csvresultrecorder.py +414 -0
- ccai/testbase/results/recorders/jsonresultrecorder.py +467 -0
- ccai/testbase/results/recorders/resultrecorder.py +1472 -0
- ccai/testbase/results/recorders/stdoutresultrecorder.py +70 -0
- ccai/testbase/results/recorders/xmlresultrecorder.py +305 -0
- ccai/testbase/results/utilities.py +141 -0
- ccai/testbase/sequencing/__init__.py +13 -0
- ccai/testbase/sequencing/sequencermodulescope.py +96 -0
- ccai/testbase/sequencing/sequencerscopebase.py +91 -0
- ccai/testbase/sequencing/sequencersessionscope.py +84 -0
- ccai/testbase/sequencing/sequencertestscope.py +249 -0
- ccai/testbase/sequencing/sequencertestsetupscope.py +86 -0
- ccai/testbase/sequencing/testactivity.py +111 -0
- ccai/testbase/sequencing/testsequencer.py +3072 -0
- ccai/testbase/server/__init__.py +13 -0
- ccai/testbase/server/resultserver.py +599 -0
- ccai/testbase/sources/__init__.py +13 -0
- ccai/testbase/sources/configurationsource.py +32 -0
- ccai/testbase/sources/integrationsource.py +29 -0
- ccai/testbase/sources/resourcesource.py +27 -0
- ccai/testbase/sources/sourcebase.py +71 -0
- ccai/testbase/sources/unknownsource.py +30 -0
- ccai/testbase/sources/validatorsource.py +28 -0
- ccai/testbase/tandem/__init__.py +13 -0
- ccai/testbase/tandem/validators.py +286 -0
- ccai/testbase/testcollector.py +230 -0
- ccai/testbase/testgroup.py +54 -0
- ccai/testbase/testjob.py +600 -0
- ccai/testbase/testref.py +64 -0
- ccai/testbase/utilities.py +472 -0
- ccai/testbase/verification.py +768 -0
- codecraftai_testbase-1.3.0.dist-info/METADATA +99 -0
- codecraftai_testbase-1.3.0.dist-info/RECORD +142 -0
- codecraftai_testbase-1.3.0.dist-info/WHEEL +4 -0
- codecraftai_testbase-1.3.0.dist-info/entry_points.txt +3 -0
- codecraftai_testbase-1.3.0.dist-info/licenses/LICENSE.md +204 -0
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: ccai.testbase.injection.injectiongroup
|
|
3
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
4
|
+
:synopsis: Module for injectiongroup.
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Myron Walker <myron.walker@gmail.com>
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
__author__ = "Myron Walker"
|
|
12
|
+
__copyright__ = "Copyright 2025, Code Craft AI LLC"
|
|
13
|
+
__credits__ = []
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
from typing import Dict, List, Optional, Union
|
|
18
|
+
|
|
19
|
+
import sys
|
|
20
|
+
|
|
21
|
+
from ccai.errors.exceptions import SemanticError
|
|
22
|
+
|
|
23
|
+
from ccai.xmodules.xlogging.enhancedloggers import getEnhancedLogger
|
|
24
|
+
|
|
25
|
+
from ccai.testbase.injection.injectionregistry import injection_registry
|
|
26
|
+
from ccai.testbase.injection.injectionscope import InjectionScope
|
|
27
|
+
from ccai.testbase.injection.injectionmoment import InjectionMoment
|
|
28
|
+
|
|
29
|
+
logger = getEnhancedLogger()
|
|
30
|
+
|
|
31
|
+
class InjectionGroup:
|
|
32
|
+
"""
|
|
33
|
+
Represents a hierarchical grouping of injection moments organized by module paths.
|
|
34
|
+
|
|
35
|
+
The InjectionGroup class forms a tree structure that mirrors the Python module hierarchy,
|
|
36
|
+
enabling metadata resolution and propagation through the injection system. Each group
|
|
37
|
+
can contain child groups (representing subpackages or modules) and injection moments
|
|
38
|
+
(representing test functions).
|
|
39
|
+
|
|
40
|
+
Hierarchy Construction
|
|
41
|
+
----------------------
|
|
42
|
+
The group hierarchy is built dynamically as injection moments are registered. When an
|
|
43
|
+
injection moment is added via :meth:`add_descendent`, the system:
|
|
44
|
+
|
|
45
|
+
1. Parses the module path from the injection moment's scope name
|
|
46
|
+
2. Walks or creates intermediate group nodes along the path
|
|
47
|
+
3. Inserts the injection moment at the appropriate leaf position
|
|
48
|
+
|
|
49
|
+
Example hierarchy structure::
|
|
50
|
+
|
|
51
|
+
-------------
|
|
52
|
+
| Group A |
|
|
53
|
+
---------------------------
|
|
54
|
+
| Group AA | Group AB |
|
|
55
|
+
-------------------------------
|
|
56
|
+
| Group AAA/ABA |
|
|
57
|
+
-------------------------------
|
|
58
|
+
|
|
59
|
+
For a test function at ``tests.unit.auth.test_login#test_valid_credentials``:
|
|
60
|
+
|
|
61
|
+
- Root group (package=None) contains child "tests"
|
|
62
|
+
- "tests" group contains child "unit"
|
|
63
|
+
- "unit" group contains child "auth"
|
|
64
|
+
- "auth" group contains child "test_login"
|
|
65
|
+
- "test_login" group contains the InjectionMoment for "test_valid_credentials"
|
|
66
|
+
|
|
67
|
+
Metadata Propagation
|
|
68
|
+
--------------------
|
|
69
|
+
Metadata flows from parent groups to child groups and injection moments during the
|
|
70
|
+
:meth:`resolve_metadata` phase. The propagation follows these rules:
|
|
71
|
+
|
|
72
|
+
1. Each group checks its associated module for a ``_metadata_`` attribute
|
|
73
|
+
2. Parent metadata is merged with the group's own metadata
|
|
74
|
+
3. Child metadata values override parent values for the same keys
|
|
75
|
+
4. The resolved metadata is propagated to all children recursively
|
|
76
|
+
|
|
77
|
+
This enables test modules to define metadata that applies to all tests within,
|
|
78
|
+
while allowing individual tests or submodules to override specific values.
|
|
79
|
+
|
|
80
|
+
Usage Example::
|
|
81
|
+
|
|
82
|
+
# Create root group
|
|
83
|
+
root = InjectionGroup("root")
|
|
84
|
+
|
|
85
|
+
# Add injection moment (creates intermediate groups automatically)
|
|
86
|
+
moment = InjectionMoment(test_function)
|
|
87
|
+
root.add_descendent(moment)
|
|
88
|
+
|
|
89
|
+
# Resolve metadata from root down
|
|
90
|
+
root.resolve_metadata()
|
|
91
|
+
|
|
92
|
+
Context Manager Support
|
|
93
|
+
-----------------------
|
|
94
|
+
InjectionGroup supports the context manager protocol for scoped operations::
|
|
95
|
+
|
|
96
|
+
with InjectionGroup("mygroup") as group:
|
|
97
|
+
# Perform operations within group context
|
|
98
|
+
pass
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
def __init__(self, name, package=None):
|
|
102
|
+
"""
|
|
103
|
+
Initializes an :class:`InjectionGroup` instance.
|
|
104
|
+
|
|
105
|
+
Creates a new group node that can serve as part of the injection hierarchy.
|
|
106
|
+
Groups with ``package=None`` are root groups and can use the :meth:`add_descendent`
|
|
107
|
+
API. Groups with a package value are intermediate nodes created during hierarchy
|
|
108
|
+
construction.
|
|
109
|
+
|
|
110
|
+
:param name: Name of the group, typically corresponding to a module or package name.
|
|
111
|
+
:param package: Optional dotted package path where the group resides. When ``None``,
|
|
112
|
+
this group is the root of the hierarchy and can accept descendent additions.
|
|
113
|
+
When set, this group is an intermediate node in the hierarchy.
|
|
114
|
+
|
|
115
|
+
.. note::
|
|
116
|
+
The combination of ``package`` and ``name`` forms the group's full scope name,
|
|
117
|
+
which is used for resource scope lookup via :meth:`get_resource_scope`.
|
|
118
|
+
"""
|
|
119
|
+
self._name = name
|
|
120
|
+
self._package = package
|
|
121
|
+
self._children = {}
|
|
122
|
+
self._finalized = False
|
|
123
|
+
self._metadata = None
|
|
124
|
+
return
|
|
125
|
+
|
|
126
|
+
def __enter__(self):
|
|
127
|
+
"""
|
|
128
|
+
Enter the context manager.
|
|
129
|
+
|
|
130
|
+
:returns: The current :class:`InjectionGroup` instance.
|
|
131
|
+
"""
|
|
132
|
+
return self
|
|
133
|
+
|
|
134
|
+
def __exit__(self, ex_type, ex_inst, ex_tb):
|
|
135
|
+
"""
|
|
136
|
+
Exit the context manager.
|
|
137
|
+
|
|
138
|
+
:param ex_type: Exception type if raised.
|
|
139
|
+
:param ex_inst: Exception instance if raised.
|
|
140
|
+
:param ex_tb: Traceback object if raised.
|
|
141
|
+
|
|
142
|
+
:returns: ``False`` to propagate exceptions.
|
|
143
|
+
"""
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def children(self):
|
|
148
|
+
"""
|
|
149
|
+
Mapping of child injection groups and moments.
|
|
150
|
+
|
|
151
|
+
Returns a dictionary where keys are child names (module names or test base names)
|
|
152
|
+
and values are either :class:`InjectionGroup` instances (for intermediate nodes)
|
|
153
|
+
or :class:`InjectionMoment` instances (for leaf nodes representing test functions).
|
|
154
|
+
|
|
155
|
+
The children dictionary is populated during hierarchy construction via
|
|
156
|
+
:meth:`add_descendent` and its internal helper :meth:`_add_descendent`.
|
|
157
|
+
|
|
158
|
+
:returns: Dictionary mapping child names to :class:`InjectionGroup` or
|
|
159
|
+
:class:`InjectionMoment` objects.
|
|
160
|
+
"""
|
|
161
|
+
return self._children
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def finalized(self):
|
|
165
|
+
"""
|
|
166
|
+
Indicates whether the group has been finalized.
|
|
167
|
+
|
|
168
|
+
:returns: ``True`` if finalized.
|
|
169
|
+
"""
|
|
170
|
+
return self._finalized
|
|
171
|
+
|
|
172
|
+
@property
|
|
173
|
+
def name(self):
|
|
174
|
+
"""
|
|
175
|
+
The name of the injection group.
|
|
176
|
+
|
|
177
|
+
:returns: Group name string.
|
|
178
|
+
"""
|
|
179
|
+
return self._name
|
|
180
|
+
|
|
181
|
+
@property
|
|
182
|
+
def package(self):
|
|
183
|
+
"""
|
|
184
|
+
Package path for the injection group.
|
|
185
|
+
|
|
186
|
+
:returns: Package name or ``None``.
|
|
187
|
+
"""
|
|
188
|
+
return self._package
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def scope_name(self):
|
|
192
|
+
"""
|
|
193
|
+
Fully qualified scope name for the group.
|
|
194
|
+
|
|
195
|
+
Computes the dotted scope name by combining the package path and group name.
|
|
196
|
+
This scope name is used to look up the associated :class:`InjectionScope` in
|
|
197
|
+
the injection registry via :meth:`get_resource_scope`.
|
|
198
|
+
|
|
199
|
+
For a group with ``name="test_auth"`` and ``package="tests.unit"``, the
|
|
200
|
+
scope name would be ``"tests.unit.test_auth"``.
|
|
201
|
+
|
|
202
|
+
For a root group with ``name="root"`` and ``package=None``, the scope name
|
|
203
|
+
would simply be ``"root"``.
|
|
204
|
+
|
|
205
|
+
:returns: The dotted scope name string used for resource scope lookup.
|
|
206
|
+
"""
|
|
207
|
+
sname = self._name
|
|
208
|
+
if self._package is not None and len(self._package) > 0:
|
|
209
|
+
sname = "{}.{}".format(self.package, sname)
|
|
210
|
+
return sname
|
|
211
|
+
|
|
212
|
+
def add_descendent(self, child_ref:InjectionMoment, group_type: Optional[type] = None):
|
|
213
|
+
"""
|
|
214
|
+
Adds a descendent injection moment under this group, creating intermediate nodes as needed.
|
|
215
|
+
|
|
216
|
+
This is the primary API for building the injection group hierarchy. It parses the
|
|
217
|
+
injection moment's scope name to determine the module path, then walks or creates
|
|
218
|
+
intermediate group nodes along that path before inserting the moment at the leaf.
|
|
219
|
+
|
|
220
|
+
Hierarchy Construction Process
|
|
221
|
+
-------------------------------
|
|
222
|
+
1. Extract the module path from the injection moment's name (before the ``#`` separator)
|
|
223
|
+
2. Split the module path into components (e.g., ``["tests", "unit", "auth"]``)
|
|
224
|
+
3. Recursively walk the tree, creating intermediate :class:`InjectionGroup` nodes
|
|
225
|
+
for any missing path components
|
|
226
|
+
4. Insert the injection moment at the final position using its base name
|
|
227
|
+
|
|
228
|
+
Example::
|
|
229
|
+
|
|
230
|
+
# For an injection moment with name "tests.unit.auth#test_login"
|
|
231
|
+
root.add_descendent(moment)
|
|
232
|
+
|
|
233
|
+
# This creates the hierarchy:
|
|
234
|
+
# root -> tests -> unit -> auth -> test_login (InjectionMoment)
|
|
235
|
+
|
|
236
|
+
:param child_ref: The :class:`InjectionMoment` to add to the hierarchy.
|
|
237
|
+
:param group_type: Optional class to use when creating intermediate group nodes.
|
|
238
|
+
If ``None``, uses the same class as ``self`` (typically :class:`InjectionGroup`).
|
|
239
|
+
This allows subclasses to maintain their type throughout the hierarchy.
|
|
240
|
+
|
|
241
|
+
:raises SemanticError: If called on a non-root group (i.e., a group with a
|
|
242
|
+
non-None package). Only the root group should use this API; intermediate
|
|
243
|
+
nodes are populated via the internal :meth:`_add_descendent` method.
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
if self._package is not None:
|
|
247
|
+
err_msg = "The 'add_descendent' API can only be called on the root package."
|
|
248
|
+
raise SemanticError(err_msg) from None
|
|
249
|
+
|
|
250
|
+
injname = child_ref.name
|
|
251
|
+
module_name, _ = injname.split("#")
|
|
252
|
+
to_walk_list = module_name.split(".")
|
|
253
|
+
path_stack = []
|
|
254
|
+
|
|
255
|
+
self._add_descendent(child_ref, to_walk_list, path_stack, group_type=group_type)
|
|
256
|
+
|
|
257
|
+
return
|
|
258
|
+
|
|
259
|
+
def finalize(self):
|
|
260
|
+
"""
|
|
261
|
+
Marks the group as finalized, indicating setup is complete.
|
|
262
|
+
|
|
263
|
+
Once finalized, the group's structure should not be modified. This method
|
|
264
|
+
is called after all injection moments have been added and metadata has
|
|
265
|
+
been resolved, signaling that the group is ready for test execution.
|
|
266
|
+
|
|
267
|
+
The finalized state can be checked via the :attr:`finalized` property.
|
|
268
|
+
"""
|
|
269
|
+
self._finalized = True
|
|
270
|
+
return
|
|
271
|
+
|
|
272
|
+
def get_resource_scope(self) -> InjectionScope:
|
|
273
|
+
"""
|
|
274
|
+
Retrieves the resource scope associated with this group from the injection registry.
|
|
275
|
+
|
|
276
|
+
The resource scope contains parameter and validator originations that are visible
|
|
277
|
+
at this group's level in the hierarchy. This method looks up the scope using the
|
|
278
|
+
group's :attr:`scope_name` property.
|
|
279
|
+
|
|
280
|
+
:returns: The :class:`InjectionScope` instance associated with this group's
|
|
281
|
+
scope name, or ``None`` if no scope exists for this group.
|
|
282
|
+
"""
|
|
283
|
+
scope_name = self.scope_name
|
|
284
|
+
rscope = injection_registry.lookup_resource_scope(scope_name)
|
|
285
|
+
return rscope
|
|
286
|
+
|
|
287
|
+
def resolve_metadata(self, parent_metadata: Optional[Dict[str, str]]=None):
|
|
288
|
+
"""
|
|
289
|
+
Resolves metadata for this group and propagates it to all descendents.
|
|
290
|
+
|
|
291
|
+
This method implements the metadata propagation algorithm that flows metadata
|
|
292
|
+
from parent groups down to child groups and injection moments. It is typically
|
|
293
|
+
called on the root group after the hierarchy has been fully constructed.
|
|
294
|
+
|
|
295
|
+
Metadata Resolution Algorithm
|
|
296
|
+
------------------------------
|
|
297
|
+
1. Look up this group's reference metadata from its associated module's
|
|
298
|
+
``_metadata_`` attribute (via :meth:`_reference_metadata`)
|
|
299
|
+
2. Merge parent metadata with reference metadata:
|
|
300
|
+
|
|
301
|
+
- If both exist: create new dict, copy parent, then update with reference
|
|
302
|
+
(child values override parent values for same keys)
|
|
303
|
+
- If only parent exists: use parent metadata directly
|
|
304
|
+
- If only reference exists: use reference metadata directly
|
|
305
|
+
- If neither exists: metadata remains ``None``
|
|
306
|
+
|
|
307
|
+
3. Store the resolved metadata in ``self._metadata``
|
|
308
|
+
4. Recursively call :meth:`resolve_metadata` on all children, passing
|
|
309
|
+
the resolved metadata as the new parent metadata
|
|
310
|
+
|
|
311
|
+
Metadata Inheritance Example
|
|
312
|
+
----------------------------
|
|
313
|
+
Given a module hierarchy where:
|
|
314
|
+
|
|
315
|
+
- ``tests`` module has ``_metadata_ = {"priority": "high", "team": "core"}``
|
|
316
|
+
- ``tests.unit`` module has ``_metadata_ = {"priority": "low"}``
|
|
317
|
+
|
|
318
|
+
After resolution:
|
|
319
|
+
|
|
320
|
+
- ``tests`` group has metadata ``{"priority": "high", "team": "core"}``
|
|
321
|
+
- ``tests.unit`` group has metadata ``{"priority": "low", "team": "core"}``
|
|
322
|
+
(priority overridden, team inherited)
|
|
323
|
+
|
|
324
|
+
:param parent_metadata: Metadata dictionary inherited from the parent group.
|
|
325
|
+
When ``None``, this group is either the root or has no parent metadata
|
|
326
|
+
to inherit.
|
|
327
|
+
"""
|
|
328
|
+
|
|
329
|
+
reference_metadata = self._reference_metadata()
|
|
330
|
+
|
|
331
|
+
if parent_metadata is not None:
|
|
332
|
+
if reference_metadata is not None:
|
|
333
|
+
self._metadata = {}
|
|
334
|
+
self._metadata.update(parent_metadata)
|
|
335
|
+
self._metadata.update(reference_metadata)
|
|
336
|
+
else:
|
|
337
|
+
self._metadata = parent_metadata
|
|
338
|
+
else:
|
|
339
|
+
self._metadata = reference_metadata
|
|
340
|
+
|
|
341
|
+
child: Union[InjectionMoment, InjectionGroup]
|
|
342
|
+
for child in self._children.values():
|
|
343
|
+
child.resolve_metadata(self._metadata)
|
|
344
|
+
|
|
345
|
+
return
|
|
346
|
+
|
|
347
|
+
def _add_descendent(self, child_ref: InjectionMoment, to_walk_list: List[str], path_stack: List[str], group_type: Optional[type] = None):
|
|
348
|
+
"""
|
|
349
|
+
Internal recursive helper that walks the group tree and inserts a child.
|
|
350
|
+
|
|
351
|
+
This method implements the recursive tree traversal for hierarchy construction.
|
|
352
|
+
It is called by :meth:`add_descendent` and recursively calls itself to walk
|
|
353
|
+
down the module path until reaching the insertion point.
|
|
354
|
+
|
|
355
|
+
Tree Walking Algorithm
|
|
356
|
+
----------------------
|
|
357
|
+
1. If ``to_walk_list`` is empty, we've reached the insertion point:
|
|
358
|
+
|
|
359
|
+
- Insert the injection moment using its ``base_name`` as the key
|
|
360
|
+
|
|
361
|
+
2. Otherwise, process the next path component:
|
|
362
|
+
|
|
363
|
+
- Extract the first element from ``to_walk_list`` as ``child_leaf``
|
|
364
|
+
- Check if a child group already exists for ``child_leaf``
|
|
365
|
+
- If not, create a new group using ``group_type`` (or ``type(self)``)
|
|
366
|
+
- Push ``child_leaf`` onto ``path_stack`` for package path tracking
|
|
367
|
+
- Recursively call ``_add_descendent`` on the child group
|
|
368
|
+
- Pop ``child_leaf`` from ``path_stack`` after recursion completes
|
|
369
|
+
|
|
370
|
+
Path Stack Usage
|
|
371
|
+
----------------
|
|
372
|
+
The ``path_stack`` accumulates the traversed path components and is used
|
|
373
|
+
to construct the ``package`` attribute for newly created intermediate groups.
|
|
374
|
+
For example, when creating a group at path ``["tests", "unit", "auth"]``:
|
|
375
|
+
|
|
376
|
+
- At "tests": path_stack=[], new group has package=""
|
|
377
|
+
- At "unit": path_stack=["tests"], new group has package="tests"
|
|
378
|
+
- At "auth": path_stack=["tests", "unit"], new group has package="tests.unit"
|
|
379
|
+
|
|
380
|
+
:param child_ref: The :class:`InjectionMoment` being inserted into the tree.
|
|
381
|
+
:param to_walk_list: Remaining path segments to traverse before insertion.
|
|
382
|
+
:param path_stack: Stack of already-traversed path segments, used to build
|
|
383
|
+
the package path for new intermediate groups.
|
|
384
|
+
:param group_type: Optional class to use when creating new group nodes.
|
|
385
|
+
Defaults to ``type(self)`` if not specified.
|
|
386
|
+
"""
|
|
387
|
+
|
|
388
|
+
if len(to_walk_list) == 0:
|
|
389
|
+
tbname = child_ref.base_name
|
|
390
|
+
self._children[tbname] = child_ref
|
|
391
|
+
else:
|
|
392
|
+
child_leaf = to_walk_list[0]
|
|
393
|
+
|
|
394
|
+
desc_to_walk_list = []
|
|
395
|
+
if len(to_walk_list) > 1:
|
|
396
|
+
desc_to_walk_list = to_walk_list[1:]
|
|
397
|
+
|
|
398
|
+
child_leaf_group = None
|
|
399
|
+
if child_leaf in self._children:
|
|
400
|
+
child_leaf_group = self._children[child_leaf]
|
|
401
|
+
else:
|
|
402
|
+
if group_type is None:
|
|
403
|
+
group_type = type(self)
|
|
404
|
+
|
|
405
|
+
tgname = child_leaf
|
|
406
|
+
tgpkg = ".".join(path_stack)
|
|
407
|
+
|
|
408
|
+
child_leaf_group = group_type(tgname, tgpkg)
|
|
409
|
+
self._children[child_leaf] = child_leaf_group
|
|
410
|
+
|
|
411
|
+
path_stack.append(child_leaf)
|
|
412
|
+
try:
|
|
413
|
+
child_leaf_group._add_descendent(child_ref, desc_to_walk_list, path_stack)
|
|
414
|
+
finally:
|
|
415
|
+
path_stack.pop()
|
|
416
|
+
|
|
417
|
+
return
|
|
418
|
+
|
|
419
|
+
def _reference_metadata(self):
|
|
420
|
+
"""
|
|
421
|
+
Looks up the metadata defined on the module associated with this group.
|
|
422
|
+
|
|
423
|
+
This method checks if the module corresponding to this group's scope name
|
|
424
|
+
has been imported and contains a ``_metadata_`` attribute. The ``_metadata_``
|
|
425
|
+
attribute is a convention for defining metadata that should apply to all
|
|
426
|
+
tests within a module.
|
|
427
|
+
|
|
428
|
+
Module Lookup Process
|
|
429
|
+
---------------------
|
|
430
|
+
1. Construct the full module name from ``package`` and ``name``
|
|
431
|
+
2. Check if the module exists in ``sys.modules``
|
|
432
|
+
3. If found, check for a ``_metadata_`` attribute on the module
|
|
433
|
+
4. Return the metadata dictionary if found, otherwise ``None``
|
|
434
|
+
|
|
435
|
+
Example Module with Metadata::
|
|
436
|
+
|
|
437
|
+
# In tests/unit/test_auth.py
|
|
438
|
+
_metadata_ = {
|
|
439
|
+
"priority": "high",
|
|
440
|
+
"team": "security",
|
|
441
|
+
"tags": ["authentication", "security"]
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
def test_login():
|
|
445
|
+
pass # This test inherits the module's metadata
|
|
446
|
+
|
|
447
|
+
:returns: The metadata dictionary from the module's ``_metadata_`` attribute,
|
|
448
|
+
or ``None`` if the module is not loaded or has no metadata defined.
|
|
449
|
+
"""
|
|
450
|
+
|
|
451
|
+
modname = self._name
|
|
452
|
+
if self._package is not None and self._package != "":
|
|
453
|
+
modname = "{}.{}".format(self._package, self._name)
|
|
454
|
+
|
|
455
|
+
refmd = None
|
|
456
|
+
if modname in sys.modules:
|
|
457
|
+
mod = sys.modules[modname]
|
|
458
|
+
if hasattr(mod, "_metadata_"):
|
|
459
|
+
refmd = mod._metadata_
|
|
460
|
+
|
|
461
|
+
return refmd
|
|
462
|
+
|
|
463
|
+
def __contains__(self, key):
|
|
464
|
+
"""
|
|
465
|
+
Checks whether a child with *key* exists.
|
|
466
|
+
|
|
467
|
+
:param key: Name of the child to check.
|
|
468
|
+
|
|
469
|
+
:returns: ``True`` if the child exists.
|
|
470
|
+
"""
|
|
471
|
+
has_item = key in self._children
|
|
472
|
+
return has_item
|
|
473
|
+
|
|
474
|
+
def __getitem__(self, key):
|
|
475
|
+
"""
|
|
476
|
+
Retrieves a child by *key*.
|
|
477
|
+
|
|
478
|
+
:param key: Name of the child to retrieve.
|
|
479
|
+
|
|
480
|
+
:returns: The requested child object.
|
|
481
|
+
"""
|
|
482
|
+
item = self._children[key]
|
|
483
|
+
return item
|
|
484
|
+
|
|
485
|
+
def __setitem__(self, key, value):
|
|
486
|
+
"""
|
|
487
|
+
Inserts or replaces a child in the group.
|
|
488
|
+
|
|
489
|
+
:param key: Name of the child.
|
|
490
|
+
:param value: The child object to store.
|
|
491
|
+
"""
|
|
492
|
+
self._children[key] = value
|
|
493
|
+
return
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: ccai.testbase.injection.injectionmoment
|
|
3
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
4
|
+
:synopsis: Module for injectionmoment.
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Myron Walker <myron.walker@gmail.com>
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
__author__ = "Myron Walker"
|
|
12
|
+
__copyright__ = "Copyright 2025, Code Craft AI LLC"
|
|
13
|
+
__credits__ = []
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
from typing import Any, Dict, List, OrderedDict, Optional, Sequence
|
|
18
|
+
|
|
19
|
+
from types import FunctionType
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
import collections
|
|
23
|
+
import inspect
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
from ccai.testbase.markers import MetaFilter
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class InjectionMoment:
|
|
30
|
+
"""
|
|
31
|
+
The :class:`InjectiionMoment` objects are used to record the details around the injection of executable content and resource creation code into an execution scope.
|
|
32
|
+
:class:`InjectiionMoment` instances provide a reference to factory or test function, the injection location, the lifespan and any other constraints associated with
|
|
33
|
+
a injection.
|
|
34
|
+
|
|
35
|
+
The :class:`InjectiionMoment` object allows us to delay the creation of injectable runtime instance data and state until it is necessary to instantiate it and allows
|
|
36
|
+
us to cleanup the runtime instance and state as soon as it is no longer being used.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self, injfunc: FunctionType, monikers: Optional[List[str]] = None, pivots: Optional[OrderedDict[str, Any]] = None):
|
|
40
|
+
"""
|
|
41
|
+
Initializes the injectable reference object.
|
|
42
|
+
"""
|
|
43
|
+
if monikers is None:
|
|
44
|
+
monikers = []
|
|
45
|
+
|
|
46
|
+
if pivots is None:
|
|
47
|
+
pivots = collections.OrderedDict()
|
|
48
|
+
|
|
49
|
+
self._inj_function = injfunc
|
|
50
|
+
self._monikers = monikers
|
|
51
|
+
self._pivots = pivots
|
|
52
|
+
self._subscriptions = {}
|
|
53
|
+
self._finalized = False
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def finalized(self) -> bool:
|
|
58
|
+
return self._finalized
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def monikers(self) -> List[str]:
|
|
62
|
+
return self._monikers
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def pivots(self) -> OrderedDict[str, Any]:
|
|
66
|
+
return self._pivots
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def scope_name(self) -> str:
|
|
70
|
+
return self.name
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def subscriptions(self):
|
|
74
|
+
return self._subscriptions
|
|
75
|
+
|
|
76
|
+
@subscriptions.setter
|
|
77
|
+
def subscriptions(self, val):
|
|
78
|
+
self._subscriptions = val
|
|
79
|
+
return
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def base_name(self) -> str:
|
|
83
|
+
tbname = self._inj_function.__name__
|
|
84
|
+
return tbname
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def function(self) -> FunctionType:
|
|
88
|
+
"""
|
|
89
|
+
The injectable function
|
|
90
|
+
"""
|
|
91
|
+
return self._inj_function
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def function_parameters(self):
|
|
95
|
+
signature = inspect.signature(self._inj_function)
|
|
96
|
+
return signature.parameters
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def module_name(self) -> str:
|
|
100
|
+
return self._inj_function.__module__
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def name(self) -> str:
|
|
104
|
+
"""
|
|
105
|
+
The fully qualified name of the injectable that is referenced.
|
|
106
|
+
"""
|
|
107
|
+
tf = self._inj_function
|
|
108
|
+
inj_name = "%s#%s" % (tf.__module__, tf.__name__)
|
|
109
|
+
return inj_name
|
|
110
|
+
|
|
111
|
+
def finalize(self):
|
|
112
|
+
self._finalized = True
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
def is_member_of_metaset(self, metafilters: Sequence[MetaFilter]):
|
|
116
|
+
"""
|
|
117
|
+
Indicates if the associated injection belongs to a set that is associated with a collection of metafilters.
|
|
118
|
+
"""
|
|
119
|
+
include = True
|
|
120
|
+
|
|
121
|
+
for mfilter in metafilters:
|
|
122
|
+
if not mfilter.should_include(self._metadata):
|
|
123
|
+
include = False
|
|
124
|
+
|
|
125
|
+
return include
|
|
126
|
+
|
|
127
|
+
def resolve_metadata(self, parent_metadata: Optional[Dict[str, str]]=None):
|
|
128
|
+
|
|
129
|
+
reference_metadata = self._reference_metadata()
|
|
130
|
+
|
|
131
|
+
if parent_metadata is not None:
|
|
132
|
+
if reference_metadata is not None:
|
|
133
|
+
self._metadata = {}
|
|
134
|
+
self._metadata.update(parent_metadata)
|
|
135
|
+
self._metadata.update(reference_metadata)
|
|
136
|
+
else:
|
|
137
|
+
self._metadata = parent_metadata
|
|
138
|
+
else:
|
|
139
|
+
self._metadata = reference_metadata
|
|
140
|
+
|
|
141
|
+
return
|
|
142
|
+
|
|
143
|
+
def _reference_metadata(self):
|
|
144
|
+
"""
|
|
145
|
+
Looks up the metadata if any on the module associated with this group.
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
refmd = None
|
|
149
|
+
if hasattr(self._inj_function, "_metadata_"):
|
|
150
|
+
refmd = self._inj_function._metadata_
|
|
151
|
+
|
|
152
|
+
return refmd
|
|
153
|
+
|
|
154
|
+
def __str__(self):
|
|
155
|
+
return self.name
|