Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds tests for formatter and decorator
Browse files Browse the repository at this point in the history
amandasavluchinske committed Jun 21, 2024
1 parent a6a426c commit 8d7055d
Showing 3 changed files with 57 additions and 4 deletions.
3 changes: 2 additions & 1 deletion django_ai_assistant/decorators.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from functools import wraps

from django_ai_assistant.helpers.formatters import cast_id
from django_ai_assistant.models import Message, Thread


def with_cast_id(func):
@wraps(func)
def wrapper(*args, **kwargs):
from django_ai_assistant.models import Message, Thread

thread_id = kwargs.get("thread_id")
message_id = kwargs.get("message_id")
message_ids = kwargs.get("message_ids")
51 changes: 51 additions & 0 deletions tests/test_helpers/test_formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import uuid

from django_ai_assistant.decorators import with_cast_id
from django_ai_assistant.helpers.formatters import cast_id
from django_ai_assistant.models import Thread


def test_cast_id_does_not_transform_regular_ids():
assert isinstance(cast_id(1, Thread), int)


def test_cast_id_does_not_transform_str_ids():
assert isinstance(cast_id("dfjsdjfkndskjf", Thread), str)


def test_cast_id_transforms_uuids(monkeypatch):
def mock_get_internal_type():
return "UUIDField"

monkeypatch.setattr(Thread._meta.pk, "get_internal_type", mock_get_internal_type)

assert isinstance(cast_id("c8e6d7f7-7b2e-4d3b-8b9d-5b1d4b1f3e6b", Thread), uuid.UUID)


def test_with_cast_id_transforms_regular_ids():
@with_cast_id
def dummy_function(thread_id):
return thread_id

assert isinstance(dummy_function(thread_id=1), int)


def test_with_cast_id_transforms_str_ids():
@with_cast_id
def dummy_function(thread_id):
return thread_id

assert isinstance(dummy_function(thread_id="dfjsdjfkndskjf"), str)


def test_with_cast_id_transforms_uuids(monkeypatch):
def mock_get_internal_type():
return "UUIDField"

monkeypatch.setattr(Thread._meta.pk, "get_internal_type", mock_get_internal_type)

@with_cast_id
def dummy_function(thread_id):
return thread_id

assert isinstance(dummy_function(thread_id="c8e6d7f7-7b2e-4d3b-8b9d-5b1d4b1f3e6b"), uuid.UUID)
7 changes: 4 additions & 3 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.models import User

import pytest
from model_bakery import baker

from django_ai_assistant.models import Thread
from django_ai_assistant.permissions import owns_thread
@@ -18,14 +19,14 @@ def regular_user(db):

@pytest.mark.django_db()
def test_owns_thread(superuser, regular_user):
thread = Thread.objects.create(name="AAA")
thread = baker.make(Thread, name="AAA")
assert owns_thread(superuser, thread)
assert not owns_thread(regular_user, thread)

thread = Thread.objects.create(name="CCC", created_by=superuser)
thread = baker.make(Thread, name="CCC", created_by=superuser)
assert owns_thread(superuser, thread)
assert not owns_thread(regular_user, thread)

thread = Thread.objects.create(name="BBB", created_by=regular_user)
thread = baker.make(Thread, name="BBB", created_by=regular_user)
assert owns_thread(superuser, thread)
assert owns_thread(regular_user, thread)

0 comments on commit 8d7055d

Please sign in to comment.