Skip to content

Commit

Permalink
Add unit tests for MemoryCondenser in test_condenser.py (#3379)
Browse files Browse the repository at this point in the history
* Add unit tests for MemoryCondenser in test_condenser.py

* Formatting

* Fix formatting etc

---------

Co-authored-by: opendevin <[email protected]>
  • Loading branch information
neubig and openhands-agent committed Aug 14, 2024
1 parent e33b356 commit 92b19ed
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/unit/test_condenser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from unittest.mock import Mock, patch

import pytest

from opendevin.core.exceptions import LLMResponseError
from opendevin.llm.llm import LLM
from opendevin.memory.condenser import MemoryCondenser


@pytest.fixture
def memory_condenser():
return MemoryCondenser()


@pytest.fixture
def mock_llm():
return Mock(spec=LLM)


def test_condense_success(memory_condenser, mock_llm):
mock_llm.completion.return_value = {
'choices': [{'message': {'content': 'Condensed memory'}}]
}
result = memory_condenser.condense('Summarize this', mock_llm)
assert result == 'Condensed memory'
mock_llm.completion.assert_called_once_with(
messages=[{'content': 'Summarize this', 'role': 'user'}]
)


def test_condense_exception(memory_condenser, mock_llm):
mock_llm.completion.side_effect = LLMResponseError('LLM error')
with pytest.raises(LLMResponseError, match='LLM error'):
memory_condenser.condense('Summarize this', mock_llm)


@patch('opendevin.memory.condenser.logger')
def test_condense_logs_error(mock_logger, memory_condenser, mock_llm):
mock_llm.completion.side_effect = LLMResponseError('LLM error')
with pytest.raises(LLMResponseError):
memory_condenser.condense('Summarize this', mock_llm)
mock_logger.error.assert_called_once_with(
'Error condensing thoughts: %s', 'LLM error', exc_info=False
)

0 comments on commit 92b19ed

Please sign in to comment.