orionis 0.471.0__py3-none-any.whl → 0.473.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.
@@ -0,0 +1,234 @@
1
+ import time
2
+ from orionis.container.container import Container
3
+ from orionis.test.cases.asynchronous import AsyncTestCase
4
+ from tests.container.mocks.mock_advanced_async import ErrorService, IPerformanceService, MixedConsumer, PerformanceService, simple_async, sync_returns_coroutine
5
+
6
+ class TestContainer(AsyncTestCase):
7
+
8
+ async def testPerformanceComparison(self):
9
+ """
10
+ Measures and verifies the performance of synchronous service calls within the container.
11
+
12
+ This test performs the following steps:
13
+ - Instantiates a container and registers `PerformanceService` as a singleton for the `IPerformanceService` interface.
14
+ - Resolves the registered service from the container.
15
+ - Executes the synchronous operation multiple times using the container's `call` method, measuring the total elapsed time.
16
+ - Asserts that the synchronous operation returns the expected result.
17
+ - Asserts that the measured execution time is greater than zero, confirming that the operation duration is measurable.
18
+
19
+ Returns
20
+ -------
21
+ None
22
+ This method does not return a value. Assertions are used to validate expected behavior.
23
+ """
24
+ # Create a container instance
25
+ container = Container()
26
+
27
+ # Register the PerformanceService as a singleton for the IPerformanceService interface
28
+ container.singleton(IPerformanceService, PerformanceService)
29
+
30
+ # Resolve the service instance from the container
31
+ service = container.make(IPerformanceService)
32
+
33
+ # Start timing the synchronous calls
34
+ start_time = time.time()
35
+
36
+ # Call the sync operation multiple times to measure performance
37
+ for i in range(3):
38
+ result = container.call(service, 'sync_operation')
39
+
40
+ # Calculate the total elapsed time for synchronous calls
41
+ sync_time = time.time() - start_time
42
+
43
+ # Assert that the sync operation returns the expected result
44
+ self.assertEqual(result, "Sync operation completed")
45
+
46
+ # Assert that the measured time is greater than zero
47
+ self.assertGreater(sync_time, 0, "Sync operation should take some time")
48
+
49
+ async def testAsyncPerformance(self):
50
+ """
51
+ Measures and verifies the performance of asynchronous service calls within the container.
52
+
53
+ This test performs the following steps:
54
+ - Instantiates a container and registers `PerformanceService` as a singleton for the `IPerformanceService` interface.
55
+ - Resolves the registered service from the container.
56
+ - Executes the asynchronous operation multiple times using both `call` and `callAsync` methods, measuring the total elapsed time.
57
+ - Asserts that the asynchronous operation returns the expected result.
58
+ - Asserts that the measured execution time is greater than zero, confirming that the operation duration is measurable.
59
+
60
+ Returns
61
+ -------
62
+ None
63
+ This method does not return a value. Assertions are used to validate expected behavior.
64
+
65
+ Raises
66
+ ------
67
+ AssertionError
68
+ If the asynchronous operation does not return the expected result or if the measured time is not greater than zero.
69
+ """
70
+
71
+ # Create a container instance
72
+ container = Container()
73
+
74
+ # Register the PerformanceService as a singleton for the IPerformanceService interface
75
+ container.singleton(IPerformanceService, PerformanceService)
76
+
77
+ # Resolve the service instance from the container
78
+ service = container.make(IPerformanceService)
79
+
80
+ # Start timing the asynchronous calls
81
+ start_time = time.time()
82
+
83
+ # Call the async operation multiple times using the container's call method
84
+ for i in range(3):
85
+
86
+ # Await the result of the async operation
87
+ result = container.call(service, 'async_operation')
88
+
89
+ # Calculate the total elapsed time for asynchronous calls
90
+ async_time = time.time() - start_time
91
+
92
+ # Assert that the async operation returns the expected result
93
+ self.assertEqual(result, "Async operation completed")
94
+
95
+ # Call the async operation multiple times using the container's callAsync method
96
+ for i in range(3):
97
+ result = await container.callAsync(service, 'async_operation')
98
+
99
+ # Assert that the async operation returns the expected result
100
+ self.assertEqual(result, "Async operation completed")
101
+
102
+ # Assert that the measured time is greater than zero
103
+ self.assertGreater(async_time, 0, "Async operation should take some time")
104
+
105
+ async def testErrorHandling(self):
106
+ """
107
+ Tests the error handling capabilities of the container for both synchronous and asynchronous service methods.
108
+
109
+ This method performs the following steps:
110
+ - Instantiates a container and resolves the `ErrorService`.
111
+ - Invokes a synchronous method that is expected to raise an exception and asserts that the exception is raised.
112
+ - (Asynchronous error handling is tested elsewhere.)
113
+
114
+ Returns
115
+ -------
116
+ None
117
+ This method does not return a value. Assertions are used to validate that exceptions are properly raised.
118
+ """
119
+
120
+ # Create a container instance
121
+ container = Container()
122
+
123
+ # Resolve the ErrorService instance from the container
124
+ service = container.make(ErrorService)
125
+
126
+ # Test synchronous error handling: expect an exception to be raised
127
+ with self.assertRaises(Exception) as context:
128
+ container.call(service, 'sync_error_method')
129
+
130
+ async def testAsyncErrors(self):
131
+
132
+ # Create a container instance
133
+ container = Container()
134
+
135
+ # Resolve the ErrorService instance from the container
136
+ service = container.make(ErrorService)
137
+
138
+ # Call the async error method and await its result
139
+ with self.assertRaises(Exception) as context:
140
+ await container.callAsync(service, 'async_error_method')
141
+
142
+ async def testMixedDependencyInjection(self):
143
+ """
144
+ Tests the container's ability to perform mixed synchronous and asynchronous dependency injection.
145
+
146
+ This test performs the following steps:
147
+ - Instantiates a container and uses it to auto-resolve dependencies for the `MixedConsumer` class.
148
+ - Invokes a synchronous method with dependency injection and verifies its return value.
149
+ - (Asynchronous methods are tested elsewhere.)
150
+
151
+ The test asserts that:
152
+ - The synchronous method returns a string starting with "Sync:".
153
+
154
+ Returns
155
+ -------
156
+ None
157
+ This method does not return a value. Assertions are used to validate expected behavior.
158
+ """
159
+
160
+ # Create an instance of the container
161
+ container = Container()
162
+
163
+ # Auto-resolve dependencies for MixedConsumer
164
+ consumer = container.make(MixedConsumer)
165
+
166
+ # Test synchronous method with dependency injection
167
+ sync_result: str = container.call(consumer, 'sync_method')
168
+
169
+ # Assert that the result starts with "Sync:"
170
+ self.assertTrue(sync_result.startswith("Sync:"))
171
+
172
+ async def testAsyncWithDI(self):
173
+ """
174
+ Tests the container's capability to perform asynchronous dependency injection.
175
+
176
+ This test performs the following steps:
177
+ - Instantiates a container and uses it to auto-resolve dependencies for the `MixedConsumer` class.
178
+ - Invokes an asynchronous method with dependency injection and verifies its return value.
179
+
180
+ The test asserts that:
181
+ - The asynchronous method returns a string starting with "Mixed: ".
182
+
183
+ Returns
184
+ -------
185
+ None
186
+ This method does not return a value. Assertions are used to validate expected behavior.
187
+ """
188
+
189
+ # Create an instance of the container
190
+ container = Container()
191
+
192
+ # Auto-resolve dependencies for MixedConsumer
193
+ consumer = container.make(MixedConsumer)
194
+
195
+ # Call the asynchronous method with dependency injection
196
+ async_result: str = await container.callAsync(consumer, 'async_method')
197
+
198
+ # Assert that the result starts with "Mixed: "
199
+ self.assertTrue(async_result.startswith("Mixed: "))
200
+
201
+ async def testCallableAsyncSync(self):
202
+ """
203
+ Tests the container's ability to register and resolve both synchronous and asynchronous callable functions,
204
+ ensuring that both types return coroutine objects when resolved.
205
+
206
+ This method performs the following steps:
207
+ - Instantiates a container and registers two callable functions:
208
+ - A synchronous function that returns a coroutine.
209
+ - An asynchronous function.
210
+ - Resolves each callable from the container.
211
+ - Asserts that the resolved objects are coroutine objects by checking for the '__await__' attribute.
212
+
213
+ Returns
214
+ -------
215
+ None
216
+ This method does not return a value. Assertions are used to validate that the resolved callables are coroutine objects.
217
+ """
218
+
219
+ # Create a container instance
220
+ container = Container()
221
+
222
+ # Register a synchronous function that returns a coroutine
223
+ container.callable("sync_returns_coro", sync_returns_coroutine)
224
+
225
+ # Register an asynchronous function
226
+ container.callable("simple_async", simple_async)
227
+
228
+ # Resolve the synchronous function and check if it returns a coroutine object
229
+ result1 = container.make("sync_returns_coro")
230
+ self.assertTrue(result1 == "Sync function returning coroutine")
231
+
232
+ # Resolve the asynchronous function and check if it returns a coroutine object
233
+ result2 = container.make("simple_async")
234
+ self.assertTrue(result2 == "Simple async callable")
@@ -0,0 +1,268 @@
1
+ from orionis.container.container import Container
2
+ from orionis.test.cases.asynchronous import AsyncTestCase
3
+ from tests.container.mocks.mock_async_optimizations import AsyncTestService, AsyncTestServiceWithDependency, IAsyncTestService, IAsyncTestServiceWithDependency, ITestService, ITestServiceWithDependency, MixedService, TestService, TestServiceWithDependency, async_callable, async_callable_with_dependency, sync_callable, sync_callable_with_dependency
4
+
5
+ class TestContainer(AsyncTestCase):
6
+
7
+ async def testSyncServices(self):
8
+ """
9
+ Tests the registration and resolution of synchronous services in the container.
10
+
11
+ This method verifies that singleton and transient services can be registered and resolved correctly.
12
+ It checks that the singleton service returns the same instance on multiple resolutions, and that
13
+ the service with a dependency is properly constructed.
14
+
15
+ Returns
16
+ -------
17
+ None
18
+ This method does not return a value. Assertions are used to validate behavior.
19
+ """
20
+
21
+ # Create a new container instance
22
+ container = Container()
23
+
24
+ # Register a singleton service and a transient service with dependency
25
+ container.singleton(ITestService, TestService)
26
+ container.transient(ITestServiceWithDependency, TestServiceWithDependency)
27
+
28
+ # Resolve the singleton service and verify its method output
29
+ service: ITestService = container.make(ITestService)
30
+ self.assertEqual(service.get_message(), "Hello from sync service")
31
+
32
+ # Resolve the transient service with dependency and check its output
33
+ service_with_dep: ITestServiceWithDependency = container.make(ITestServiceWithDependency)
34
+ self.assertTrue(service_with_dep.get_combined_message().startswith("Combined: "))
35
+
36
+ # Ensure that singleton returns the same instance on multiple resolutions
37
+ service2: ITestService = container.make(ITestService)
38
+ self.assertIs(service, service2, "Singleton service should return the same instance")
39
+
40
+ async def testAsyncServices(self):
41
+ """
42
+ Tests the registration and resolution of asynchronous services in the dependency injection container.
43
+
44
+ This method ensures that asynchronous services can be registered as singletons or transients and resolved correctly.
45
+ It verifies that the resolved service instance matches the expected implementation type.
46
+
47
+ Parameters
48
+ ----------
49
+ self : TestContainer
50
+ The test case instance.
51
+
52
+ Returns
53
+ -------
54
+ None
55
+ This method does not return a value. Assertions are used to validate correct behavior.
56
+ """
57
+
58
+ # Create a new container instance
59
+ container = Container()
60
+
61
+ # Register an asynchronous singleton service
62
+ container.singleton(IAsyncTestService, AsyncTestService)
63
+
64
+ # Register an asynchronous transient service with dependency
65
+ container.transient(IAsyncTestServiceWithDependency, AsyncTestServiceWithDependency)
66
+
67
+ # Resolve the asynchronous singleton service and check its type
68
+ async_service = container.make(IAsyncTestService)
69
+ self.assertIsInstance(async_service, AsyncTestService)
70
+
71
+ async def testAsyncCalls(self):
72
+ """
73
+ Tests asynchronous service resolution and asynchronous method invocation in the dependency injection container.
74
+
75
+ This method verifies that asynchronous services can be registered, resolved, and their asynchronous methods invoked correctly.
76
+ It checks both singleton and transient registrations, and asserts that the returned values are as expected.
77
+
78
+ Parameters
79
+ ----------
80
+ self : TestContainer
81
+ The test case instance.
82
+
83
+ Returns
84
+ -------
85
+ None
86
+ This method does not return a value. Assertions are used to validate correct behavior.
87
+ """
88
+
89
+ # Create a new container instance
90
+ container = Container()
91
+
92
+ # Register an asynchronous singleton service
93
+ container.singleton(IAsyncTestService, AsyncTestService)
94
+
95
+ # Register an asynchronous transient service with dependency
96
+ container.transient(IAsyncTestServiceWithDependency, AsyncTestServiceWithDependency)
97
+
98
+ # Resolve the asynchronous singleton service
99
+ async_service: IAsyncTestService = container.make(IAsyncTestService)
100
+
101
+ # Call an asynchronous method on the resolved service and check the result
102
+ result = await container.callAsync(async_service, 'get_async_message')
103
+ self.assertEqual(result, "Hello from async service")
104
+
105
+ # Resolve the asynchronous transient service with dependency
106
+ service_with_dep: IAsyncTestServiceWithDependency = container.make(IAsyncTestServiceWithDependency)
107
+
108
+ # Call an asynchronous method that combines results from dependencies and check the result
109
+ result2: str = await container.callAsync(service_with_dep, 'get_combined_async_message')
110
+ self.assertTrue(result2.startswith("Combined: "))
111
+
112
+ async def testCallableRegistration(self):
113
+ """
114
+ Tests the registration and resolution of synchronous and asynchronous callables in the dependency injection container.
115
+
116
+ This method verifies that:
117
+ - Synchronous services can be registered as singletons.
118
+ - Synchronous callables can be registered and resolved by name.
119
+ - Callables with dependencies are correctly resolved and invoked.
120
+ - The resolved callables return the expected results.
121
+
122
+ Parameters
123
+ ----------
124
+ self : TestContainer
125
+ The test case instance.
126
+
127
+ Returns
128
+ -------
129
+ None
130
+ This method does not return a value. Assertions are used to validate correct behavior.
131
+ """
132
+
133
+ # Create a new container instance
134
+ container = Container()
135
+
136
+ # Register base services as singletons
137
+ container.singleton(ITestService, TestService)
138
+ container.singleton(IAsyncTestService, AsyncTestService)
139
+
140
+ # Register synchronous callables, including one with a dependency
141
+ container.callable("sync_func", sync_callable)
142
+ container.callable("sync_func_with_dep", sync_callable_with_dependency)
143
+
144
+ # Resolve and invoke the synchronous callable, then check its result
145
+ result1: str = container.make("sync_func")
146
+ self.assertEqual(result1, "Sync callable result")
147
+
148
+ # Resolve and invoke the callable with dependency, then check its result
149
+ result2: str = container.make("sync_func_with_dep")
150
+ self.assertTrue(result2.startswith("Callable with dependency: "))
151
+
152
+ async def testAsyncCallables(self):
153
+ """
154
+ Tests registration, resolution, and invocation of asynchronous callables in the dependency injection container.
155
+
156
+ This method ensures that:
157
+ - Asynchronous callables can be registered using the `callable` method.
158
+ - Registered asynchronous callables are resolved correctly from the container.
159
+ - Dependencies for asynchronous callables are properly injected by the container.
160
+ - The results returned by the asynchronous callables match the expected output.
161
+
162
+ Parameters
163
+ ----------
164
+ self : TestContainer
165
+ The test case instance.
166
+
167
+ Returns
168
+ -------
169
+ None
170
+ This method does not return a value. Assertions are used to validate correct behavior.
171
+ """
172
+
173
+ # Create a new container instance
174
+ container = Container()
175
+
176
+ # Register base services as singletons for dependency injection
177
+ container.singleton(ITestService, TestService)
178
+ container.singleton(IAsyncTestService, AsyncTestService)
179
+
180
+ # Register asynchronous callables, including one with a dependency
181
+ container.callable("async_func", async_callable)
182
+ container.callable("async_func_with_dep", async_callable_with_dependency)
183
+
184
+ # Resolve and invoke the asynchronous callable, then check its result
185
+ result1: str = container.make("async_func")
186
+ self.assertEqual(result1, "Async callable result")
187
+
188
+ # Resolve and invoke the asynchronous callable with dependency, then check its result
189
+ result2: str = container.make("async_func_with_dep")
190
+ self.assertTrue(result2.startswith("Async callable with dependency: "))
191
+
192
+ async def testMixedSyncAsync(self):
193
+ """
194
+ Tests the container's ability to integrate synchronous and asynchronous services and dependencies.
195
+
196
+ This method registers both a synchronous service (`TestService`) and an asynchronous service (`AsyncTestService`)
197
+ as singletons in the container. It then resolves a `MixedService` instance, which depends on both types of services,
198
+ and invokes its synchronous method `get_sync_message` using the container's `call` method. The test asserts that
199
+ the returned message from the synchronous method starts with "Mixed sync: ", verifying that the container can
200
+ correctly handle and inject both synchronous and asynchronous dependencies into a mixed service.
201
+
202
+ Parameters
203
+ ----------
204
+ self : TestContainer
205
+ The test case instance.
206
+
207
+ Returns
208
+ -------
209
+ None
210
+ This method does not return a value. Assertions are used to validate correct behavior.
211
+ """
212
+
213
+ # Create a new container instance
214
+ container = Container()
215
+
216
+ # Register synchronous and asynchronous services as singletons
217
+ container.singleton(ITestService, TestService)
218
+ container.singleton(IAsyncTestService, AsyncTestService)
219
+
220
+ # Resolve the mixed service, which depends on both sync and async services
221
+ mixed_service = container.make(MixedService)
222
+
223
+ # Invoke the synchronous method and check the result
224
+ sync_result: str = container.call(mixed_service, 'get_sync_message')
225
+ self.assertTrue(sync_result.startswith("Mixed sync: "))
226
+
227
+ async def testMixedAsync(self):
228
+ """
229
+ Tests the container's ability to integrate synchronous and asynchronous services and dependencies,
230
+ focusing on asynchronous method invocation.
231
+
232
+ This method performs the following verifications:
233
+ - Registers both a synchronous service (`TestService`) and an asynchronous service (`AsyncTestService`)
234
+ as singletons in the container.
235
+ - Resolves a `MixedService` instance, which depends on both types of services.
236
+ - Invokes the asynchronous method `get_async_message` on the mixed service using `callAsync` and asserts
237
+ that the returned message starts with "Mixed async: ".
238
+ - Invokes the asynchronous method `get_both_messages` on the mixed service using `callAsync` and asserts
239
+ that the returned message starts with "Both: ".
240
+
241
+ Parameters
242
+ ----------
243
+ self : TestContainer
244
+ The test case instance.
245
+
246
+ Returns
247
+ -------
248
+ None
249
+ This method does not return a value. Assertions are used to validate correct behavior.
250
+ """
251
+
252
+ # Create a new container instance
253
+ container = Container()
254
+
255
+ # Register synchronous and asynchronous services as singletons
256
+ container.singleton(ITestService, TestService)
257
+ container.singleton(IAsyncTestService, AsyncTestService)
258
+
259
+ # Resolve the mixed service, which depends on both sync and async services
260
+ mixed_service = container.make(MixedService)
261
+
262
+ # Invoke the asynchronous method and check the result
263
+ async_result: str = await container.callAsync(mixed_service, 'get_async_message')
264
+ self.assertTrue(async_result.startswith("Mixed async: "))
265
+
266
+ # Invoke the asynchronous method that combines both sync and async dependencies and check the result
267
+ both_result = await container.callAsync(mixed_service, 'get_both_messages')
268
+ self.assertTrue(both_result.startswith("Both: "))