Skip to content

Commit

Permalink
test cov for MemoryManager.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lane-neuro committed Jul 28, 2024
1 parent 31ad4fc commit f4fa47c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
5 changes: 3 additions & 2 deletions research_analytics_suite/data_engine/memory/MemoryManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ async def delete_slot(self, memory_id: str) -> None:
memory_slot = self._data_cache.get_key(key=memory_id)
if memory_slot:
memory_slot.close()
os.remove(memory_slot._file_path) # Remove memory-mapped file if it exists
self._data_cache.delete(key=memory_id)
if memory_slot._file_path:
os.remove(memory_slot._file_path) # Remove memory-mapped file if it exists
self._data_cache.delete(key=memory_id)

async def list_slots(self) -> dict:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,11 @@ async def get_results(self) -> dict:
Returns:
dict: The name of the variable and its value.
"""
from research_analytics_suite.data_engine.memory.MemoryManager import MemoryManager
memory_manager = MemoryManager()

_results = {}
for slot_id in self.memory_outputs:
_name = await memory_manager.slot_name(slot_id)
_results[_name] = await memory_manager.slot_data(slot_id)
return _results
results = {}
for memory_slot in self.memory_outputs:
if memory_slot.data is not None:
results[memory_slot.name] = await self.get_slot_data(memory_slot)
return results

@command
async def clear_inputs(self):
Expand Down
50 changes: 48 additions & 2 deletions tests/data_engine/memory/test_memory_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,57 @@ async def test_initialize(self):
await self.memory_manager.initialize()
assert self.memory_manager._initialized is True

@pytest.mark.asyncio
async def test_initialize_twice(self):
await self.memory_manager.initialize()
assert self.memory_manager._initialized is True

# Initialize again and check that it doesn't reinitialize
await self.memory_manager.initialize()
assert self.memory_manager._initialized is True # Should still be true without reinitialization

@pytest.mark.asyncio
async def test_update_slot_with_non_existing_id(self):
memory_id = "nonexistingid"
self.memory_manager._data_cache.get_key.return_value = None

with patch.object(MemorySlot, 'setup', new_callable=AsyncMock):
with patch.object(MemorySlot, 'set_data', new_callable=AsyncMock):
updated_memory_id = await self.memory_manager.update_slot(memory_id=memory_id, data="new_value")

self.memory_manager._data_cache.set.assert_called_once()
assert updated_memory_id == memory_id

@pytest.mark.asyncio
async def test_delete_non_existing_slot(self):
memory_id = "nonexistingid"
self.memory_manager._data_cache.get_key.return_value = None

# Should not raise any exception
await self.memory_manager.delete_slot(memory_id=memory_id)

# Verify delete was not called because the slot doesn't exist
self.memory_manager._data_cache.delete.assert_not_called()

@pytest.mark.asyncio
async def test_slot_data_retrieval_error(self):
memory_id = "nonexistingid"
self.memory_manager._data_cache.get_key.return_value = None

data = await self.memory_manager.slot_data(memory_id=memory_id)
assert data is None

@pytest.mark.asyncio
async def test_validate_slots_with_empty_list(self):
memory_ids = []
valid_slots, invalid_slots = await self.memory_manager.validate_slots(memory_ids=memory_ids)

assert valid_slots == []
assert invalid_slots == []

@pytest.mark.asyncio
async def test_create_slot(self):
memory_id = await self.memory_manager.create_slot(name="test_slot", data="value", db_path=":memory:")

self.memory_manager._data_cache.set.assert_called_once()
assert len(memory_id) == 8 # UUID truncated to 8 characters

Expand Down Expand Up @@ -112,5 +159,4 @@ async def test_validate_slots(self):
@pytest.mark.asyncio
async def test_cleanup(self):
await self.memory_manager.cleanup()

self.memory_manager._data_cache.close.assert_called_once()

0 comments on commit f4fa47c

Please sign in to comment.