Skip to content

Commit

Permalink
Adds tests for list threads and also a helper to create threads
Browse files Browse the repository at this point in the history
  • Loading branch information
amandasavluchinske committed Jun 17, 2024
1 parent b2266a0 commit df723f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.contrib.auth.models import User

from django_ai_assistant.models import Message, Thread


def create_thread(
name: str | None = None, user: User | None = None, message: str | None = None
) -> Thread:
name = name or "Test Thread"
user = user or User.objects.create_user(username="test_user")
message = message or "Test Message"

thread = Thread.objects.create(name=name, created_by=user)

Message.objects.create(message={"content": message}, thread=thread)

thread.refresh_from_db()

return thread
24 changes: 23 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from http import HTTPStatus

from django.contrib.auth.models import User
from django.test import Client, TestCase

import pytest

from django_ai_assistant.exceptions import AIAssistantNotDefinedError
from django_ai_assistant.helpers.assistants import AIAssistant, register_assistant
from django_ai_assistant.tools import BaseModel, Field, method_tool
from tests.helpers import create_thread


# Set up
Expand Down Expand Up @@ -73,4 +75,24 @@ def test_does_not_return_assistant_if_unauthorized(self):

# Threads Views

# Up next

class ThreadsListViewsTests(TestCase):
def setUp(self):
super().setUp()
self.user = User.objects.create_user(username="testuser", password="password")
self.client = Client()
self.client.login(username="testuser", password="password")

def test_list_threads_without_results(self):
response = self.client.get("/threads/")

assert response.status_code == HTTPStatus.OK
assert response.json() == []

def test_list_threads_with_results(self):
thread = create_thread(user=self.user)

response = self.client.get("/threads/")

assert response.status_code == HTTPStatus.OK
assert response.json()[0].get("id") == thread.id

0 comments on commit df723f0

Please sign in to comment.