test: convert demo script to unittest framework in test_rpc.py

Co-authored-by: aider (openrouter/openai/o3-mini-high) <aider@aider.chat>
This commit is contained in:
Stanislav Hejny
2025-07-24 08:08:00 +01:00
parent 2b20c9a0dc
commit ad49cb2836
+73 -92
View File
@@ -1,20 +1,16 @@
import importlib import importlib
import asyncio import asyncio
import sys import sys
import unittest
from amqp.service.amq_message_handler import DataMessageHandler from amqp.service.amq_message_handler import DataMessageHandler
# Create a dummy package and module for demonstration class TestRPCInvocation(unittest.IsolatedAsyncioTestCase):
# You would typically have these in separate files/directories """Test cases for the RPC invocation functionality in DataMessageHandler."""
# e.g., my_package/
# __init__.py
# my_module.py
# For this self-contained example, we'll create them dynamically in memory async def asyncSetUp(self):
# In a real application, 'my_package' would be a proper installed package or in sys.path """Set up the test environment with a dummy module."""
# Define a simple module string
# --- Dummy Package/Module Setup --- dummy_module_content = """
# Define a simple module string
dummy_module_content = """
import asyncio import asyncio
# Global function # Global function
@@ -22,7 +18,7 @@ def greet_sync(name: str) -> str:
return f"Hello, {name} (sync global)!" return f"Hello, {name} (sync global)!"
async def greet_async(name: str) -> str: async def greet_async(name: str) -> str:
await asyncio.sleep(0.05) # Simulate async work await asyncio.sleep(0.01) # Simulate async work
return f"Hello, {name} (async global)!" return f"Hello, {name} (async global)!"
# A simple class # A simple class
@@ -40,121 +36,106 @@ class MyService:
def _private_method(self): def _private_method(self):
return "This is a private method." return "This is a private method."
""" """
# Dynamically create a module object
spec = importlib.util.spec_from_loader("my_package.my_module", loader=None)
if spec is None:
raise ImportError("Could not create module spec for my_package.my_module")
# Dynamically create a module object self.my_module = importlib.util.module_from_spec(spec)
spec = importlib.util.spec_from_loader("my_package.my_module", loader=None) exec(dummy_module_content, self.my_module.__dict__)
if spec is None: sys.modules["my_package"] = self.my_module # Add to sys.modules for importlib to find
raise ImportError("Could not create module spec for my_package.my_module")
my_module = importlib.util.module_from_spec(spec) async def asyncTearDown(self):
exec(dummy_module_content, my_module.__dict__) """Clean up after tests."""
sys.modules["my_package"] = my_module # Add to sys.modules for importlib to find # Remove the dummy module from sys.modules
if "my_package" in sys.modules:
del sys.modules["my_package"]
# --- Running the Demos --- async def test_invoke_global_sync_function(self):
async def main(): """Test invoking a global synchronous function."""
print("\n--- Demo: Invoking Global Synchronous Function ---")
try:
result = await DataMessageHandler.invoke_command( result = await DataMessageHandler.invoke_command(
package_name="my_package.my_module", package_name="my_package.my_module",
class_name="__global__", class_name="__global__",
function_name="greet_sync", function_name="greet_sync",
kwargs={"name": "Alice"} kwargs={"name": "Alice"}
) )
print(f"Demo Result: {result}\n") self.assertEqual(result, "Hello, Alice (sync global)!")
except Exception as e:
print(f"Demo Failed: {e}\n")
print("\n--- Demo: Invoking Global Asynchronous Function ---") async def test_invoke_global_async_function(self):
try: """Test invoking a global asynchronous function."""
result = await DataMessageHandler.invoke_command( result = await DataMessageHandler.invoke_command(
package_name="my_package.my_module", package_name="my_package.my_module",
class_name="__global__", class_name="__global__",
function_name="greet_async", function_name="greet_async",
kwargs={"name": "Bob"} kwargs={"name": "Bob"}
) )
print(f"Demo Result: {result}\n") self.assertEqual(result, "Hello, Bob (async global)!")
except Exception as e:
print(f"Demo Failed: {e}\n")
print("\n--- Demo: Invoking Class Synchronous Method ---") async def test_invoke_class_sync_method(self):
try: """Test invoking a class synchronous method."""
result = await DataMessageHandler.invoke_command( result = await DataMessageHandler.invoke_command(
package_name="my_package.my_module", package_name="my_package.my_module",
class_name="MyService", class_name="MyService",
function_name="process_data_sync", function_name="process_data_sync",
kwargs={"data": "report", "count": 3} kwargs={"data": "report", "count": 3}
) )
print(f"Demo Result: {result}\n") self.assertEqual(
except Exception as e: result,
print(f"Demo Failed: {e}\n") "Service 'MyAwesomeService' processed 'report' 3 times (sync method)."
)
print("\n--- Demo: Invoking Class Asynchronous Method ---") async def test_invoke_class_async_method(self):
try: """Test invoking a class asynchronous method."""
result = await DataMessageHandler.invoke_command( result = await DataMessageHandler.invoke_command(
package_name="my_package.my_module", package_name="my_package.my_module",
class_name="MyService", class_name="MyService",
function_name="process_data_async", function_name="process_data_async",
kwargs={"data": "metrics", "delay": 0.1} kwargs={"data": "metrics", "delay": 0.01}
) )
print(f"Demo Result: {result}\n") self.assertEqual(
except Exception as e: result,
print(f"Demo Failed: {e}\n") "Service 'MyAwesomeService' processed 'metrics' after 0.01s (async method)."
print("\n--- Demo: Error - Package Not Found ---")
try:
await DataMessageHandler.invoke_command(
package_name="non_existent_package",
class_name="__global__",
function_name="some_func",
kwargs={}
) )
except ModuleNotFoundError:
print("Demo Handled: ModuleNotFoundError as expected.\n")
except Exception as e:
print(f"Demo Failed Unexpectedly: {e}\n")
print("\n--- Demo: Error - Class Not Found ---") async def test_package_not_found(self):
try: """Test error handling when package is not found."""
await DataMessageHandler.invoke_command( with self.assertRaises(ModuleNotFoundError):
package_name="my_package.my_module", await DataMessageHandler.invoke_command(
class_name="NonExistentClass", package_name="non_existent_package",
function_name="some_method", class_name="__global__",
kwargs={} function_name="some_func",
) kwargs={}
except AttributeError: )
print("Demo Handled: AttributeError (Class Not Found) as expected.\n")
except Exception as e:
print(f"Demo Failed Unexpectedly: {e}\n")
print("\n--- Demo: Error - Function/Method Not Found ---") async def test_class_not_found(self):
try: """Test error handling when class is not found."""
await DataMessageHandler.invoke_command( with self.assertRaises(AttributeError):
await DataMessageHandler.invoke_command(
package_name="my_package.my_module",
class_name="NonExistentClass",
function_name="some_method",
kwargs={}
)
async def test_method_not_found(self):
"""Test error handling when method is not found."""
with self.assertRaises(AttributeError):
await DataMessageHandler.invoke_command(
package_name="my_package.my_module",
class_name="MyService",
function_name="non_existent_method",
kwargs={}
)
async def test_private_method_access(self):
"""Test that private methods can be accessed (Python doesn't enforce privacy)."""
result = await DataMessageHandler.invoke_command(
package_name="my_package.my_module", package_name="my_package.my_module",
class_name="MyService", class_name="MyService",
function_name="non_existent_method", function_name="_private_method",
kwargs={} kwargs={}
) )
except AttributeError: self.assertEqual(result, "This is a private method.")
print("Demo Handled: AttributeError (Function/Method Not Found) as expected.\n")
except Exception as e:
print(f"Demo Failed Unexpectedly: {e}\n")
print("\n--- Demo: Error - Private Method (AttributeError) ---")
try:
await DataMessageHandler.invoke_command(
package_name="my_package.my_module",
class_name="MyService",
function_name="_private_method", # Python convention, but still accessible via getattr
kwargs={}
)
print("Demo Result: Successfully called _private_method (Python allows this via getattr).\n")
except Exception as e:
print(f"Demo Failed: {e}\n")
if __name__ == "__main__": if __name__ == "__main__":
import sys unittest.main()
import importlib.util
# Run the main asynchronous demo
asyncio.run(main())