Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Jun 18, 2024
1 parent 66a3065 commit cef6f31
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ start:

test:
# We need to update handling of env variables for tests
YDC_API_KEY=placeholder OPENAI_API_KEY=placeholder PGVECTOR_URI=postgres://postgres:postgres@langgraph-postgres:5432/postgres?sslmode=disable poetry run pytest $(TEST_FILE)
YDC_API_KEY=placeholder OPENAI_API_KEY=placeholder LANGGRAPH_URL=http://localhost:8123 PGVECTOR_URI=postgresql+psycopg2://postgres:postgres@localhost:5433/postgres?sslmode=disable poetry run pytest $(TEST_FILE)

test_watch:
# We need to update handling of env variables for tests
YDC_API_KEY=placeholder OPENAI_API_KEY=placeholder PGVECTOR_URI=postgres://postgres:postgres@langgraph-postgres:5432/postgres?sslmode=disable poetry run ptw . -- $(TEST_FILE)
YDC_API_KEY=placeholder OPENAI_API_KEY=placeholder LANGGRAPH_URL=http://localhost:8123 PGVECTOR_URI=postgresql+psycopg2://postgres:postgres@localhost:5433/postgres?sslmode=disable poetry run ptw . -- $(TEST_FILE)

######################
# LINTING AND FORMATTING
Expand Down
2 changes: 1 addition & 1 deletion backend/app/api/assistants.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def create_assistant(


@router.patch("/{aid}")
async def upsert_assistant(
async def patch_assistant(
user: AuthedUser,
aid: AssistantID,
payload: AssistantPayload,
Expand Down
2 changes: 1 addition & 1 deletion backend/app/api/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def create_thread(


@router.patch("/{tid}")
async def upsert_thread(
async def patch_thread(
user: AuthedUser,
tid: ThreadID,
thread_put_request: ThreadPutRequest,
Expand Down
7 changes: 5 additions & 2 deletions backend/tests/unit_tests/app/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import asynccontextmanager

from httpx import AsyncClient
from app.lifespan import lifespan
from httpx import AsyncClient, ASGITransport
from typing_extensions import AsyncGenerator


Expand All @@ -9,5 +10,7 @@ async def get_client() -> AsyncGenerator[AsyncClient, None]:
"""Get the app."""
from app.server import app

async with AsyncClient(app=app, base_url="http://test") as ac:
async with lifespan(app), AsyncClient(
transport=ASGITransport(app), base_url="http://test"
) as ac:
yield ac
62 changes: 36 additions & 26 deletions backend/tests/unit_tests/app/test_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test the server and client together."""

from typing import Optional, Sequence
from uuid import uuid4

from tests.unit_tests.app.helpers import get_client

Expand All @@ -15,32 +14,36 @@ def _project(d: dict, *, exclude_keys: Optional[Sequence[str]]) -> dict:
async def test_list_and_create_assistants() -> None:
"""Test list and create assistants."""
headers = {"Cookie": "opengpts_user_id=1"}
aid = str(uuid4())

async with get_client() as client:
response = await client.get(
"/assistants/",
"/api/assistants/",
headers=headers,
)
assert response.status_code == 200

assert response.json() == []

# Create an assistant
response = await client.put(
f"/assistants/{aid}",
json={"name": "bobby", "config": {}, "public": False},
response = await client.post(
"/api/assistants",
json={
"name": "bobby",
"config": {"configurable": {"type": "chatbot"}},
"public": False,
},
headers=headers,
)
assert response.status_code == 200
aid = response.json()["assistant_id"]
assert _project(response.json(), exclude_keys=["updated_at", "user_id"]) == {
"assistant_id": aid,
"config": {},
"config": {"configurable": {"type": "chatbot"}},
"name": "bobby",
"public": False,
}

response = await client.get("/assistants/", headers=headers)
response = await client.get("/api/assistants/", headers=headers)
assert [
_project(d, exclude_keys=["updated_at", "user_id"]) for d in response.json()
] == [
Expand All @@ -52,8 +55,8 @@ async def test_list_and_create_assistants() -> None:
}
]

response = await client.put(
f"/assistants/{aid}",
response = await client.patch(
f"/api/assistants/{aid}",
json={"name": "bobby", "config": {}, "public": False},
headers=headers,
)
Expand All @@ -67,40 +70,50 @@ async def test_list_and_create_assistants() -> None:

# Check not visible to other users
headers = {"Cookie": "opengpts_user_id=2"}
response = await client.get("/assistants/", headers=headers)
response = await client.get("/api/assistants/", headers=headers)
assert response.status_code == 200, response.text
assert response.json() == []

await client.delete(f"/api/assistants/{aid}", headers=headers)


async def test_threads() -> None:
"""Test put thread."""
headers = {"Cookie": "opengpts_user_id=1"}
aid = str(uuid4())
tid = str(uuid4())

async with get_client() as client:
response = await client.put(
f"/assistants/{aid}",
response = await client.post(
"/api/assistants",
json={
"name": "assistant",
"config": {"configurable": {"type": "chatbot"}},
"public": False,
},
headers=headers,
)
assert response.status_code == 200, response.text
aid = response.json()["assistant_id"]

response = await client.put(
f"/threads/{tid}",
response = await client.post(
"/api/threads",
json={"name": "bobby", "assistant_id": aid},
headers=headers,
)
assert response.status_code == 200, response.text
tid = response.json()["thread_id"]

response = await client.get(f"/threads/{tid}/state", headers=headers)
response = await client.get(f"/api/threads/{tid}/state", headers=headers)
assert response.status_code == 200
assert response.json() == {"values": None, "next": []}
assert response.json() == {
"values": {},
"next": [],
"config": {},
"metadata": {},
"created_at": None,
"parent_config": {},
}

response = await client.get("/threads/", headers=headers)
response = await client.get("/api/threads/", headers=headers)

assert response.status_code == 200
assert [
Expand All @@ -110,12 +123,9 @@ async def test_threads() -> None:
"assistant_id": aid,
"name": "bobby",
"thread_id": tid,
"metadata": {"assistant_type": "chatbot"},
"metadata": {},
}
]

response = await client.put(
f"/threads/{tid}",
headers={"Cookie": "opengpts_user_id=2"},
)
assert response.status_code == 422
await client.delete(f"/api/threads/{tid}", headers=headers)
await client.delete(f"/api/assistants/{aid}", headers=headers)

0 comments on commit cef6f31

Please sign in to comment.