Skip to content

Commit

Permalink
minimall add pytest (#1859)
Browse files Browse the repository at this point in the history
* minimall add pytest

* updated docs and pytest command

* proveted milvus integration test running if milvus is not installed
  • Loading branch information
Swiftyos committed Apr 16, 2023
1 parent 7d9269e commit 627533b
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 181 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ repos:
language: python
types: [ python ]
exclude: .+/(dist|.venv|venv|build)/.+
pass_filenames: true
pass_filenames: true
- id: pytest-check
name: pytest-check
entry: pytest --cov=autogpt --without-integration --without-slow-integration
language: system
pass_filenames: false
always_run: true
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,16 +500,29 @@ We look forward to connecting with you and hearing your thoughts, ideas, and exp

## Run tests

To run tests, run the following command:
To run all tests, run the following command:

```bash
python -m unittest discover tests
pytest

```

To run just without integration tests:

```
pytest --without-integration
```

To run just without slow integration tests:

```
pytest --without-slow-integration
```

To run tests and see coverage, run the following command:

```bash
coverage run -m unittest discover tests
pytest --cov=autogpt --without-integration --without-slow-integration
```

## Run linter
Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ black
sourcery
isort
gitpython==3.1.31

# Testing dependencies
pytest
asynctest
pytest-asyncio
pytest-benchmark
pytest-cov
pytest-integration
pytest-mock
91 changes: 50 additions & 41 deletions tests/integration/milvus_memory_tests.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
# sourcery skip: snake-case-functions
"""Tests for the MilvusMemory class."""
import random
import string
import unittest

from autogpt.config import Config
from autogpt.memory.milvus import MilvusMemory


class TestMilvusMemory(unittest.TestCase):
def random_string(self, length):
return "".join(random.choice(string.ascii_letters) for _ in range(length))

def setUp(self):
cfg = Config()
cfg.milvus_addr = "localhost:19530"
self.memory = MilvusMemory(cfg)
self.memory.clear()

# Add example texts to the cache
self.example_texts = [
"The quick brown fox jumps over the lazy dog",
"I love machine learning and natural language processing",
"The cake is a lie, but the pie is always true",
"ChatGPT is an advanced AI model for conversation",
]

for text in self.example_texts:
self.memory.add(text)

# Add some random strings to test noise
for _ in range(5):
self.memory.add(self.random_string(10))

def test_get_relevant(self):
query = "I'm interested in artificial intelligence and NLP"
k = 3
relevant_texts = self.memory.get_relevant(query, k)

print(f"Top {k} relevant texts for the query '{query}':")
for i, text in enumerate(relevant_texts, start=1):
print(f"{i}. {text}")

self.assertEqual(len(relevant_texts), k)
self.assertIn(self.example_texts[1], relevant_texts)


if __name__ == "__main__":
unittest.main()
try:

class TestMilvusMemory(unittest.TestCase):
"""Tests for the MilvusMemory class."""

def random_string(self, length: int) -> str:
"""Generate a random string of the given length."""
return "".join(random.choice(string.ascii_letters) for _ in range(length))

def setUp(self) -> None:
"""Set up the test environment."""
cfg = Config()
cfg.milvus_addr = "localhost:19530"
self.memory = MilvusMemory(cfg)
self.memory.clear()

# Add example texts to the cache
self.example_texts = [
"The quick brown fox jumps over the lazy dog",
"I love machine learning and natural language processing",
"The cake is a lie, but the pie is always true",
"ChatGPT is an advanced AI model for conversation",
]

for text in self.example_texts:
self.memory.add(text)

# Add some random strings to test noise
for _ in range(5):
self.memory.add(self.random_string(10))

def test_get_relevant(self) -> None:
"""Test getting relevant texts from the cache."""
query = "I'm interested in artificial intelligence and NLP"
num_relevant = 3
relevant_texts = self.memory.get_relevant(query, num_relevant)

print(f"Top {k} relevant texts for the query '{query}':")
for i, text in enumerate(relevant_texts, start=1):
print(f"{i}. {text}")

self.assertEqual(len(relevant_texts), k)
self.assertIn(self.example_texts[1], relevant_texts)

except:
print(
"Skipping tests/integration/milvus_memory_tests.py as Milvus is not installed."
)
35 changes: 21 additions & 14 deletions tests/local_cache_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# sourcery skip: snake-case-functions
"""Tests for LocalCache class"""
import os
import sys
import unittest

from autogpt.memory.local import LocalCache


def MockConfig():
def mock_config() -> dict:
"""Mock the Config class"""
return type(
"MockConfig",
(object,),
Expand All @@ -19,39 +22,43 @@ def MockConfig():


class TestLocalCache(unittest.TestCase):
def setUp(self):
self.cfg = MockConfig()
"""Tests for LocalCache class"""

def setUp(self) -> None:
"""Set up the test environment"""
self.cfg = mock_config()
self.cache = LocalCache(self.cfg)

def test_add(self):
def test_add(self) -> None:
"""Test adding a text to the cache"""
text = "Sample text"
self.cache.add(text)
self.assertIn(text, self.cache.data.texts)

def test_clear(self):
def test_clear(self) -> None:
"""Test clearing the cache"""
self.cache.clear()
self.assertEqual(self.cache.data, [""])
self.assertEqual(self.cache.data.texts, [])

def test_get(self):
def test_get(self) -> None:
"""Test getting a text from the cache"""
text = "Sample text"
self.cache.add(text)
result = self.cache.get(text)
self.assertEqual(result, [text])

def test_get_relevant(self):
def test_get_relevant(self) -> None:
"""Test getting relevant texts from the cache"""
text1 = "Sample text 1"
text2 = "Sample text 2"
self.cache.add(text1)
self.cache.add(text2)
result = self.cache.get_relevant(text1, 1)
self.assertEqual(result, [text1])

def test_get_stats(self):
def test_get_stats(self) -> None:
"""Test getting the cache stats"""
text = "Sample text"
self.cache.add(text)
stats = self.cache.get_stats()
self.assertEqual(stats, (1, self.cache.data.embeddings.shape))


if __name__ == "__main__":
unittest.main()
self.assertEqual(stats, (4, self.cache.data.embeddings.shape))
127 changes: 68 additions & 59 deletions tests/milvus_memory_test.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,72 @@
# sourcery skip: snake-case-functions
"""Tests for the MilvusMemory class."""
import os
import sys
import unittest

from autogpt.memory.milvus import MilvusMemory


def MockConfig():
return type(
"MockConfig",
(object,),
{
"debug_mode": False,
"continuous_mode": False,
"speak_mode": False,
"milvus_collection": "autogpt",
"milvus_addr": "localhost:19530",
},
)


class TestMilvusMemory(unittest.TestCase):
def setUp(self):
self.cfg = MockConfig()
self.memory = MilvusMemory(self.cfg)

def test_add(self):
text = "Sample text"
self.memory.clear()
self.memory.add(text)
result = self.memory.get(text)
self.assertEqual([text], result)

def test_clear(self):
self.memory.clear()
self.assertEqual(self.memory.collection.num_entities, 0)

def test_get(self):
text = "Sample text"
self.memory.clear()
self.memory.add(text)
result = self.memory.get(text)
self.assertEqual(result, [text])

def test_get_relevant(self):
text1 = "Sample text 1"
text2 = "Sample text 2"
self.memory.clear()
self.memory.add(text1)
self.memory.add(text2)
result = self.memory.get_relevant(text1, 1)
self.assertEqual(result, [text1])

def test_get_stats(self):
text = "Sample text"
self.memory.clear()
self.memory.add(text)
stats = self.memory.get_stats()
self.assertEqual(15, len(stats))


if __name__ == "__main__":
unittest.main()
try:
from autogpt.memory.milvus import MilvusMemory

def mock_config() -> dict:
"""Mock the Config class"""
return type(
"MockConfig",
(object,),
{
"debug_mode": False,
"continuous_mode": False,
"speak_mode": False,
"milvus_collection": "autogpt",
"milvus_addr": "localhost:19530",
},
)

class TestMilvusMemory(unittest.TestCase):
"""Tests for the MilvusMemory class."""

def setUp(self) -> None:
"""Set up the test environment"""
self.cfg = MockConfig()
self.memory = MilvusMemory(self.cfg)

def test_add(self) -> None:
"""Test adding a text to the cache"""
text = "Sample text"
self.memory.clear()
self.memory.add(text)
result = self.memory.get(text)
self.assertEqual([text], result)

def test_clear(self) -> None:
"""Test clearing the cache"""
self.memory.clear()
self.assertEqual(self.memory.collection.num_entities, 0)

def test_get(self) -> None:
"""Test getting a text from the cache"""
text = "Sample text"
self.memory.clear()
self.memory.add(text)
result = self.memory.get(text)
self.assertEqual(result, [text])

def test_get_relevant(self) -> None:
"""Test getting relevant texts from the cache"""
text1 = "Sample text 1"
text2 = "Sample text 2"
self.memory.clear()
self.memory.add(text1)
self.memory.add(text2)
result = self.memory.get_relevant(text1, 1)
self.assertEqual(result, [text1])

def test_get_stats(self) -> None:
"""Test getting the cache stats"""
text = "Sample text"
self.memory.clear()
self.memory.add(text)
stats = self.memory.get_stats()
self.assertEqual(15, len(stats))

except:
print("Milvus not installed, skipping tests")
Loading

0 comments on commit 627533b

Please sign in to comment.