test_create_chat_assistant.py


Overview

This file contains a comprehensive suite of automated tests for the chat assistant creation functionality in the InfiniFlow project. Using the pytest framework, these tests validate the behavior and robustness of the create_chat_assistant API endpoint, ensuring that chat assistants are created correctly according to various input parameters and constraints.

The tests cover:

This file plays a critical role in the continuous integration pipeline by preventing regressions in the chat assistant creation process.


Dependencies and Imports


Test Classes and Methods

1. TestAuthorization

Tests the authorization mechanism for creating chat assistants.

Method: test_invalid_auth

auth = RAGFlowHttpApiAuth(INVALID_API_TOKEN)
response = create_chat_assistant(auth)
assert response["code"] == 109
assert "API key is invalid" in response["message"]

2. TestChatAssistantCreate

Tests various aspects of the chat assistant creation process, including name validation, dataset ownership, avatar handling, LLM settings, and prompt parameters.

This class uses the clear_chat_assistants fixture to ensure a clean state before each test.

Method: test_name

payload = {"name": "valid_name", "dataset_ids": []}
res = create_chat_assistant(auth, payload)
assert res["code"] == 0
assert res["data"]["name"] == "valid_name"

Method: test_dataset_ids

payload = {"name": "assistant", "dataset_ids": ["invalid_dataset_id"]}
res = create_chat_assistant(auth, payload)
assert res["code"] == 102
assert "don't own the dataset" in res["message"]

Method: test_avatar

fn = create_image_file(tmp_path / "avatar.png")
payload = {"name": "avatar_test", "avatar": encode_avatar(fn), "dataset_ids": []}
res = create_chat_assistant(auth, payload)
assert res["code"] == 0

Method: test_llm

llm_config = {"model_name": "glm-4"}
payload = {"name": "llm_test", "dataset_ids": [dataset_id], "llm": llm_config}
res = create_chat_assistant(auth, payload)
assert res["code"] == 0
assert res["data"]["llm"]["model_name"] == "glm-4"

Method: test_prompt

prompt_config = {"prompt": "Hello World {knowledge}", "variables": []}
payload = {"name": "prompt_test", "dataset_ids": [dataset_id], "prompt": prompt_config}
res = create_chat_assistant(auth, payload)
assert res["code"] == 0
assert "knowledge" in res["data"]["prompt"]["prompt"]

3. TestChatAssistantCreate2

Additional tests focusing on document ownership validation in chat assistant creation.

Method: test_unparsed_document

payload = {"name": "prompt_test", "dataset_ids": [dataset_id]}
res = create_chat_assistant(auth, payload)
assert res["code"] == 102
assert "doesn't own parsed file" in res["message"]

Important Implementation Details


Interaction with Other System Components


Diagram: Class Structure and Main Methods

classDiagram
    class TestAuthorization {
        +test_invalid_auth(auth, expected_code, expected_message)
    }

    class TestChatAssistantCreate {
        +test_name(payload, expected_code, expected_message)
        +test_dataset_ids(dataset_ids, expected_code, expected_message)
        +test_avatar(get_http_api_auth, tmp_path)
        +test_llm(llm, expected_code, expected_message)
        +test_prompt(prompt, expected_code, expected_message)
    }

    class TestChatAssistantCreate2 {
        +test_unparsed_document(get_http_api_auth, add_document)
    }

Summary

The test_create_chat_assistant.py file is a critical quality assurance resource for the InfiniFlow project, ensuring that chat assistants are created reliably, securely, and correctly with valid parameters and proper authorization. It uses pytest capabilities to cover a wide spectrum of input scenarios, edge cases, and error handling, thereby supporting the robustness and maintainability of the chat assistant creation process in the system.