From 9e7319719fe8ae16d69527bceef7257515a7d3f1 Mon Sep 17 00:00:00 2001 From: Arham-Nasir Date: Sun, 24 Nov 2024 15:20:01 +0500 Subject: [PATCH] update the code file Signed-off-by: Arham-Nasir --- tests/show_memory_statistics_test.py | 123 +++++++++++++-------------- 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/tests/show_memory_statistics_test.py b/tests/show_memory_statistics_test.py index 4d6e3db148..270fa80e38 100644 --- a/tests/show_memory_statistics_test.py +++ b/tests/show_memory_statistics_test.py @@ -182,53 +182,50 @@ # pytest.main([__file__]) -# import pytest -# import json -# from unittest.mock import patch -# from click.testing import CliRunner -# from show.memory_statistics import cli,send_data # Replace 'my_cli_tool' with your actual module name - -# # Mock configuration data for tests -# MEMORY_STATS_CONFIG = { -# "memory_statistics": { -# "enabled": "true", -# "sampling_interval": "5", -# "retention_period": "15" -# } -# } - -# @pytest.fixture -# def mock_config_db(): -# """Mock SONiC ConfigDB connection and data retrieval.""" -# with patch('my_cli_tool.ConfigDBConnector') as mock_connector: -# mock_instance = mock_connector.return_value -# mock_instance.connect.return_value = None -# mock_instance.get_table.return_value = MEMORY_STATS_CONFIG -# yield mock_instance - -# @pytest.fixture -# def mock_socket_manager(): -# """Mock SocketManager for socket communication.""" -# with patch('my_cli_tool.SocketManager') as mock_socket: -# mock_instance = mock_socket.return_value -# mock_instance.connect.return_value = None -# mock_instance.receive_all.return_value = json.dumps({ -# "status": True, -# "data": "Sample memory data output" -# }) -# yield mock_instance +import pytest +import json +from unittest.mock import patch +from click.testing import CliRunner +from show.memory_statistics import cli,send_data # Replace 'my_cli_tool' with your actual module name + +# Mock configuration data for tests +MEMORY_STATS_CONFIG = { + "memory_statistics": { + "enabled": "true", + "sampling_interval": "5", + "retention_period": "15" + } +} + +@pytest.fixture +def mock_config_db(): + """Mock SONiC ConfigDB connection and data retrieval.""" + with patch('my_cli_tool.ConfigDBConnector') as mock_connector: + mock_instance = mock_connector.return_value + mock_instance.connect.return_value = None + mock_instance.get_table.return_value = MEMORY_STATS_CONFIG + yield mock_instance + +@pytest.fixture +def mock_socket_manager(): + """Mock SocketManager for socket communication.""" + with patch('my_cli_tool.SocketManager') as mock_socket: + mock_instance = mock_socket.return_value + mock_instance.connect.return_value = None + mock_instance.receive_all.return_value = json.dumps({ + "status": True, + "data": "Sample memory data output" + }) + yield mock_instance # def test_show_memory_stats_config(mock_config_db): -# """Test the 'show memory-stats --config' command.""" # runner = CliRunner() # result = runner.invoke(cli, ['show', 'memory-stats', '--config']) - + # assert result.exit_code == 0 -# assert "Enabled" in result.output -# assert "Retention Time (days)" in result.output -# assert "Sampling Interval (minutes)" in result.output -# assert "15" in result.output # Check retention period value -# assert "5" in result.output # Check sampling interval value +# assert "Enabled: true" in result.output # Ensure correct enabled value +# assert "Retention Time (days): 15" in result.output +# assert "Sampling Interval (minutes): 5" in result.output # def test_send_data_success(mock_socket_manager): # """Test successful socket communication with valid response.""" @@ -238,33 +235,33 @@ # assert response.status == True # assert response.data == "Sample memory data output" -# def test_socket_connection_failure(): -# """Test socket connection failure handling.""" -# with patch('my_cli_tool.SocketManager.connect', side_effect=ConnectionError("Connection failed")): -# with pytest.raises(ConnectionError): -# send_data("memory_statistics_command_request_handler", {}) +def test_socket_connection_failure(): + """Test socket connection failure handling.""" + with patch('my_cli_tool.SocketManager.connect', side_effect=ConnectionError("Connection failed")): + with pytest.raises(ConnectionError): + send_data("memory_statistics_command_request_handler", {}) -# def test_invalid_command(): -# """Test CLI with an invalid command.""" -# runner = CliRunner() -# result = runner.invoke(cli, ['invalid-command']) +def test_invalid_command(): + """Test CLI with an invalid command.""" + runner = CliRunner() + result = runner.invoke(cli, ['invalid-command']) -# assert result.exit_code != 0 -# assert "Error: Invalid command" in result.output + assert result.exit_code != 0 + assert "Error: Invalid command" in result.output -# def test_missing_socket_response(mock_socket_manager): -# """Test handling of empty socket responses.""" -# mock_socket_manager.receive_all.return_value = "" +def test_missing_socket_response(mock_socket_manager): + """Test handling of empty socket responses.""" + mock_socket_manager.receive_all.return_value = "" -# with pytest.raises(ConnectionError, match="No response received from memory statistics service"): -# send_data("memory_statistics_command_request_handler", {}) + with pytest.raises(ConnectionError, match="No response received from memory statistics service"): + send_data("memory_statistics_command_request_handler", {}) -# def test_invalid_json_response(mock_socket_manager): -# """Test handling of invalid JSON responses from socket.""" -# mock_socket_manager.receive_all.return_value = "Invalid JSON" +def test_invalid_json_response(mock_socket_manager): + """Test handling of invalid JSON responses from socket.""" + mock_socket_manager.receive_all.return_value = "Invalid JSON" -# with pytest.raises(ValueError, match="Failed to parse server response"): -# send_data("memory_statistics_command_request_handler", {}) + with pytest.raises(ValueError, match="Failed to parse server response"): + send_data("memory_statistics_command_request_handler", {}) # def test_cli_show_memory_stats(mock_socket_manager): # """Test the 'show memory-stats' CLI command for statistics retrieval.""" # runner = CliRunner()