test_documents.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import json
  2. import uuid
  3. import pytest
  4. from core.base import (
  5. DocumentResponse,
  6. DocumentType,
  7. GraphExtractionStatus,
  8. IngestionStatus,
  9. )
  10. def make_db_entry(doc: DocumentResponse):
  11. # This simulates what your real code should do:
  12. return {
  13. "id":
  14. doc.id,
  15. "collection_ids":
  16. doc.collection_ids,
  17. "owner_id":
  18. doc.owner_id,
  19. "document_type":
  20. doc.document_type.value,
  21. "metadata":
  22. json.dumps(doc.metadata),
  23. "title":
  24. doc.title,
  25. "version":
  26. doc.version,
  27. "size_in_bytes":
  28. doc.size_in_bytes,
  29. "ingestion_status":
  30. doc.ingestion_status.value,
  31. "extraction_status":
  32. doc.extraction_status.value,
  33. "created_at":
  34. doc.created_at,
  35. "updated_at":
  36. doc.updated_at,
  37. "ingestion_attempt_number":
  38. 0,
  39. "summary":
  40. doc.summary,
  41. # If summary_embedding is a list, we can store it as a string here if needed
  42. "summary_embedding": (str(doc.summary_embedding)
  43. if doc.summary_embedding is not None else None),
  44. }
  45. @pytest.mark.asyncio
  46. async def test_upsert_documents_overview_insert(documents_handler):
  47. doc_id = uuid.uuid4()
  48. doc = DocumentResponse(
  49. id=doc_id,
  50. collection_ids=[],
  51. owner_id=uuid.uuid4(),
  52. document_type=DocumentType.TXT,
  53. metadata={"description": "A test document"},
  54. title="Test Doc",
  55. version="v1",
  56. size_in_bytes=1234,
  57. ingestion_status=IngestionStatus.PENDING,
  58. extraction_status=GraphExtractionStatus.PENDING,
  59. created_at=None,
  60. updated_at=None,
  61. summary=None,
  62. summary_embedding=None,
  63. )
  64. # Simulate the handler call
  65. await documents_handler.upsert_documents_overview(
  66. [doc]) # adjust your handler to accept list or doc
  67. # If your handler expects a db entry dict, you may need to patch handler or adapt your code
  68. # Verify
  69. res = await documents_handler.get_documents_overview(
  70. offset=0, limit=10, filter_document_ids=[doc_id])
  71. assert res["total_entries"] == 1
  72. fetched_doc = res["results"][0]
  73. assert fetched_doc.id == doc_id
  74. assert fetched_doc.title == "Test Doc"
  75. assert fetched_doc.metadata["description"] == "A test document"
  76. @pytest.mark.asyncio
  77. async def test_upsert_documents_overview_update(documents_handler):
  78. doc_id = uuid.uuid4()
  79. owner_id = uuid.uuid4()
  80. doc = DocumentResponse(
  81. id=doc_id,
  82. collection_ids=[],
  83. owner_id=owner_id,
  84. document_type=DocumentType.TXT,
  85. metadata={"note": "initial"},
  86. title="Initial Title",
  87. version="v1",
  88. size_in_bytes=100,
  89. ingestion_status=IngestionStatus.PENDING,
  90. extraction_status=GraphExtractionStatus.PENDING,
  91. created_at=None,
  92. updated_at=None,
  93. summary=None,
  94. summary_embedding=None,
  95. )
  96. await documents_handler.upsert_documents_overview([doc])
  97. # Update document
  98. doc.title = "Updated Title"
  99. doc.metadata["note"] = "updated"
  100. await documents_handler.upsert_documents_overview([doc])
  101. # Verify update
  102. res = await documents_handler.get_documents_overview(
  103. offset=0, limit=10, filter_document_ids=[doc_id])
  104. fetched_doc = res["results"][0]
  105. assert fetched_doc.title == "Updated Title"
  106. assert fetched_doc.metadata["note"] == "updated"
  107. @pytest.mark.asyncio
  108. async def test_delete_document(documents_handler):
  109. doc_id = uuid.uuid4()
  110. doc = DocumentResponse(
  111. id=doc_id,
  112. collection_ids=[],
  113. owner_id=uuid.uuid4(),
  114. document_type=DocumentType.TXT,
  115. metadata={},
  116. title="ToDelete",
  117. version="v1",
  118. size_in_bytes=100,
  119. ingestion_status=IngestionStatus.PENDING,
  120. extraction_status=GraphExtractionStatus.PENDING,
  121. created_at=None,
  122. updated_at=None,
  123. summary=None,
  124. summary_embedding=None,
  125. )
  126. await documents_handler.upsert_documents_overview([doc])
  127. await documents_handler.delete(doc_id)
  128. res = await documents_handler.get_documents_overview(
  129. offset=0, limit=10, filter_document_ids=[doc_id])
  130. assert res["total_entries"] == 0