minimall add pytest (#1859)

* minimall add pytest

* updated docs and pytest command

* proveted milvus integration test running if milvus is not installed
This commit is contained in:
0xArty
2023-04-16 21:55:53 +01:00
committed by GitHub
parent 7d9269e1a1
commit 627533bed6
8 changed files with 208 additions and 157 deletions

View File

@@ -31,3 +31,9 @@ repos:
types: [ python ] types: [ python ]
exclude: .+/(dist|.venv|venv|build)/.+ 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

View File

@@ -500,16 +500,29 @@ We look forward to connecting with you and hearing your thoughts, ideas, and exp
## Run tests ## Run tests
To run tests, run the following command: To run all tests, run the following command:
```bash ```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: To run tests and see coverage, run the following command:
```bash ```bash
coverage run -m unittest discover tests pytest --cov=autogpt --without-integration --without-slow-integration
``` ```
## Run linter ## Run linter

View File

@@ -29,5 +29,12 @@ black
sourcery sourcery
isort isort
gitpython==3.1.31 gitpython==3.1.31
# Testing dependencies
pytest pytest
asynctest
pytest-asyncio
pytest-benchmark
pytest-cov
pytest-integration
pytest-mock pytest-mock

View File

@@ -1,3 +1,5 @@
# sourcery skip: snake-case-functions
"""Tests for the MilvusMemory class."""
import random import random
import string import string
import unittest import unittest
@@ -5,12 +7,17 @@ import unittest
from autogpt.config import Config from autogpt.config import Config
from autogpt.memory.milvus import MilvusMemory from autogpt.memory.milvus import MilvusMemory
try:
class TestMilvusMemory(unittest.TestCase): class TestMilvusMemory(unittest.TestCase):
def random_string(self, length): """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)) return "".join(random.choice(string.ascii_letters) for _ in range(length))
def setUp(self): def setUp(self) -> None:
"""Set up the test environment."""
cfg = Config() cfg = Config()
cfg.milvus_addr = "localhost:19530" cfg.milvus_addr = "localhost:19530"
self.memory = MilvusMemory(cfg) self.memory = MilvusMemory(cfg)
@@ -31,10 +38,11 @@ class TestMilvusMemory(unittest.TestCase):
for _ in range(5): for _ in range(5):
self.memory.add(self.random_string(10)) self.memory.add(self.random_string(10))
def test_get_relevant(self): def test_get_relevant(self) -> None:
"""Test getting relevant texts from the cache."""
query = "I'm interested in artificial intelligence and NLP" query = "I'm interested in artificial intelligence and NLP"
k = 3 num_relevant = 3
relevant_texts = self.memory.get_relevant(query, k) relevant_texts = self.memory.get_relevant(query, num_relevant)
print(f"Top {k} relevant texts for the query '{query}':") print(f"Top {k} relevant texts for the query '{query}':")
for i, text in enumerate(relevant_texts, start=1): for i, text in enumerate(relevant_texts, start=1):
@@ -43,6 +51,7 @@ class TestMilvusMemory(unittest.TestCase):
self.assertEqual(len(relevant_texts), k) self.assertEqual(len(relevant_texts), k)
self.assertIn(self.example_texts[1], relevant_texts) self.assertIn(self.example_texts[1], relevant_texts)
except:
if __name__ == "__main__": print(
unittest.main() "Skipping tests/integration/milvus_memory_tests.py as Milvus is not installed."
)

View File

@@ -1,3 +1,5 @@
# sourcery skip: snake-case-functions
"""Tests for LocalCache class"""
import os import os
import sys import sys
import unittest import unittest
@@ -5,7 +7,8 @@ import unittest
from autogpt.memory.local import LocalCache from autogpt.memory.local import LocalCache
def MockConfig(): def mock_config() -> dict:
"""Mock the Config class"""
return type( return type(
"MockConfig", "MockConfig",
(object,), (object,),
@@ -19,26 +22,33 @@ def MockConfig():
class TestLocalCache(unittest.TestCase): class TestLocalCache(unittest.TestCase):
def setUp(self): """Tests for LocalCache class"""
self.cfg = MockConfig()
def setUp(self) -> None:
"""Set up the test environment"""
self.cfg = mock_config()
self.cache = LocalCache(self.cfg) self.cache = LocalCache(self.cfg)
def test_add(self): def test_add(self) -> None:
"""Test adding a text to the cache"""
text = "Sample text" text = "Sample text"
self.cache.add(text) self.cache.add(text)
self.assertIn(text, self.cache.data.texts) self.assertIn(text, self.cache.data.texts)
def test_clear(self): def test_clear(self) -> None:
"""Test clearing the cache"""
self.cache.clear() 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" text = "Sample text"
self.cache.add(text) self.cache.add(text)
result = self.cache.get(text) result = self.cache.get(text)
self.assertEqual(result, [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" text1 = "Sample text 1"
text2 = "Sample text 2" text2 = "Sample text 2"
self.cache.add(text1) self.cache.add(text1)
@@ -46,12 +56,9 @@ class TestLocalCache(unittest.TestCase):
result = self.cache.get_relevant(text1, 1) result = self.cache.get_relevant(text1, 1)
self.assertEqual(result, [text1]) self.assertEqual(result, [text1])
def test_get_stats(self): def test_get_stats(self) -> None:
"""Test getting the cache stats"""
text = "Sample text" text = "Sample text"
self.cache.add(text) self.cache.add(text)
stats = self.cache.get_stats() stats = self.cache.get_stats()
self.assertEqual(stats, (1, self.cache.data.embeddings.shape)) self.assertEqual(stats, (4, self.cache.data.embeddings.shape))
if __name__ == "__main__":
unittest.main()

View File

@@ -1,11 +1,14 @@
# sourcery skip: snake-case-functions
"""Tests for the MilvusMemory class."""
import os import os
import sys import sys
import unittest import unittest
try:
from autogpt.memory.milvus import MilvusMemory from autogpt.memory.milvus import MilvusMemory
def mock_config() -> dict:
def MockConfig(): """Mock the Config class"""
return type( return type(
"MockConfig", "MockConfig",
(object,), (object,),
@@ -18,31 +21,37 @@ def MockConfig():
}, },
) )
class TestMilvusMemory(unittest.TestCase): class TestMilvusMemory(unittest.TestCase):
def setUp(self): """Tests for the MilvusMemory class."""
def setUp(self) -> None:
"""Set up the test environment"""
self.cfg = MockConfig() self.cfg = MockConfig()
self.memory = MilvusMemory(self.cfg) self.memory = MilvusMemory(self.cfg)
def test_add(self): def test_add(self) -> None:
"""Test adding a text to the cache"""
text = "Sample text" text = "Sample text"
self.memory.clear() self.memory.clear()
self.memory.add(text) self.memory.add(text)
result = self.memory.get(text) result = self.memory.get(text)
self.assertEqual([text], result) self.assertEqual([text], result)
def test_clear(self): def test_clear(self) -> None:
"""Test clearing the cache"""
self.memory.clear() self.memory.clear()
self.assertEqual(self.memory.collection.num_entities, 0) self.assertEqual(self.memory.collection.num_entities, 0)
def test_get(self): def test_get(self) -> None:
"""Test getting a text from the cache"""
text = "Sample text" text = "Sample text"
self.memory.clear() self.memory.clear()
self.memory.add(text) self.memory.add(text)
result = self.memory.get(text) result = self.memory.get(text)
self.assertEqual(result, [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" text1 = "Sample text 1"
text2 = "Sample text 2" text2 = "Sample text 2"
self.memory.clear() self.memory.clear()
@@ -51,13 +60,13 @@ class TestMilvusMemory(unittest.TestCase):
result = self.memory.get_relevant(text1, 1) result = self.memory.get_relevant(text1, 1)
self.assertEqual(result, [text1]) self.assertEqual(result, [text1])
def test_get_stats(self): def test_get_stats(self) -> None:
"""Test getting the cache stats"""
text = "Sample text" text = "Sample text"
self.memory.clear() self.memory.clear()
self.memory.add(text) self.memory.add(text)
stats = self.memory.get_stats() stats = self.memory.get_stats()
self.assertEqual(15, len(stats)) self.assertEqual(15, len(stats))
except:
if __name__ == "__main__": print("Milvus not installed, skipping tests")
unittest.main()

View File

@@ -1,19 +1,22 @@
"""Smoke test for the autogpt package."""
import os import os
import subprocess import subprocess
import sys import sys
import unittest
import pytest
from autogpt.commands.file_operations import delete_file, read_file from autogpt.commands.file_operations import delete_file, read_file
@pytest.mark.integration_test
def test_write_file() -> None:
"""
Test case to check if the write_file command can successfully write 'Hello World' to a file
named 'hello_world.txt'.
Read the current ai_settings.yaml file and store its content.
"""
env_vars = {"MEMORY_BACKEND": "no_memory", "TEMPERATURE": "0"} env_vars = {"MEMORY_BACKEND": "no_memory", "TEMPERATURE": "0"}
class TestCommands(unittest.TestCase):
def test_write_file(self):
# Test case to check if the write_file command can successfully write 'Hello World' to a file
# named 'hello_world.txt'.
# Read the current ai_settings.yaml file and store its content.
ai_settings = None ai_settings = None
if os.path.exists("ai_settings.yaml"): if os.path.exists("ai_settings.yaml"):
with open("ai_settings.yaml", "r") as f: with open("ai_settings.yaml", "r") as f:
@@ -53,11 +56,4 @@ EOF"""
f.write(ai_settings) f.write(ai_settings)
# Check if the content of the 'hello_world.txt' file is equal to 'Hello World'. # Check if the content of the 'hello_world.txt' file is equal to 'Hello World'.
self.assertEqual( assert content == "Hello World", f"Expected 'Hello World', got {content}"
content, "Hello World", f"Expected 'Hello World', got {content}"
)
# Run the test case.
if __name__ == "__main__":
unittest.main()

View File

@@ -1,18 +1,22 @@
"""Unit tests for the commands module"""
from unittest.mock import MagicMock, patch
import pytest
import autogpt.agent.agent_manager as agent_manager import autogpt.agent.agent_manager as agent_manager
from autogpt.app import start_agent, list_agents, execute_command from autogpt.app import execute_command, list_agents, start_agent
import unittest
from unittest.mock import patch, MagicMock
class TestCommands(unittest.TestCase): @pytest.mark.integration_test
def test_make_agent(self): def test_make_agent() -> None:
"""Test the make_agent command"""
with patch("openai.ChatCompletion.create") as mock: with patch("openai.ChatCompletion.create") as mock:
obj = MagicMock() obj = MagicMock()
obj.response.choices[0].messages[0].content = "Test message" obj.response.choices[0].messages[0].content = "Test message"
mock.return_value = obj mock.return_value = obj
start_agent("Test Agent", "chat", "Hello, how are you?", "gpt2") start_agent("Test Agent", "chat", "Hello, how are you?", "gpt2")
agents = list_agents() agents = list_agents()
self.assertEqual("List of agents:\n0: chat", agents) assert "List of agents:\n0: chat" == agents
start_agent("Test Agent 2", "write", "Hello, how are you?", "gpt2") start_agent("Test Agent 2", "write", "Hello, how are you?", "gpt2")
agents = list_agents() agents = list_agents()
self.assertEqual("List of agents:\n0: chat\n1: write", agents) assert "List of agents:\n0: chat\n1: write" == agents