test_list_documents.py

Overview

This file contains comprehensive automated tests for the list_documnets API endpoint/functionality within the InfiniFlow system. The primary purpose is to verify the correctness, robustness, and expected behavior of the document listing feature under various scenarios, including authorization, pagination, filtering, sorting, and concurrency.

The tests are implemented using pytest, a popular Python testing framework, and cover both positive (successful cases) and negative (error handling) scenarios. The file is organized into two main test classes focusing on:

These tests help ensure the integrity and reliability of the document listing API, which is critical for users to access datasets and documents securely and efficiently.


Classes and Methods

TestAuthorization

This class contains tests related to API authentication and authorization when calling list_documnets.

test_invalid_auth(self, auth, expected_code, expected_message)

auth = RAGFlowHttpApiAuth(INVALID_API_TOKEN)
res = list_documnets(auth, "dataset_id")
assert res["code"] == 109
assert res["message"] == "Authentication error: API key is invalid!"

TestDocumentsList

This class contains detailed tests for various aspects of the document listing API, including pagination, filtering, sorting, concurrency, and parameter validation.


test_default(self, get_http_api_auth, add_documents)


test_invalid_dataset_id(self, get_http_api_auth, dataset_id, expected_code, expected_message)


test_page(self, get_http_api_auth, add_documents, params, expected_code, expected_page_size, expected_message)


test_page_size(self, get_http_api_auth, add_documents, params, expected_code, expected_page_size, expected_message)


test_orderby(self, get_http_api_auth, add_documents, params, expected_code, assertions, expected_message)


test_desc(self, get_http_api_auth, add_documents, params, expected_code, assertions, expected_message)


test_keywords(self, get_http_api_auth, add_documents, params, expected_num)


test_name(self, get_http_api_auth, add_documents, params, expected_code, expected_num, expected_message)


test_id(self, get_http_api_auth, add_documents, document_id, expected_code, expected_num, expected_message)


test_name_and_id(self, get_http_api_auth, add_documents, document_id, name, expected_code, expected_num, expected_message)


test_concurrent_list(self, get_http_api_auth, add_documents)


test_invalid_params(self, get_http_api_auth, add_documents)


Important Implementation Details


Interaction with Other Parts of the System

This test suite ensures the document listing API integrates correctly with authentication, dataset management, and supports client-side parameters properly.


Usage Examples

Example of a test that checks invalid authentication:

def test_invalid_auth():
    auth = RAGFlowHttpApiAuth(INVALID_API_TOKEN)
    res = list_documnets(auth, "dataset_id")
    assert res["code"] == 109
    assert res["message"] == "Authentication error: API key is invalid!"

Example of testing pagination:

def test_page(get_http_api_auth, add_documents):
    dataset_id, _ = add_documents
    params = {"page": 2, "page_size": 2}
    res = list_documnets(get_http_api_auth, dataset_id, params=params)
    assert res["code"] == 0
    assert len(res["data"]["docs"]) == 2

Mermaid Diagram - Class Structure

classDiagram
    class TestAuthorization {
        +test_invalid_auth(auth, expected_code, expected_message)
    }
    class TestDocumentsList {
        +test_default(get_http_api_auth, add_documents)
        +test_invalid_dataset_id(get_http_api_auth, dataset_id, expected_code, expected_message)
        +test_page(get_http_api_auth, add_documents, params, expected_code, expected_page_size, expected_message)
        +test_page_size(get_http_api_auth, add_documents, params, expected_code, expected_page_size, expected_message)
        +test_orderby(get_http_api_auth, add_documents, params, expected_code, assertions, expected_message)
        +test_desc(get_http_api_auth, add_documents, params, expected_code, assertions, expected_message)
        +test_keywords(get_http_api_auth, add_documents, params, expected_num)
        +test_name(get_http_api_auth, add_documents, params, expected_code, expected_num, expected_message)
        +test_id(get_http_api_auth, add_documents, document_id, expected_code, expected_num, expected_message)
        +test_name_and_id(get_http_api_auth, add_documents, document_id, name, expected_code, expected_num, expected_message)
        +test_concurrent_list(get_http_api_auth, add_documents)
        +test_invalid_params(get_http_api_auth, add_documents)
    }

Summary

test_list_documents.py is a critical test suite focusing on validating the document listing API endpoint in the InfiniFlow project. It covers:

By systematically exercising these features, this file ensures the document listing service is secure, reliable, and user-friendly, preventing regressions and aiding maintainability of the InfiniFlow backend API.