Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/django_ai_core/contrib/index/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ def objects_from_documents(
def post_index_update(self, index):
from .models import ModelSourceIndex

index_name = type(index).__name__
ModelSourceIndex.objects.filter(
index_name=index, source_id=self.source_id
index_name=index_name, source_id=self.source_id
).delete()

for obj in self.queryset:
ModelSourceIndex.register(obj, index, self.source_id)
ModelSourceIndex.register(obj, index_name, self.source_id)
22 changes: 22 additions & 0 deletions tests/unit/contrib/index/test_model_source.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import random
import uuid

import pytest
from testapp.models import Book

from django_ai_core.contrib.index.models import ModelSourceIndex
from django_ai_core.contrib.index.source import ModelSource


Expand Down Expand Up @@ -64,3 +66,23 @@ def test_model_source_returns_unique_keys():
documents = model_source.get_documents()
document_keys = [doc.document_key for doc in documents]
assert len(document_keys) == len(set(document_keys))


@pytest.mark.django_db
def test_model_source_registers_index_by_class_name():
class VerboseIndex:
def __init__(self):
self._unique_id = uuid.uuid4()

def __repr__(self):
return f"verbose index representation {self._unique_id} " * 100

Book.objects.create(title="Book Title", description="Description")

source = ModelSource(model=Book)
source.post_index_update(VerboseIndex())
source.post_index_update(VerboseIndex())

registration = ModelSourceIndex.objects.get()
assert ModelSourceIndex.objects.count() == 1
assert registration.index_name == "VerboseIndex"
Loading