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 9b93d0f commit 9e73197
Showing 1 changed file with 60 additions and 63 deletions.
123 changes: 60 additions & 63 deletions tests/show_memory_statistics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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()
Expand Down

0 comments on commit 9e73197

Please sign in to comment.