Skip to content

Commit

Permalink
update the code file
Browse files Browse the repository at this point in the history
Signed-off-by: Arham-Nasir <[email protected]>
  • Loading branch information
Arham-Nasir committed Nov 24, 2024
1 parent 9e73197 commit c97adc6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tests/show_memory_statistics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
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
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 = {
Expand All @@ -197,6 +197,7 @@
}
}


@pytest.fixture
def mock_config_db():
"""Mock SONiC ConfigDB connection and data retrieval."""
Expand All @@ -206,6 +207,7 @@ def mock_config_db():
mock_instance.get_table.return_value = MEMORY_STATS_CONFIG
yield mock_instance


@pytest.fixture
def mock_socket_manager():
"""Mock SocketManager for socket communication."""
Expand All @@ -221,7 +223,7 @@ def mock_socket_manager():
# def test_show_memory_stats_config(mock_config_db):
# runner = CliRunner()
# result = runner.invoke(cli, ['show', 'memory-stats', '--config'])

# assert result.exit_code == 0
# assert "Enabled: true" in result.output # Ensure correct enabled value
# assert "Retention Time (days): 15" in result.output
Expand All @@ -235,12 +237,14 @@ def mock_socket_manager():
# 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_invalid_command():
"""Test CLI with an invalid command."""
runner = CliRunner()
Expand All @@ -249,13 +253,15 @@ def test_invalid_command():
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 = ""

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"
Expand Down

0 comments on commit c97adc6

Please sign in to comment.