Back to Repositories

Testing QuivrFile Object Creation and Metadata Management in QuivrHQ/quivr

This test suite validates the QuivrFile class functionality in the Quivr application, focusing on file creation and metadata handling. The tests ensure proper initialization of file objects and verify the correct storage of file attributes and metadata.

Test Coverage Overview

The test suite provides comprehensive coverage of the QuivrFile class instantiation and property validation.

Key functionality tested includes:
  • Basic file object creation with required attributes
  • UUID handling for file and brain identification
  • Path handling using pathlib.Path
  • File extension enumeration
  • Metadata dictionary storage and retrieval

Implementation Analysis

The testing approach utilizes pytest’s assertion framework to verify object properties after instantiation. The implementation follows a clear pattern of creating file objects with test data and validating their attributes individually.

The tests demonstrate proper handling of Python’s built-in UUID and Path objects, showing integration with standard library components.

Technical Details

Testing tools and components:
  • pytest as the testing framework
  • uuid4() for unique identifier generation
  • pathlib.Path for filesystem path handling
  • Custom FileExtension enumeration
  • QuivrFile class from core implementation

Best Practices Demonstrated

The test suite exhibits several testing best practices:

  • Isolated test cases with single responsibility
  • Clear test method naming conveying purpose
  • Proper separation of concerns between basic initialization and metadata handling
  • Use of appropriate data types and structures
  • Explicit assertion statements for clear validation

quivrhq/quivr

core/tests/test_quivr_file.py

            
from pathlib import Path
from uuid import uuid4

from quivr_core.files.file import FileExtension, QuivrFile


def test_create_file():
    id = uuid4()
    brain_id = uuid4()
    qfile = QuivrFile(
        id=id,
        brain_id=brain_id,
        original_filename="name",
        path=Path("/tmp/name"),
        file_extension=FileExtension.txt,
        file_sha1="123",
    )

    assert qfile.id == id
    assert qfile.brain_id == brain_id
    assert qfile.original_filename == "name"
    assert qfile.path == Path("/tmp/name")


def test_create_file_add_metadata():
    id = uuid4()
    brain_id = uuid4()
    qfile = QuivrFile(
        id=id,
        brain_id=brain_id,
        original_filename="name",
        path=Path("/tmp/name"),
        file_extension=FileExtension.txt,
        file_sha1="123",
        metadata={"other_id": "id"},
    )

    assert qfile.metadata["other_id"] == "id"