Skip to content

Commit

Permalink
update test 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 29, 2024
1 parent a757a6e commit 8bb993f
Showing 1 changed file with 21 additions and 51 deletions.
72 changes: 21 additions & 51 deletions tests/memory_statistics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,9 @@ def test_validate_socket_path_non_existent_directory(self, mock_syslog):
socket_manager._validate_socket_path()

assert "Socket directory /non/existent/path does not exist" in str(excinfo.value)
mock_syslog.assert_called_once_with(syslog.LOG_ERR,
"Socket directory /non/existent/path does not exist")
mock_syslog.assert_called_once_with(
syslog.LOG_ERR, "Socket directory /non/existent/path does not exist"
)

@patch('socket.socket')
@patch('time.sleep')
Expand All @@ -662,7 +663,7 @@ def test_connect_max_retries_exceeded(self, mock_syslog, mock_sleep, mock_socket
mock_socket.side_effect = socket.error("Connection failed")

with patch.object(Config, 'MAX_RETRIES', 3), \
patch.object(Config, 'RETRY_DELAY', 0.1):
patch.object(Config, 'RETRY_DELAY', 0.1):
with pytest.raises(ConnectionError) as excinfo:
socket_manager = SocketManager()
socket_manager.connect()
Expand All @@ -689,7 +690,7 @@ def test_receive_all_socket_timeout(self, mock_socket):
mock_socket_instance.recv.side_effect = socket.timeout()

with patch.object(Config, 'SOCKET_TIMEOUT', 5), \
patch.object(Config, 'BUFFER_SIZE', 1024):
patch.object(Config, 'BUFFER_SIZE', 1024):
with pytest.raises(ConnectionError) as excinfo:
socket_manager = SocketManager()
socket_manager.sock = mock_socket_instance
Expand Down Expand Up @@ -720,51 +721,36 @@ def test_send_socket_error(self, mock_socket):

assert "Failed to send data: Send failed" in str(excinfo.value)


def test_send_data_no_response():
def test_send_data_no_response(self):
"""Test send_data function when no response is received."""
with patch('socket.socket'), \
patch.object(SocketManager, 'receive_all', return_value=''):
patch.object(SocketManager, 'receive_all', return_value=''):
with pytest.raises(ConnectionError) as excinfo:
send_data("test_command", {"key": "value"})

assert "No response received from memory statistics service" in str(excinfo.value)


def test_send_data_json_decode_error():
def test_send_data_json_decode_error(self):
"""Test send_data function with invalid JSON response."""
with patch('socket.socket'), \
patch.object(SocketManager, 'receive_all', return_value='invalid json'):
patch.object(SocketManager, 'receive_all', return_value='invalid json'):
with pytest.raises(ValueError) as excinfo:
send_data("test_command", {"key": "value"})

assert "Failed to parse server response" in str(excinfo.value)


# def test_send_data_invalid_response_format():
# """Test send_data function with invalid response format."""
# with patch('socket.socket'), \
# patch.object(SocketManager, 'receive_all', return_value='[1, 2, 3]'):
# with pytest.raises(ValueError) as excinfo:
# send_data(
# "test_command", {"key": "value"}
# )

# assert "Invalid response format from server" in str(excinfo.value)


def test_send_data_server_error_response():
def test_send_data_server_error_response(self):
"""Test send_data function with server error response."""
error_response = json.dumps({"status": False, "msg": "Server error"})
with patch('socket.socket'), \
patch.object(SocketManager, 'receive_all', return_value=error_response):
patch.object(SocketManager, 'receive_all', return_value=error_response):
with pytest.raises(RuntimeError) as excinfo:
send_data("test_command", {"key": "value"})

assert "Server error" in str(excinfo.value)

@patch('click.echo')
def test_display_config_exception(mock_echo):
def test_display_config_exception(self, mock_echo):
"""Test display_config function with database connection error."""
mock_db_connector = Mock()
mock_db_connector.get_memory_statistics_config.side_effect = Exception("DB Error")
Expand All @@ -774,45 +760,29 @@ def test_display_config_exception(mock_echo):
display_config(mock_db_connector)

assert "Failed to retrieve configuration: DB Error" in str(excinfo.value)
mock_syslog.assert_called_once_with(syslog.LOG_ERR,
"Failed to retrieve configuration: DB Error")

# def test_main_invalid_command():
# """Test main function with an invalid command."""
# with patch('sys.argv', ['script.py', 'invalid_command']), \
# patch('syslog.syslog') as mock_syslog:
# with pytest.raises(click.UsageError) as excinfo:
# main()

# assert "Error: Invalid command 'invalid_command'" in str(excinfo.value)
# mock_syslog.assert_called_once_with(
# syslog.LOG_ERR,
# "Error: Invalid command 'invalid_command'."
# )

mock_syslog.assert_called_once_with(
syslog.LOG_ERR, "Failed to retrieve configuration: DB Error"
)

def test_send_data_invalid_response_format():
def test_send_data_invalid_response_format(self):
"""Test send_data function with invalid response format."""
with patch('socket.socket'), \
patch.object(SocketManager, 'receive_all', return_value='[1, 2, 3]'):
patch.object(SocketManager, 'receive_all', return_value='[1, 2, 3]'):
with pytest.raises(ValueError) as excinfo:
send_data(
"test_command", {"key": "value"}
)
send_data("test_command", {"key": "value"})

assert "Invalid response format from server" in str(excinfo.value)

def test_main_invalid_command():
def test_main_invalid_command(self):
"""Test main function with an invalid command."""
with patch('sys.argv', ['script.py', 'invalid_command']), \
patch('syslog.syslog') as mock_syslog:
patch('syslog.syslog') as mock_syslog:
with pytest.raises(click.UsageError) as excinfo:
main()

assert "Error: Invalid command 'invalid_command'" in str(excinfo.value)
mock_syslog.assert_called_once_with(
syslog.LOG_ERR,
"Error: Invalid command 'invalid_command'."
syslog.LOG_ERR, "Error: Invalid command 'invalid_command'."
)


Expand Down

0 comments on commit 8bb993f

Please sign in to comment.