SIENTIAPDE-1318
Refactor project structure and update configurations - Deleted the empty `__init__.py` file to clean up the project structure. - Renamed the project from "laborious" to "ingestor" in `pyproject.toml`, updating the description accordingly. - Improved type hints and conditional checks in the `Ingestor`, `DataManager`, `IngestorManager`, and `OpcManager` classes for better code clarity and type safety. - Enhanced error handling and assertions in various methods to ensure robustness. - Updated unit tests to reflect changes in class names and error handling improvements.
This commit is contained in:
@@ -135,7 +135,7 @@ def test___init___failure_max_attempts(mongo, kafka):
|
||||
assert logger_mock.info.call_count == 3
|
||||
|
||||
else:
|
||||
assert False, 'Expected NoBrokersAvailable exception was not raised.'
|
||||
raise AssertionError('Expected NoBrokersAvailable exception was not raised.')
|
||||
|
||||
|
||||
def test_shutdown_has_producer(data_manager):
|
||||
|
||||
@@ -127,7 +127,7 @@ async def test_set_security_no_cert(opc_manager):
|
||||
except ValueError as e:
|
||||
assert str(e) == 'Certificate and private key paths must be provided for secure connection.'
|
||||
else:
|
||||
assert False, 'ValueError not raised'
|
||||
raise AssertionError('ValueError not raised')
|
||||
|
||||
assert opc_manager.client.set_security.call_count == 0
|
||||
|
||||
@@ -218,7 +218,7 @@ async def test_create_subscription_no_client(raw_opc_manager):
|
||||
except ValueError as e:
|
||||
assert str(e) == 'Client not connected. Call connect first.'
|
||||
else:
|
||||
assert False, 'ValueError not raised'
|
||||
raise AssertionError('ValueError not raised')
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
@@ -284,7 +284,7 @@ async def test_subscribe_no_subscription(metrics, opc_manager):
|
||||
except ValueError as e:
|
||||
assert str(e) == 'Subscription not created. Call create_subscription first.'
|
||||
else:
|
||||
assert False, 'ValueError not raised'
|
||||
raise AssertionError('ValueError not raised')
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels.assert_not_called()
|
||||
|
||||
|
||||
@@ -315,7 +315,7 @@ async def test_unsubscribe_no_subscription(opc_manager):
|
||||
async def test_unsubscribe_success(opc_manager_subscribed):
|
||||
await opc_manager_subscribed.unsubscribe('sub1')
|
||||
|
||||
opc_manager_subscribed.subscriptions.get('sub1') is None
|
||||
assert opc_manager_subscribed.subscriptions.get('sub1') is None
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
|
||||
@@ -10,7 +10,7 @@ from ingestor import app
|
||||
|
||||
|
||||
# Custom exception to catch os._exit calls
|
||||
class OsExitCalled(Exception):
|
||||
class OsExitCalledError(Exception):
|
||||
def __init__(self, code):
|
||||
super().__init__(f'os._exit({code}) called')
|
||||
self.code = code
|
||||
@@ -18,7 +18,7 @@ class OsExitCalled(Exception):
|
||||
|
||||
# Helper function for the os_exit mock's side_effect
|
||||
def raise_os_exit_with_code(exit_code):
|
||||
raise OsExitCalled(exit_code)
|
||||
raise OsExitCalledError(exit_code)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -95,7 +95,7 @@ async def test_main_successful_run_one_loop(mock_app_env, capsys):
|
||||
mock_exit_signal.is_set.side_effect = [False, True]
|
||||
mock_app_env['time_time'].side_effect = [10.0, 11.5]
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
with pytest.raises(OsExitCalledError) as excinfo:
|
||||
await app.main()
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
@@ -133,7 +133,7 @@ async def test_main_prometheus_server_fails_to_start(mock_app_env, capsys):
|
||||
"""Test the scenario where starting the Prometheus server fails."""
|
||||
mock_app_env['start_http_server'].side_effect = OSError('Port already in use')
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
with pytest.raises(OsExitCalledError) as excinfo:
|
||||
await app.main()
|
||||
assert excinfo.value.code == 1
|
||||
|
||||
@@ -160,7 +160,7 @@ async def test_main_loop_exception_handling(mock_app_env, capsys):
|
||||
mock_ingestor_instance.loop.side_effect = Exception('Test loop exception')
|
||||
mock_app_env['time_time'].side_effect = [10.0, 10.1]
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
with pytest.raises(OsExitCalledError) as excinfo:
|
||||
await app.main()
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
@@ -194,7 +194,7 @@ async def test_main_keyboard_interrupt_handling(mock_app_env, capsys):
|
||||
mock_ingestor_instance.loop.side_effect = KeyboardInterrupt()
|
||||
mock_app_env['time_time'].side_effect = [10.0, 10.1]
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
with pytest.raises(OsExitCalledError) as excinfo:
|
||||
await app.main()
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
@@ -223,7 +223,7 @@ async def test_main_multiple_loop_iterations(mock_app_env):
|
||||
mock_exit_signal.is_set.side_effect = [False, False, False, True]
|
||||
mock_app_env['time_time'].side_effect = [10.0, 10.1, 10.2, 10.3, 10.4, 10.5]
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
with pytest.raises(OsExitCalledError) as excinfo:
|
||||
await app.main()
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
@@ -254,7 +254,7 @@ async def test_main_pod_id_used_in_metrics(mock_app_env):
|
||||
mock_exit_signal.is_set.side_effect = [False, True]
|
||||
mock_app_env['time_time'].side_effect = [10.0, 11.0]
|
||||
|
||||
with pytest.raises(OsExitCalled):
|
||||
with pytest.raises(OsExitCalledError):
|
||||
await app.main()
|
||||
|
||||
app.metrics.APP_UP.labels.assert_any_call(pod_id='test_pod')
|
||||
@@ -279,7 +279,7 @@ async def test_run_async_main(mock_app_env, capsys):
|
||||
|
||||
# Instead of calling run_async_main() which creates a new event loop,
|
||||
# we test the main() function directly since that's what run_async_main() would call
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
with pytest.raises(OsExitCalledError) as excinfo:
|
||||
await app.main()
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
@@ -299,7 +299,7 @@ async def test_main_prepare_ingestor_failure(mock_app_env, capsys):
|
||||
mock_ingestor_instance.prepare_ingestor.side_effect = Exception('Preparation failed')
|
||||
mock_exit_signal.is_set.side_effect = [False, True]
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
with pytest.raises(OsExitCalledError) as excinfo:
|
||||
await app.main()
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ async def test_prepare_ingestor(ingestor_manager_mock, ingestor):
|
||||
await ingestor.prepare_ingestor()
|
||||
|
||||
ingestor_manager_mock.assert_called_once_with(
|
||||
kafka_servers=ingestor.kafka_servers,
|
||||
kafka_servers=','.join(ingestor.kafka_servers),
|
||||
redis_data={
|
||||
'host': ingestor.redis_host,
|
||||
'port': ingestor.redis_port,
|
||||
|
||||
Reference in New Issue
Block a user