CoolFace
Apppublic

coding-alt/AutoGPT

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
local_cache_test.py68 linesDownload Raw Back to tests
1# sourcery skip: snake-case-functions2"""Tests for LocalCache class"""3import os4import sys5import unittest6 7import pytest8 9from autogpt.memory.local import LocalCache10 11 12def mock_config() -> dict:13    """Mock the Config class"""14    return type(15        "MockConfig",16        (object,),17        {18            "debug_mode": False,19            "continuous_mode": False,20            "speak_mode": False,21            "memory_index": "auto-gpt",22        },23    )24 25 26@pytest.mark.integration_test27class TestLocalCache(unittest.TestCase):28    """Tests for LocalCache class"""29 30    def setUp(self) -> None:31        """Set up the test environment"""32        self.cfg = mock_config()33        self.cache = LocalCache(self.cfg)34 35    def test_add(self) -> None:36        """Test adding a text to the cache"""37        text = "Sample text"38        self.cache.add(text)39        self.assertIn(text, self.cache.data.texts)40 41    def test_clear(self) -> None:42        """Test clearing the cache"""43        self.cache.clear()44        self.assertEqual(self.cache.data.texts, [])45 46    def test_get(self) -> None:47        """Test getting a text from the cache"""48        text = "Sample text"49        self.cache.add(text)50        result = self.cache.get(text)51        self.assertEqual(result, [text])52 53    def test_get_relevant(self) -> None:54        """Test getting relevant texts from the cache"""55        text1 = "Sample text 1"56        text2 = "Sample text 2"57        self.cache.add(text1)58        self.cache.add(text2)59        result = self.cache.get_relevant(text1, 1)60        self.assertEqual(result, [text1])61 62    def test_get_stats(self) -> None:63        """Test getting the cache stats"""64        text = "Sample text"65        self.cache.add(text)66        stats = self.cache.get_stats()67        self.assertEqual(stats, (4, self.cache.data.embeddings.shape))68