orionis 0.316.0__py3-none-any.whl → 0.318.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,156 @@
1
+ from orionis.services.introspection.callables.reflection_callable import ReflectionCallable
2
+ from orionis.services.introspection.dependencies.entities.callable_dependencies import CallableDependency
3
+ from orionis.unittesting import TestCase
4
+ from orionis.services.introspection.exceptions.reflection_type_error import ReflectionTypeError
5
+
6
+ class TestReflectionCallable(TestCase):
7
+
8
+ async def testInitValidFunction(self):
9
+ """
10
+ Tests that a ReflectionCallable object can be correctly initialized with a valid function.
11
+ Verifies that the callable stored in the ReflectionCallable instance matches the original function.
12
+ """
13
+ def sample_function(a, b=2):
14
+ """Sample docstring."""
15
+ return a + b
16
+ rc = ReflectionCallable(sample_function)
17
+ self.assertEqual(rc.getCallable(), sample_function)
18
+
19
+ async def testInitInvalid(self):
20
+ """
21
+ Test that initializing ReflectionCallable with an invalid argument (e.g., an integer)
22
+ raises a ReflectionTypeError.
23
+ """
24
+ with self.assertRaises(ReflectionTypeError):
25
+ ReflectionCallable(123)
26
+
27
+ async def testGetName(self):
28
+ """
29
+ Tests that the ReflectionCallable.getName() method correctly returns the name of the provided function.
30
+
31
+ This test defines a sample function, wraps it with ReflectionCallable, and asserts that getName()
32
+ returns the function's name as a string.
33
+ """
34
+ def sample_function(a, b=2):
35
+ """Sample docstring."""
36
+ return a + b
37
+ rc = ReflectionCallable(sample_function)
38
+ self.assertEqual(rc.getName(), "sample_function")
39
+
40
+ async def testGetModuleName(self):
41
+ """
42
+ Tests that the getModuleName method of ReflectionCallable returns the correct module name
43
+ for a given function. It verifies that the module name returned matches the __module__ attribute
44
+ of the sample function.
45
+ """
46
+ def sample_function(a, b=2):
47
+ """Sample docstring."""
48
+ return a + b
49
+ rc = ReflectionCallable(sample_function)
50
+ self.assertEqual(rc.getModuleName(), sample_function.__module__)
51
+
52
+ async def testGetModuleWithCallableName(self):
53
+ """
54
+ Tests that the `getModuleWithCallableName` method of the `ReflectionCallable` class
55
+ correctly returns the fully qualified name of a given callable, including its module
56
+ and function name.
57
+
58
+ The test defines a sample function, wraps it with `ReflectionCallable`, and asserts
59
+ that the returned string matches the expected format: "<module>.<function_name>".
60
+ """
61
+ def sample_function(a, b=2):
62
+ """Sample docstring."""
63
+ return a + b
64
+ rc = ReflectionCallable(sample_function)
65
+ expected = f"{sample_function.__module__}.sample_function"
66
+ self.assertEqual(rc.getModuleWithCallableName(), expected)
67
+
68
+ async def testGetDocstring(self):
69
+ """
70
+ Tests that the getDocstring method of ReflectionCallable correctly retrieves the docstring from a given function.
71
+ """
72
+ def sample_function(a, b=2):
73
+ """Sample docstring."""
74
+ return a + b
75
+ rc = ReflectionCallable(sample_function)
76
+ self.assertIn("Sample docstring", rc.getDocstring())
77
+
78
+ async def testGetSourceCode(self):
79
+ """
80
+ Tests that the getSourceCode method of ReflectionCallable correctly retrieves
81
+ the source code of a given function. Verifies that the returned code contains
82
+ the function definition for 'sample_function'.
83
+ """
84
+ def sample_function(a, b=2):
85
+ """Sample docstring."""
86
+ return a + b
87
+ rc = ReflectionCallable(sample_function)
88
+ code = rc.getSourceCode()
89
+ self.assertIn("def sample_function", code)
90
+
91
+ async def testGetSourceCodeError(self):
92
+ """
93
+ Test that ReflectionCallable.getSourceCode() raises a ReflectionTypeError
94
+ when called on a built-in function (e.g., len) that does not have accessible source code.
95
+ """
96
+ with self.assertRaises(ReflectionTypeError):
97
+ rc = ReflectionCallable(len)
98
+ rc.getSourceCode()
99
+
100
+ async def testGetFile(self):
101
+ """
102
+ Tests that the getFile() method of the ReflectionCallable class returns the correct file path
103
+ for a given callable. Verifies that the returned file path ends with '.py', indicating it is a
104
+ Python source file.
105
+ """
106
+ def sample_function(a, b=2):
107
+ """Sample docstring."""
108
+ return a + b
109
+ rc = ReflectionCallable(sample_function)
110
+ file_path = rc.getFile()
111
+ self.assertTrue(file_path.endswith(".py"))
112
+
113
+ async def testCallSync(self):
114
+ """
115
+ Tests the synchronous call functionality of the ReflectionCallable class.
116
+
117
+ This test defines a sample function with one required and one optional argument,
118
+ wraps it with ReflectionCallable, and asserts that calling it with specific arguments
119
+ returns the expected result.
120
+ """
121
+ def sample_function(a, b=2):
122
+ """Sample docstring."""
123
+ return a + b
124
+ rc = ReflectionCallable(sample_function)
125
+ self.assertEqual(rc.call(1, 2), 3)
126
+
127
+ async def testCallAsync(self):
128
+ """
129
+ Tests the ReflectionCallable's ability to call an asynchronous function synchronously.
130
+
131
+ This test defines an asynchronous function `sample_async_function` that takes two arguments and returns their sum.
132
+ It then wraps this function with `ReflectionCallable` and asserts that calling it with arguments (1, 2) returns 3.
133
+ """
134
+ async def sample_async_function(a, b=2):
135
+ """Async docstring."""
136
+ return a + b
137
+ rc = ReflectionCallable(sample_async_function)
138
+ self.assertEqual(await rc.call(1, 2), 3)
139
+
140
+ async def testGetDependencies(self):
141
+ """
142
+ Tests the getDependencies method of the ReflectionCallable class.
143
+
144
+ This test defines a sample function with one required and one default argument,
145
+ creates a ReflectionCallable instance for it, and retrieves its dependencies.
146
+ It then asserts that the returned dependencies object has both 'resolved' and
147
+ 'unresolved' attributes.
148
+ """
149
+ def sample_function(a, b=2):
150
+ """Sample docstring."""
151
+ return a + b
152
+ rc = ReflectionCallable(sample_function)
153
+ deps = rc.getDependencies()
154
+ self.assertIsInstance(deps, CallableDependency)
155
+ self.assertTrue(hasattr(deps, "resolved"))
156
+ self.assertTrue(hasattr(deps, "unresolved"))