feat/#63 - fix the send_command and add unit tests
Unit test coverage / pytest (pull_request) Successful in 1m15s
Unit test coverage / pytest (push) Failing after 1m50s
CI for pypl publish / publish-lib (push) Successful in 1m57s
/ build-and-push (push) Successful in 1m59s
Build and Publish Docker Image / build-and-push (push) Successful in 2m12s

This commit is contained in:
Stanislav Hejny
2025-07-24 10:05:36 +01:00
parent 4985344e5e
commit 9491517696
4 changed files with 201 additions and 20 deletions
+20 -16
View File
@@ -1,6 +1,7 @@
import importlib
import sys
import unittest
from amqp.service.amq_message_handler import invoke_command
@@ -43,7 +44,9 @@ class MyService:
self.my_module = importlib.util.module_from_spec(spec)
exec(dummy_module_content, self.my_module.__dict__)
sys.modules["my_package"] = self.my_module # Add to sys.modules for importlib to find
sys.modules["my_package.my_module"] = (
self.my_module
) # Add to sys.modules for importlib to find
async def asyncTearDown(self):
"""Clean up after tests."""
@@ -57,9 +60,9 @@ class MyService:
package_name="my_package.my_module",
class_name="__global__",
function_name="greet_sync",
kwargs={"name": "Alice"}
kwargs={"name": "Alice"},
)
self.assertEqual(result, "Hello, Alice (sync global)!")
self.assertEqual(result.body().decode(), "Hello, Alice (sync global)!")
async def test_invoke_global_async_function(self):
"""Test invoking a global asynchronous function."""
@@ -67,9 +70,9 @@ class MyService:
package_name="my_package.my_module",
class_name="__global__",
function_name="greet_async",
kwargs={"name": "Bob"}
kwargs={"name": "Bob"},
)
self.assertEqual(result, "Hello, Bob (async global)!")
self.assertEqual(result.body().decode(), "Hello, Bob (async global)!")
async def test_invoke_class_sync_method(self):
"""Test invoking a class synchronous method."""
@@ -77,11 +80,11 @@ class MyService:
package_name="my_package.my_module",
class_name="MyService",
function_name="process_data_sync",
kwargs={"data": "report", "count": 3}
kwargs={"data": "report", "count": 3},
)
self.assertEqual(
result,
"Service 'MyAwesomeService' processed 'report' 3 times (sync method)."
result.body().decode(),
"Service 'MyAwesomeService' processed 'report' 3 times (sync method).",
)
async def test_invoke_class_async_method(self):
@@ -90,11 +93,11 @@ class MyService:
package_name="my_package.my_module",
class_name="MyService",
function_name="process_data_async",
kwargs={"data": "metrics", "delay": 0.01}
kwargs={"data": "metrics", "delay": 0.01},
)
self.assertEqual(
result,
"Service 'MyAwesomeService' processed 'metrics' after 0.01s (async method)."
result.body().decode(),
"Service 'MyAwesomeService' processed 'metrics' after 0.01s (async method).",
)
async def test_package_not_found(self):
@@ -104,7 +107,7 @@ class MyService:
package_name="non_existent_package",
class_name="__global__",
function_name="some_func",
kwargs={}
kwargs={},
)
async def test_class_not_found(self):
@@ -114,7 +117,7 @@ class MyService:
package_name="my_package.my_module",
class_name="NonExistentClass",
function_name="some_method",
kwargs={}
kwargs={},
)
async def test_method_not_found(self):
@@ -124,7 +127,7 @@ class MyService:
package_name="my_package.my_module",
class_name="MyService",
function_name="non_existent_method",
kwargs={}
kwargs={},
)
async def test_private_method_access(self):
@@ -133,9 +136,10 @@ class MyService:
package_name="my_package.my_module",
class_name="MyService",
function_name="_private_method",
kwargs={}
kwargs={},
)
self.assertEqual(result, "This is a private method.")
self.assertEqual(result.body().decode(), "This is a private method.")
if __name__ == "__main__":
unittest.main()