Initial test setup.

This commit is contained in:
Hemang
2025-02-05 19:10:36 +01:00
parent e279351e93
commit c4fe6ebae3
8 changed files with 106 additions and 1 deletions
+11 -1
View File
@@ -1 +1,11 @@
INVARIANT_API_URL=https://explorer.invariantlabs.ai
INVARIANT_API_URL=https://explorer.invariantlabs.ai
# For the testing stack only
# TODO: Check if these can be removed.
# Currently we rely on the `invariant explorer` command
# to setup a local instance of explorer to test the proxy.
# That requires these variables to be set in the .env file.
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=invariantmonitor
POSTGRES_HOST=database
+1
View File
@@ -2,6 +2,7 @@
__pycache__/
.pytest_cache/
.py[oc]
data/
# Coverage and build artifacts
.coverage
+29
View File
@@ -20,6 +20,27 @@ down() {
docker compose -f docker-compose.local.yml down
}
tests() {
# Run tests
pip install invariant-ai
invariant explorer up -d --build
until curl -X GET -I http://127.0.0.1/api/v1 --fail --silent --output /dev/null; do
echo "Backend API not available yet - checking health..."
sleep 2
done
echo "Backend API is available. Running tests..."
docker build -t 'explorer-proxy-test' -f ./tests/Dockerfile.test ./tests
docker run \
--mount type=bind,source=./tests,target=/tests \
--network host \
explorer-proxy-test $@
}
# -----------------------------
# Command dispatcher
# -----------------------------
@@ -36,4 +57,12 @@ case "$1" in
"logs")
docker compose -f docker-compose.local.yml logs -f
;;
"tests")
shift
tests $@
;;
*)
echo "Usage: $0 [up|build|down|logs|tests]"
exit 1
;;
esac
+8
View File
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/playwright/python:v1.50.0-noble
RUN mkdir -p /tests
COPY ./requirements.txt /tests/requirements.txt
WORKDIR /tests
RUN pip install --no-cache-dir -r requirements.txt
ENTRYPOINT ["pytest", "-s", "-vv"]
@@ -0,0 +1,22 @@
"""Test the chat completions proxy calls without tool calling."""
import os
# add tests folder (parent) to sys.path
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from util import * # needed for pytest fixtures
pytest_plugins = ("pytest_asyncio",)
async def test_hello_world(context, url):
"""Demo test"""
response = await context.request.get(
f"{url}/api/v1/dataset/byuser/developer/Welcome-to-Explorer"
)
dataset = await response.json()
assert dataset["name"] == "Welcome-to-Explorer"
assert dataset["user"]["username"] == "developer"
+2
View File
@@ -0,0 +1,2 @@
[pytest]
asyncio_mode = auto
+4
View File
@@ -0,0 +1,4 @@
openai
pytest
pytest-asyncio
pytest-playwright
+29
View File
@@ -0,0 +1,29 @@
"""Util functions for tests"""
import pytest
from playwright.async_api import async_playwright
@pytest.fixture
def url():
return "http://127.0.0.1"
@pytest.fixture
async def playwright(scope="session"):
async with async_playwright() as playwright_instance:
yield playwright_instance
@pytest.fixture
async def browser(playwright, scope="session"):
browser = await playwright.firefox.launch(headless=True)
yield browser
await browser.close()
@pytest.fixture
async def context(browser):
context = await browser.new_context(ignore_https_errors=True)
yield context
await context.close()