orionis 0.306.0__py3-none-any.whl → 0.307.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,397 @@
1
+ from orionis.unittesting import TestCase
2
+ from orionis.services.introspection.modules.reflection_module import ReflectionModule
3
+ from orionis.services.introspection.exceptions.reflection_type_error import ReflectionTypeError
4
+ from orionis.services.introspection.exceptions.reflection_value_error import ReflectionValueError
5
+
6
+ class TestServiceReflectionModule(TestCase):
7
+
8
+ module_name = 'tests.services.inspection.reflection.mock.fake_reflect_instance'
9
+
10
+ async def testGetModule(self):
11
+ """
12
+ Test that the `getModule` method of the `ReflectionModule` class returns a module object
13
+ whose `__name__` attribute matches the expected module name.
14
+ """
15
+ reflection = ReflectionModule(self.module_name)
16
+ module = reflection.getModule()
17
+ self.assertEqual(module.__name__, self.module_name)
18
+
19
+ async def testHasClass(self):
20
+ """
21
+ Tests the hasClass method of the ReflectionModule.
22
+
23
+ This test verifies that hasClass correctly identifies the presence or absence of a class within the module:
24
+ - Asserts that hasClass returns True for an existing class ('PublicFakeClass').
25
+ - Asserts that hasClass returns False for a non-existent class ('NonExistentClass').
26
+ """
27
+ reflection = ReflectionModule(self.module_name)
28
+ self.assertTrue(reflection.hasClass('PublicFakeClass'))
29
+ self.assertFalse(reflection.hasClass('NonExistentClass'))
30
+
31
+ async def testGetClass(self):
32
+ """
33
+ Test the `getClass` method of the ReflectionModule.
34
+
35
+ This test verifies that:
36
+ - The method returns the correct class object when given a valid class name ('PublicFakeClass').
37
+ - The returned class object has the expected name.
38
+ - The method returns None when given a non-existent class name ('NonExistentClass').
39
+ """
40
+ reflection = ReflectionModule(self.module_name)
41
+ cls = reflection.getClass('PublicFakeClass')
42
+ self.assertIsNotNone(cls)
43
+ self.assertEqual(cls.__name__, 'PublicFakeClass')
44
+ self.assertIsNone(reflection.getClass('NonExistentClass'))
45
+
46
+ async def testSetAndRemoveClass(self):
47
+ """
48
+ Test the functionality of setting, retrieving, and removing a class in the ReflectionModule.
49
+
50
+ This test verifies that:
51
+ - A class can be registered with the ReflectionModule using setClass.
52
+ - The presence of the class can be checked with hasClass.
53
+ - The registered class can be retrieved with getClass.
54
+ - The class can be removed with removeClass.
55
+ - After removal, hasClass returns False for the class name.
56
+ """
57
+ reflection = ReflectionModule(self.module_name)
58
+ class MockClass:
59
+ pass
60
+ reflection.setClass('MockClass', MockClass)
61
+ self.assertTrue(reflection.hasClass('MockClass'))
62
+ self.assertEqual(reflection.getClass('MockClass'), MockClass)
63
+ reflection.removeClass('MockClass')
64
+ self.assertFalse(reflection.hasClass('MockClass'))
65
+
66
+ async def testSetClassInvalid(self):
67
+ """
68
+ Test that the `setClass` method of `ReflectionModule` raises a `ReflectionValueError`
69
+ when provided with invalid class names or class types.
70
+
71
+ Scenarios tested:
72
+ - Class name starts with a digit.
73
+ - Class name is a reserved keyword.
74
+ - Class type is not a valid type (e.g., passing an integer instead of a class).
75
+ """
76
+ reflection = ReflectionModule(self.module_name)
77
+ with self.assertRaises(ReflectionValueError):
78
+ reflection.setClass('123Invalid', object)
79
+ with self.assertRaises(ReflectionValueError):
80
+ reflection.setClass('class', object)
81
+ with self.assertRaises(ReflectionValueError):
82
+ reflection.setClass('ValidName', 123)
83
+
84
+ async def testRemoveClassInvalid(self):
85
+ """
86
+ Test that attempting to remove a non-existent class from the ReflectionModule raises a ValueError.
87
+
88
+ This test verifies that the `removeClass` method correctly handles the case where the specified class
89
+ does not exist in the module by raising a `ValueError` exception.
90
+ """
91
+ reflection = ReflectionModule(self.module_name)
92
+ with self.assertRaises(ValueError):
93
+ reflection.removeClass('NonExistentClass')
94
+
95
+ async def testInitClass(self):
96
+ """
97
+ Tests the initialization of a class using the ReflectionModule.
98
+
99
+ This test verifies that:
100
+ - An instance of 'PublicFakeClass' can be successfully created using the initClass method.
101
+ - The created instance has the correct class name.
102
+ - Attempting to initialize a non-existent class ('NonExistentClass') raises a ReflectionValueError.
103
+ """
104
+ reflection = ReflectionModule(self.module_name)
105
+ instance = reflection.initClass('PublicFakeClass')
106
+ self.assertEqual(instance.__class__.__name__, 'PublicFakeClass')
107
+ with self.assertRaises(ReflectionValueError):
108
+ reflection.initClass('NonExistentClass')
109
+
110
+ async def testGetClasses(self):
111
+ """
112
+ Test that the `getClasses` method of the `ReflectionModule` returns a list of class names
113
+ defined in the module, including public, protected, and private classes.
114
+ """
115
+ reflection = ReflectionModule(self.module_name)
116
+ classes = reflection.getClasses()
117
+ self.assertIn('PublicFakeClass', classes)
118
+ self.assertIn('_ProtectedFakeClass', classes)
119
+ self.assertIn('__PrivateFakeClass', classes)
120
+
121
+ async def testGetPublicClasses(self):
122
+ """
123
+ Test that the `getPublicClasses` method of the `ReflectionModule` returns only public class names.
124
+ Verifies that:
125
+ - Public class names (e.g., 'PublicFakeClass') are included in the result.
126
+ - Protected (e.g., '_ProtectedFakeClass') and private (e.g., '__PrivateFakeClass') class names are excluded.
127
+ """
128
+ reflection = ReflectionModule(self.module_name)
129
+ public_classes = reflection.getPublicClasses()
130
+ self.assertIn('PublicFakeClass', public_classes)
131
+ self.assertNotIn('_ProtectedFakeClass', public_classes)
132
+ self.assertNotIn('__PrivateFakeClass', public_classes)
133
+
134
+ async def testGetProtectedClasses(self):
135
+ """
136
+ Test that the ReflectionModule correctly identifies protected classes.
137
+
138
+ This test verifies that the `getProtectedClasses` method returns a list containing
139
+ the names of protected classes (those prefixed with an underscore) and excludes
140
+ public class names. Specifically, it checks that '_ProtectedFakeClass' is present
141
+ in the returned list, while 'PublicFakeClass' is not.
142
+ """
143
+ reflection = ReflectionModule(self.module_name)
144
+ protected_classes = reflection.getProtectedClasses()
145
+ self.assertIn('_ProtectedFakeClass', protected_classes)
146
+ self.assertNotIn('PublicFakeClass', protected_classes)
147
+
148
+ async def testGetPrivateClasses(self):
149
+ """
150
+ Test that the `getPrivateClasses` method of the `ReflectionModule` correctly identifies private classes within the specified module.
151
+
152
+ This test verifies that:
153
+ - The private class '__PrivateFakeClass' is present in the list of private classes returned.
154
+ - The public class 'PublicFakeClass' is not included in the list of private classes.
155
+ """
156
+ reflection = ReflectionModule(self.module_name)
157
+ private_classes = reflection.getPrivateClasses()
158
+ self.assertIn('__PrivateFakeClass', private_classes)
159
+ self.assertNotIn('PublicFakeClass', private_classes)
160
+
161
+ async def testGetConstants(self):
162
+ """
163
+ Test that the `getConstants` method of the `ReflectionModule` retrieves all types of constants
164
+ (public, protected, and private) defined in the module.
165
+
166
+ Asserts:
167
+ - 'PUBLIC_CONSTANT' is present in the returned constants.
168
+ - '_PROTECTED_CONSTANT' is present in the returned constants.
169
+ - '__PRIVATE_CONSTANT' is present in the returned constants.
170
+ """
171
+ reflection = ReflectionModule(self.module_name)
172
+ consts = reflection.getConstants()
173
+ self.assertIn('PUBLIC_CONSTANT', consts)
174
+ self.assertIn('_PROTECTED_CONSTANT', consts)
175
+ self.assertIn('__PRIVATE_CONSTANT', consts)
176
+
177
+ async def testGetPublicConstants(self):
178
+ """
179
+ Test that `getPublicConstants` returns only public constants from the module.
180
+
181
+ This test verifies that:
182
+ - 'PUBLIC_CONSTANT' is present in the returned list of public constants.
183
+ - '_PROTECTED_CONSTANT' and '__PRIVATE_CONSTANT' are not included in the list, ensuring that protected and private constants are excluded.
184
+ """
185
+ reflection = ReflectionModule(self.module_name)
186
+ public_consts = reflection.getPublicConstants()
187
+ self.assertIn('PUBLIC_CONSTANT', public_consts)
188
+ self.assertNotIn('_PROTECTED_CONSTANT', public_consts)
189
+ self.assertNotIn('__PRIVATE_CONSTANT', public_consts)
190
+
191
+ async def testGetProtectedConstants(self):
192
+ """
193
+ Tests that the `getProtectedConstants` method of the `ReflectionModule` class
194
+ correctly retrieves protected constants (those prefixed with an underscore) from the module.
195
+
196
+ Asserts that '_PROTECTED_CONSTANT' is present in the returned list of protected constants,
197
+ and that 'PUBLIC_CONSTANT' is not included.
198
+ """
199
+ reflection = ReflectionModule(self.module_name)
200
+ protected_consts = reflection.getProtectedConstants()
201
+ self.assertIn('_PROTECTED_CONSTANT', protected_consts)
202
+ self.assertNotIn('PUBLIC_CONSTANT', protected_consts)
203
+
204
+ async def testGetPrivateConstants(self):
205
+ """
206
+ Test that the getPrivateConstants method of the ReflectionModule correctly retrieves private constants.
207
+
208
+ This test verifies that:
209
+ - The private constant '__PRIVATE_CONSTANT' is present in the list of private constants.
210
+ - The public constant 'PUBLIC_CONSTANT' is not included in the list of private constants.
211
+ """
212
+ reflection = ReflectionModule(self.module_name)
213
+ private_consts = reflection.getPrivateConstants()
214
+ self.assertIn('__PRIVATE_CONSTANT', private_consts)
215
+ self.assertNotIn('PUBLIC_CONSTANT', private_consts)
216
+
217
+ async def testGetConstant(self):
218
+ """
219
+ Test the `getConstant` method of the ReflectionModule.
220
+
221
+ This test verifies that:
222
+ - Retrieving an existing constant ('PUBLIC_CONSTANT') returns its expected value ('public constant').
223
+ - Retrieving a non-existent constant ('NON_EXISTENT_CONST') returns None.
224
+ """
225
+ reflection = ReflectionModule(self.module_name)
226
+ value = reflection.getConstant('PUBLIC_CONSTANT')
227
+ self.assertEqual(value, 'public constant')
228
+ self.assertIsNone(reflection.getConstant('NON_EXISTENT_CONST'))
229
+
230
+ async def testGetFunctions(self):
231
+ """
232
+ Test that the ReflectionModule correctly retrieves all function names, including public, protected, and private methods, from the specified module.
233
+ """
234
+ reflection = ReflectionModule(self.module_name)
235
+ funcs = reflection.getFunctions()
236
+ self.assertIn('publicSyncFunction', funcs)
237
+ self.assertIn('publicAsyncFunction', funcs)
238
+ self.assertIn('_protectedSyncFunction', funcs)
239
+ self.assertIn('_protectedAsyncFunction', funcs)
240
+ self.assertIn('__privateSyncFunction', funcs)
241
+ self.assertIn('__privateAsyncFunction', funcs)
242
+
243
+ async def testGetPublicFunctions(self):
244
+ """
245
+ Test that ReflectionModule.getPublicFunctions() returns only public functions.
246
+
247
+ This test verifies that:
248
+ - Public synchronous and asynchronous functions are included in the returned list.
249
+ - Protected (prefixed with a single underscore) and private (prefixed with double underscores) functions are not included.
250
+ """
251
+ reflection = ReflectionModule(self.module_name)
252
+ public_funcs = reflection.getPublicFunctions()
253
+ self.assertIn('publicSyncFunction', public_funcs)
254
+ self.assertIn('publicAsyncFunction', public_funcs)
255
+ self.assertNotIn('_protectedSyncFunction', public_funcs)
256
+ self.assertNotIn('__privateSyncFunction', public_funcs)
257
+
258
+ async def testGetPublicSyncFunctions(self):
259
+ """
260
+ Test that `getPublicSyncFunctions` returns only public synchronous functions.
261
+
262
+ This test verifies that:
263
+ - The list of public synchronous functions includes 'publicSyncFunction'.
264
+ - The list does not include 'publicAsyncFunction', ensuring only synchronous functions are returned.
265
+ """
266
+ reflection = ReflectionModule(self.module_name)
267
+ sync_funcs = reflection.getPublicSyncFunctions()
268
+ self.assertIn('publicSyncFunction', sync_funcs)
269
+ self.assertNotIn('publicAsyncFunction', sync_funcs)
270
+
271
+ async def testGetPublicAsyncFunctions(self):
272
+ """
273
+ Test that ReflectionModule.getPublicAsyncFunctions() returns a list containing the names of public asynchronous functions,
274
+ including 'publicAsyncFunction', and excludes synchronous functions such as 'publicSyncFunction'.
275
+ """
276
+ reflection = ReflectionModule(self.module_name)
277
+ async_funcs = reflection.getPublicAsyncFunctions()
278
+ self.assertIn('publicAsyncFunction', async_funcs)
279
+ self.assertNotIn('publicSyncFunction', async_funcs)
280
+
281
+ async def testGetProtectedFunctions(self):
282
+ """
283
+ Test that the ReflectionModule correctly identifies protected functions.
284
+
285
+ This test verifies that:
286
+ - Protected functions (those prefixed with a single underscore) are included in the list returned by getProtectedFunctions().
287
+ - Public functions (those without a leading underscore) are not included in the list of protected functions.
288
+ """
289
+ reflection = ReflectionModule(self.module_name)
290
+ protected_funcs = reflection.getProtectedFunctions()
291
+ self.assertIn('_protectedSyncFunction', protected_funcs)
292
+ self.assertIn('_protectedAsyncFunction', protected_funcs)
293
+ self.assertNotIn('publicSyncFunction', protected_funcs)
294
+
295
+ async def testGetProtectedSyncFunctions(self):
296
+ """
297
+ Test that the `getProtectedSyncFunctions` method of the `ReflectionModule` class
298
+ returns a list containing protected synchronous function names, specifically
299
+ including '_protectedSyncFunction' and excluding '_protectedAsyncFunction'.
300
+ """
301
+ reflection = ReflectionModule(self.module_name)
302
+ sync_funcs = reflection.getProtectedSyncFunctions()
303
+ self.assertIn('_protectedSyncFunction', sync_funcs)
304
+ self.assertNotIn('_protectedAsyncFunction', sync_funcs)
305
+
306
+ async def testGetProtectedAsyncFunctions(self):
307
+ """
308
+ Test that the ReflectionModule correctly identifies protected asynchronous functions.
309
+
310
+ This test verifies that:
311
+ - The list of protected async functions returned by `getProtectedAsyncFunctions()` includes '_protectedAsyncFunction'.
312
+ - The list does not include '_protectedSyncFunction', ensuring only async functions are returned.
313
+ """
314
+ reflection = ReflectionModule(self.module_name)
315
+ async_funcs = reflection.getProtectedAsyncFunctions()
316
+ self.assertIn('_protectedAsyncFunction', async_funcs)
317
+ self.assertNotIn('_protectedSyncFunction', async_funcs)
318
+
319
+ async def testGetPrivateFunctions(self):
320
+ """
321
+ Test that ReflectionModule.getPrivateFunctions correctly identifies private functions.
322
+
323
+ This test verifies that:
324
+ - Private functions (e.g., '__privateSyncFunction', '__privateAsyncFunction') are included in the returned list.
325
+ - Public functions (e.g., 'publicSyncFunction') are not included in the returned list.
326
+ """
327
+ reflection = ReflectionModule(self.module_name)
328
+ private_funcs = reflection.getPrivateFunctions()
329
+ self.assertIn('__privateSyncFunction', private_funcs)
330
+ self.assertIn('__privateAsyncFunction', private_funcs)
331
+ self.assertNotIn('publicSyncFunction', private_funcs)
332
+
333
+ async def testGetPrivateSyncFunctions(self):
334
+ """
335
+ Test that the getPrivateSyncFunctions method of ReflectionModule returns a list containing
336
+ the names of private synchronous functions, specifically including '__privateSyncFunction'
337
+ and excluding '__privateAsyncFunction'.
338
+ """
339
+ reflection = ReflectionModule(self.module_name)
340
+ sync_funcs = reflection.getPrivateSyncFunctions()
341
+ self.assertIn('__privateSyncFunction', sync_funcs)
342
+ self.assertNotIn('__privateAsyncFunction', sync_funcs)
343
+
344
+ async def testGetPrivateAsyncFunctions(self):
345
+ """
346
+ Tests that the ReflectionModule correctly identifies private asynchronous functions.
347
+
348
+ This test verifies that:
349
+ - The list of private async functions returned by `getPrivateAsyncFunctions()` includes '__privateAsyncFunction'.
350
+ - The list does not include '__privateSyncFunction', ensuring only async functions are returned.
351
+ """
352
+ reflection = ReflectionModule(self.module_name)
353
+ async_funcs = reflection.getPrivateAsyncFunctions()
354
+ self.assertIn('__privateAsyncFunction', async_funcs)
355
+ self.assertNotIn('__privateSyncFunction', async_funcs)
356
+
357
+ async def testGetImports(self):
358
+ """
359
+ Test that the `getImports` method of the `ReflectionModule` correctly retrieves the list of imported modules.
360
+ Asserts that 'asyncio' is present in the returned imports.
361
+ """
362
+ reflection = ReflectionModule(self.module_name)
363
+ imports = reflection.getImports()
364
+ self.assertIn('asyncio', imports)
365
+
366
+ async def testGetFile(self):
367
+ """
368
+ Tests that the `getFile` method of the `ReflectionModule` returns the correct file path.
369
+
370
+ This test creates an instance of `ReflectionModule` with the specified module name,
371
+ retrieves the file path using `getFile`, and asserts that the returned path ends with
372
+ 'fake_reflect_instance.py', indicating the correct file is being referenced.
373
+ """
374
+ reflection = ReflectionModule(self.module_name)
375
+ file_path = reflection.getFile()
376
+ self.assertTrue(file_path.endswith('fake_reflect_instance.py'))
377
+
378
+ async def testGetSourceCode(self):
379
+ """
380
+ Tests that the `getSourceCode` method of the ReflectionModule retrieves the source code
381
+ containing specific elements such as 'PUBLIC_CONSTANT' and the function definition
382
+ 'def publicSyncFunction'. Asserts that these elements are present in the returned code.
383
+ """
384
+ reflection = ReflectionModule(self.module_name)
385
+ code = reflection.getSourceCode()
386
+ self.assertIn('PUBLIC_CONSTANT', code)
387
+ self.assertIn('def publicSyncFunction', code)
388
+
389
+ async def test_invalid_module_name(self):
390
+ """
391
+ Test that ReflectionModule raises a ReflectionTypeError when initialized with an invalid module name,
392
+ such as an empty string or a non-existent module path.
393
+ """
394
+ with self.assertRaises(ReflectionTypeError):
395
+ ReflectionModule('')
396
+ with self.assertRaises(ReflectionTypeError):
397
+ ReflectionModule('nonexistent.module.name')