Improve tests (#800)
This commit is contained in:
@@ -1,108 +1,106 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from embedchain import App, CustomApp, Llama2App, OpenSourceApp
|
||||
from embedchain.config import (AddConfig, AppConfig, BaseEmbedderConfig,
|
||||
BaseLlmConfig, ChromaDbConfig)
|
||||
from embedchain.config import AddConfig, AppConfig, BaseEmbedderConfig, BaseLlmConfig, ChromaDbConfig
|
||||
from embedchain.embedder.base import BaseEmbedder
|
||||
from embedchain.llm.base import BaseLlm
|
||||
from embedchain.vectordb.base import BaseVectorDB, BaseVectorDbConfig
|
||||
from embedchain.vectordb.chroma import ChromaDB
|
||||
|
||||
|
||||
class TestApps(unittest.TestCase):
|
||||
@pytest.fixture
|
||||
def app():
|
||||
os.environ["OPENAI_API_KEY"] = "test_api_key"
|
||||
|
||||
def test_app(self):
|
||||
app = App()
|
||||
self.assertIsInstance(app.llm, BaseLlm)
|
||||
self.assertIsInstance(app.db, BaseVectorDB)
|
||||
self.assertIsInstance(app.embedder, BaseEmbedder)
|
||||
|
||||
wrong_llm = "wrong_llm"
|
||||
with self.assertRaises(TypeError):
|
||||
App(llm=wrong_llm)
|
||||
|
||||
wrong_db = "wrong_db"
|
||||
with self.assertRaises(TypeError):
|
||||
App(db=wrong_db)
|
||||
|
||||
wrong_embedder = "wrong_embedder"
|
||||
with self.assertRaises(TypeError):
|
||||
App(embedder=wrong_embedder)
|
||||
|
||||
def test_custom_app(self):
|
||||
app = CustomApp()
|
||||
self.assertIsInstance(app.llm, BaseLlm)
|
||||
self.assertIsInstance(app.db, BaseVectorDB)
|
||||
self.assertIsInstance(app.embedder, BaseEmbedder)
|
||||
|
||||
def test_opensource_app(self):
|
||||
app = OpenSourceApp()
|
||||
self.assertIsInstance(app.llm, BaseLlm)
|
||||
self.assertIsInstance(app.db, BaseVectorDB)
|
||||
self.assertIsInstance(app.embedder, BaseEmbedder)
|
||||
|
||||
def test_llama2_app(self):
|
||||
os.environ["REPLICATE_API_TOKEN"] = "-"
|
||||
app = Llama2App()
|
||||
self.assertIsInstance(app.llm, BaseLlm)
|
||||
self.assertIsInstance(app.db, BaseVectorDB)
|
||||
self.assertIsInstance(app.embedder, BaseEmbedder)
|
||||
return App()
|
||||
|
||||
|
||||
class TestConfigForAppComponents(unittest.TestCase):
|
||||
collection_name = "my-test-collection"
|
||||
@pytest.fixture
|
||||
def custom_app():
|
||||
os.environ["OPENAI_API_KEY"] = "test_api_key"
|
||||
return CustomApp()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def opensource_app():
|
||||
os.environ["OPENAI_API_KEY"] = "test_api_key"
|
||||
return OpenSourceApp()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def llama2_app():
|
||||
os.environ["OPENAI_API_KEY"] = "test_api_key"
|
||||
os.environ["REPLICATE_API_TOKEN"] = "-"
|
||||
return Llama2App()
|
||||
|
||||
|
||||
def test_app(app):
|
||||
assert isinstance(app.llm, BaseLlm)
|
||||
assert isinstance(app.db, BaseVectorDB)
|
||||
assert isinstance(app.embedder, BaseEmbedder)
|
||||
|
||||
|
||||
def test_custom_app(custom_app):
|
||||
assert isinstance(custom_app.llm, BaseLlm)
|
||||
assert isinstance(custom_app.db, BaseVectorDB)
|
||||
assert isinstance(custom_app.embedder, BaseEmbedder)
|
||||
|
||||
|
||||
def test_opensource_app(opensource_app):
|
||||
assert isinstance(opensource_app.llm, BaseLlm)
|
||||
assert isinstance(opensource_app.db, BaseVectorDB)
|
||||
assert isinstance(opensource_app.embedder, BaseEmbedder)
|
||||
|
||||
|
||||
def test_llama2_app(llama2_app):
|
||||
assert isinstance(llama2_app.llm, BaseLlm)
|
||||
assert isinstance(llama2_app.db, BaseVectorDB)
|
||||
assert isinstance(llama2_app.embedder, BaseEmbedder)
|
||||
|
||||
|
||||
class TestConfigForAppComponents:
|
||||
def test_constructor_config(self):
|
||||
"""
|
||||
Test that app can be configured through the app constructor.
|
||||
"""
|
||||
app = App(db_config=ChromaDbConfig(collection_name=self.collection_name))
|
||||
self.assertEqual(app.db.config.collection_name, self.collection_name)
|
||||
collection_name = "my-test-collection"
|
||||
app = App(db_config=ChromaDbConfig(collection_name=collection_name))
|
||||
assert app.db.config.collection_name == collection_name
|
||||
|
||||
def test_component_config(self):
|
||||
"""
|
||||
Test that app can also be configured by passing a configured component to the app
|
||||
"""
|
||||
database = ChromaDB(config=ChromaDbConfig(collection_name=self.collection_name))
|
||||
collection_name = "my-test-collection"
|
||||
database = ChromaDB(config=ChromaDbConfig(collection_name=collection_name))
|
||||
app = App(db=database)
|
||||
self.assertEqual(app.db.config.collection_name, self.collection_name)
|
||||
assert app.db.config.collection_name == collection_name
|
||||
|
||||
def test_different_configs_are_proper_instances(self):
|
||||
config = AppConfig()
|
||||
wrong_app_config = AddConfig()
|
||||
app_config = AppConfig()
|
||||
wrong_config = AddConfig()
|
||||
with pytest.raises(TypeError):
|
||||
App(config=wrong_config)
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
App(config=wrong_app_config)
|
||||
|
||||
self.assertIsInstance(config, AppConfig)
|
||||
assert isinstance(app_config, AppConfig)
|
||||
|
||||
llm_config = BaseLlmConfig()
|
||||
wrong_llm_config = "wrong_llm_config"
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
with pytest.raises(TypeError):
|
||||
App(llm_config=wrong_llm_config)
|
||||
|
||||
self.assertIsInstance(llm_config, BaseLlmConfig)
|
||||
assert isinstance(llm_config, BaseLlmConfig)
|
||||
|
||||
db_config = BaseVectorDbConfig()
|
||||
wrong_db_config = "wrong_db_config"
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
with pytest.raises(TypeError):
|
||||
App(db_config=wrong_db_config)
|
||||
|
||||
self.assertIsInstance(db_config, BaseVectorDbConfig)
|
||||
assert isinstance(db_config, BaseVectorDbConfig)
|
||||
|
||||
embedder_config = BaseEmbedderConfig()
|
||||
wrong_embedder_config = "wrong_embedder_config"
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
with pytest.raises(TypeError):
|
||||
App(embedder_config=wrong_embedder_config)
|
||||
|
||||
self.assertIsInstance(embedder_config, BaseEmbedderConfig)
|
||||
assert isinstance(embedder_config, BaseEmbedderConfig)
|
||||
|
||||
|
||||
class TestAppFromConfig:
|
||||
|
||||
80
tests/apps/test_person_app.py
Normal file
80
tests/apps/test_person_app.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import pytest
|
||||
from embedchain.apps.app import App
|
||||
from embedchain.apps.person_app import PersonApp, PersonOpenSourceApp
|
||||
from embedchain.config import BaseLlmConfig, AppConfig
|
||||
from embedchain.config.llm.base import DEFAULT_PROMPT
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def person_app():
|
||||
config = AppConfig()
|
||||
return PersonApp("John Doe", config)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def opensource_person_app():
|
||||
config = AppConfig()
|
||||
return PersonOpenSourceApp("John Doe", config)
|
||||
|
||||
|
||||
def test_person_app_initialization(person_app):
|
||||
assert person_app.person == "John Doe"
|
||||
assert f"You are {person_app.person}" in person_app.person_prompt
|
||||
assert isinstance(person_app.config, AppConfig)
|
||||
|
||||
|
||||
def test_person_app_add_person_template_to_config_with_invalid_template():
|
||||
app = PersonApp("John Doe")
|
||||
default_prompt = "Input Prompt"
|
||||
with pytest.raises(ValueError):
|
||||
# as prompt doesn't contain $context and $query
|
||||
app.add_person_template_to_config(default_prompt)
|
||||
|
||||
|
||||
def test_person_app_add_person_template_to_config_with_valid_template():
|
||||
app = PersonApp("John Doe")
|
||||
config = app.add_person_template_to_config(DEFAULT_PROMPT)
|
||||
assert (
|
||||
config.template.template
|
||||
== f"You are John Doe. Whatever you say, you will always say in John Doe style. {DEFAULT_PROMPT}"
|
||||
)
|
||||
|
||||
|
||||
def test_person_app_query(mocker, person_app):
|
||||
input_query = "Hello, how are you?"
|
||||
config = BaseLlmConfig()
|
||||
|
||||
mocker.patch.object(App, "query", return_value="Mocked response")
|
||||
|
||||
result = person_app.query(input_query, config)
|
||||
assert result == "Mocked response"
|
||||
|
||||
|
||||
def test_person_app_chat(mocker, person_app):
|
||||
input_query = "Hello, how are you?"
|
||||
config = BaseLlmConfig()
|
||||
|
||||
mocker.patch.object(App, "chat", return_value="Mocked chat response")
|
||||
|
||||
result = person_app.chat(input_query, config)
|
||||
assert result == "Mocked chat response"
|
||||
|
||||
|
||||
def test_opensource_person_app_query(mocker, opensource_person_app):
|
||||
input_query = "Hello, how are you?"
|
||||
config = BaseLlmConfig()
|
||||
|
||||
mocker.patch.object(App, "query", return_value="Mocked response")
|
||||
|
||||
result = opensource_person_app.query(input_query, config)
|
||||
assert result == "Mocked response"
|
||||
|
||||
|
||||
def test_opensource_person_app_chat(mocker, opensource_person_app):
|
||||
input_query = "Hello, how are you?"
|
||||
config = BaseLlmConfig()
|
||||
|
||||
mocker.patch.object(App, "chat", return_value="Mocked chat response")
|
||||
|
||||
result = opensource_person_app.chat(input_query, config)
|
||||
assert result == "Mocked chat response"
|
||||
Reference in New Issue
Block a user