-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
import tomlkit | ||
|
||
from shelloracle.config import Configuration | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def tmp_shelloracle_home(monkeypatch, tmp_path): | ||
monkeypatch.setattr("shelloracle.settings.Settings.shelloracle_home", tmp_path) | ||
return tmp_path | ||
|
||
|
||
@pytest.fixture | ||
def set_config(monkeypatch, tmp_shelloracle_home): | ||
config_path = tmp_shelloracle_home / "config.toml" | ||
|
||
def setter(config: dict) -> Configuration: | ||
with config_path.open("w") as f: | ||
tomlkit.dump(config, f) | ||
configuration = Configuration(config_path) | ||
monkeypatch.setattr("shelloracle.config._config", configuration) | ||
return configuration | ||
|
||
yield setter | ||
|
||
config_path.unlink() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import textwrap | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def ollama_config(set_config): | ||
config = textwrap.dedent("""\ | ||
[shelloracle] | ||
provider = "Ollama" | ||
[provider.Ollama] | ||
host = "localhost" | ||
port = 11434 | ||
model = "dolphin-mistral" | ||
""") | ||
set_config(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
|
||
|
||
class TestLocalAI: | ||
@pytest.fixture | ||
def localai_config(self, set_config): | ||
config = {'shelloracle': {'provider': 'LocalAI'}, | ||
'provider': {'LocalAI': {'host': 'localhost', 'port': 8080, 'model': 'gpt-3.5-turbo'}}} | ||
|
||
return set_config(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,43 @@ | ||
import pytest | ||
from pytest_httpx import IteratorStream | ||
|
||
from shelloracle.providers.ollama import Ollama | ||
|
||
|
||
def test_name(): | ||
assert Ollama.name == "Ollama" | ||
class TestOllama: | ||
@pytest.fixture | ||
def ollama_config(self, set_config): | ||
config = {'shelloracle': {'provider': 'Ollama'}, | ||
'provider': {'Ollama': {'host': 'localhost', 'port': 11434, 'model': 'dolphin-mistral'}}} | ||
return set_config(config) | ||
|
||
@pytest.fixture | ||
def ollama_instance(self, ollama_config): | ||
return Ollama() | ||
|
||
def test_name(self): | ||
assert Ollama.name == "Ollama" | ||
|
||
def test_host(self, ollama_instance): | ||
assert ollama_instance.host == "localhost" | ||
|
||
def test_port(self, ollama_instance): | ||
assert ollama_instance.port == 11434 | ||
|
||
def test_model(self, ollama_instance): | ||
assert ollama_instance.model == "dolphin-mistral" | ||
|
||
def test_endpoint(self, ollama_instance): | ||
assert ollama_instance.endpoint == "http://localhost:11434/api/generate" | ||
|
||
@pytest.mark.asyncio | ||
async def test_generate(self, ollama_instance, httpx_mock): | ||
responses = [ | ||
b'{"response": "cat"}\n', b'{"response": " test"}\n', b'{"response": "."}\n', b'{"response": "py"}\n', | ||
b'{"response": ""}\n' | ||
] | ||
httpx_mock.add_response(stream=IteratorStream(responses)) | ||
result = "" | ||
async for response in ollama_instance.generate(""): | ||
result += response | ||
assert result == "cat test.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
import pytest | ||
|
||
from shelloracle.providers.openai import OpenAI | ||
|
||
|
||
def test_name(): | ||
assert OpenAI.name == "OpenAI" | ||
class TestOpenAI: | ||
@pytest.fixture | ||
def openai_config(self, set_config): | ||
config = {'shelloracle': {'provider': 'OpenAI'}, 'provider': { | ||
'OpenAI': {'api_key': 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'model': 'gpt-3.5-turbo'}}} | ||
return set_config(config) | ||
|
||
@pytest.fixture | ||
def openai_instance(self, openai_config): | ||
return OpenAI() | ||
|
||
def test_name(self): | ||
assert OpenAI.name == "OpenAI" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from shelloracle.config import get_config, initialize_config | ||
|
||
|
||
class TestConfiguration: | ||
@pytest.fixture | ||
def default_config(self, set_config): | ||
config = {'shelloracle': {'provider': 'Ollama', 'spinner_style': 'earth'}, | ||
'provider': {'Ollama': {'host': 'localhost', 'port': 11434, 'model': 'dolphin-mistral'}}} | ||
set_config(config) | ||
return config | ||
|
||
def test_initialize_config(self, default_config): | ||
with pytest.raises(RuntimeError): | ||
initialize_config() | ||
|
||
def test_from_file(self, default_config): | ||
assert get_config() == default_config | ||
|
||
def test_getitem(self, default_config): | ||
for key in default_config: | ||
assert default_config[key] == get_config()[key] | ||
|
||
def test_len(self, default_config): | ||
assert len(default_config) == len(get_config()) | ||
|
||
def test_iter(self, default_config): | ||
assert list(iter(default_config)) == list(iter(get_config())) | ||
|
||
def test_str(self, default_config): | ||
assert str(get_config()) == f"Configuration({default_config})" | ||
|
||
def test_repr(self, default_config): | ||
assert repr(default_config) == str(default_config) | ||
|
||
def test_provider(self, default_config): | ||
assert get_config().provider == "Ollama" | ||
|
||
def test_spinner_style(self, default_config): | ||
assert get_config().spinner_style == "earth" | ||
|
||
def test_no_spinner_style(self, caplog, set_config): | ||
config_dict = {'shelloracle': {'provider': 'Ollama'}, | ||
'provider': {'Ollama': {'host': 'localhost', 'port': 11434, 'model': 'dolphin-mistral'}}} | ||
set_config(config_dict) | ||
assert get_config().spinner_style is None | ||
assert "invalid spinner style" not in caplog.text | ||
|
||
def test_invalid_spinner_style(self, caplog, set_config): | ||
config_dict = {'shelloracle': {'provider': 'Ollama', 'spinner_style': 'invalid'}, | ||
'provider': {'Ollama': {'host': 'localhost', 'port': 11434, 'model': 'dolphin-mistral'}}} | ||
set_config(config_dict) | ||
assert get_config().spinner_style is None | ||
assert "invalid spinner style" in caplog.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters