test_list_documents.py

Overview

test_list_documents.py is a comprehensive test suite designed to validate the functionality, robustness, and security of the list_documents API endpoint within the InfiniFlow system. This endpoint retrieves documents from a specified knowledge base (KB) and supports features such as pagination, sorting, filtering by keywords, and authorization checks.

The file uses the pytest testing framework to organize and run tests. It covers positive test cases (valid inputs and expected results) as well as negative test cases (invalid inputs, unauthorized access) to ensure the endpoint behaves correctly under various scenarios.

Key functionalities tested include:


Detailed Explanation of Classes and Methods

Imports and Dependencies


Class: TestAuthorization

Tests authorization and authentication for the list_documents API.

Method: test_invalid_auth

res = list_documents(None, {"kb_id": "dataset_id"})
assert res["code"] == 401
assert res["message"] == "<Unauthorized '401: Unauthorized'>"

Class: TestDocumentsList

Contains tests validating the document listing functionality with various parameters and edge cases.

Method: test_default

kb_id, _ = add_documents
res = list_documents(WebApiAuth, {"kb_id": kb_id})
assert res["code"] == 0
assert len(res["data"]["docs"]) == 5
assert res["data"]["total"] == 5

Method: test_invalid_dataset_id


Method: test_page

res = list_documents(WebApiAuth, {"kb_id": kb_id, "page": 2, "page_size": 2})
assert len(res["data"]["docs"]) == 2

Method: test_page_size


Method: test_orderby

res = list_documents(WebApiAuth, {"kb_id": kb_id, "orderby": "create_time"})
assert is_sorted(res["data"]["docs"], "create_time", True)

Method: test_desc


Method: test_keywords


Method: test_concurrent_list


Important Implementation Details


Interaction with Other System Components

This file primarily tests the interface and contract of the list_documents API endpoint and ensures that any changes in backend logic or parameters will be caught by these automated tests before deployment.


Visual Diagram

classDiagram
    class TestAuthorization {
        +test_invalid_auth(invalid_auth, expected_code, expected_message)
    }
    class TestDocumentsList {
        +test_default(WebApiAuth, add_documents)
        +test_invalid_dataset_id(WebApiAuth, kb_id, expected_code, expected_message)
        +test_page(WebApiAuth, add_documents, params, expected_code, expected_page_size, expected_message)
        +test_page_size(WebApiAuth, add_documents, params, expected_code, expected_page_size, expected_message)
        +test_orderby(WebApiAuth, add_documents, params, expected_code, assertions, expected_message)
        +test_desc(WebApiAuth, add_documents, params, expected_code, assertions, expected_message)
        +test_keywords(WebApiAuth, add_documents, params, expected_num)
        +test_concurrent_list(WebApiAuth, add_documents)
    }
    TestAuthorization ..> list_documents : calls
    TestDocumentsList ..> list_documents : calls
    TestAuthorization ..> RAGFlowWebApiAuth : uses
    TestDocumentsList ..> is_sorted : uses

Summary

test_list_documents.py is a critical testing module for validating the document listing API in InfiniFlow, covering authorization, input validation, pagination, sorting, keyword filtering, and concurrency. It uses parametrized pytest tests and utility functions to ensure robust verification and prevent regressions in document retrieval functionality. The tests interact primarily with list_documents API and authentication utilities and depend on external fixtures for setup.