llm/tests/test_llm.py

49 lines
1.2 KiB
Python
Raw Normal View History

2023-04-01 21:28:24 +00:00
from click.testing import CliRunner
2023-04-01 22:00:16 +00:00
from llm.cli import cli
2023-04-02 01:52:46 +00:00
import json
import pytest
import sqlite_utils
2023-04-01 21:28:24 +00:00
2023-04-01 22:00:16 +00:00
def test_version():
2023-04-01 21:28:24 +00:00
runner = CliRunner()
with runner.isolated_filesystem():
2023-04-01 22:00:16 +00:00
result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0
assert result.output.startswith("cli, version ")
2023-04-02 01:52:46 +00:00
@pytest.fixture
def log_path(tmp_path):
path = str(tmp_path / "log.db")
db = sqlite_utils.Database(path)
db["log"].insert_all(
{
"command": "chatgpt",
"system": "system",
"prompt": "prompt",
"response": "response",
"model": "davinci",
}
for i in range(100)
)
return path
@pytest.mark.parametrize("n", (None, 0, 2))
def test_logs(n, log_path):
runner = CliRunner()
args = ["logs", "-p", log_path]
if n is not None:
args.extend(["-n", str(n)])
result = runner.invoke(cli, args)
assert result.exit_code == 0
logs = json.loads(result.output)
expected_length = 3
if n is not None:
if n == 0:
expected_length = 100
else:
expected_length = n
assert len(logs) == expected_length