| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- """
- 向量搜索命令单元测试
- 测试向量搜索应用层的命令定义和验证逻辑。
- """
- import pytest
- from src.application.vector_search.commands import (
- CreateDocumentCommand,
- UpdateDocumentCommand,
- DeleteDocumentCommand,
- )
- class TestCreateDocumentCommand:
- """CreateDocumentCommand 单元测试"""
-
- def test_create_with_valid_content(self):
- """测试使用有效内容创建命令"""
- # Act
- command = CreateDocumentCommand(
- content="This is a test document",
- metadata={"source": "test"}
- )
-
- # Assert
- assert command.content == "This is a test document"
- assert command.metadata == {"source": "test"}
-
- def test_create_with_default_metadata(self):
- """测试使用默认元数据创建命令"""
- # Act
- command = CreateDocumentCommand(content="Test content")
-
- # Assert
- assert command.content == "Test content"
- assert command.metadata == {}
-
- def test_create_with_empty_content_raises_error(self):
- """测试使用空内容创建命令会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document content cannot be empty"):
- CreateDocumentCommand(content="")
-
- def test_create_with_whitespace_only_content_raises_error(self):
- """测试使用仅空白字符的内容创建命令会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document content cannot be whitespace only"):
- CreateDocumentCommand(content=" \n\t ")
-
- def test_create_with_non_string_content_raises_error(self):
- """测试使用非字符串内容创建命令会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document content must be a string"):
- CreateDocumentCommand(content=123) # type: ignore
-
- def test_create_with_non_dict_metadata_raises_error(self):
- """测试使用非字典元数据创建命令会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document metadata must be a dictionary"):
- CreateDocumentCommand(
- content="Test content",
- metadata="invalid" # type: ignore
- )
- class TestUpdateDocumentCommand:
- """UpdateDocumentCommand 单元测试"""
-
- def test_update_with_content_only(self):
- """测试只更新内容"""
- # Act
- command = UpdateDocumentCommand(
- document_id="doc_123",
- content="Updated content"
- )
-
- # Assert
- assert command.document_id == "doc_123"
- assert command.content == "Updated content"
- assert command.metadata is None
- assert command.merge_metadata is True
-
- def test_update_with_metadata_only(self):
- """测试只更新元数据"""
- # Act
- command = UpdateDocumentCommand(
- document_id="doc_123",
- metadata={"version": "2.0"}
- )
-
- # Assert
- assert command.document_id == "doc_123"
- assert command.content is None
- assert command.metadata == {"version": "2.0"}
-
- def test_update_with_content_and_metadata(self):
- """测试同时更新内容和元数据"""
- # Act
- command = UpdateDocumentCommand(
- document_id="doc_123",
- content="Updated content",
- metadata={"version": "2.0"},
- merge_metadata=False
- )
-
- # Assert
- assert command.document_id == "doc_123"
- assert command.content == "Updated content"
- assert command.metadata == {"version": "2.0"}
- assert command.merge_metadata is False
-
- def test_update_with_empty_document_id_raises_error(self):
- """测试使用空文档 ID 更新会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document ID cannot be empty"):
- UpdateDocumentCommand(
- document_id="",
- content="Updated content"
- )
-
- def test_update_with_no_content_and_no_metadata_raises_error(self):
- """测试不提供内容和元数据会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="At least one of content or metadata must be provided"):
- UpdateDocumentCommand(document_id="doc_123")
-
- def test_update_with_whitespace_only_content_raises_error(self):
- """测试使用仅空白字符的内容更新会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document content cannot be whitespace only"):
- UpdateDocumentCommand(
- document_id="doc_123",
- content=" \n\t "
- )
-
- def test_update_with_non_string_content_raises_error(self):
- """测试使用非字符串内容更新会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document content must be a string"):
- UpdateDocumentCommand(
- document_id="doc_123",
- content=123 # type: ignore
- )
-
- def test_update_with_non_dict_metadata_raises_error(self):
- """测试使用非字典元数据更新会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document metadata must be a dictionary"):
- UpdateDocumentCommand(
- document_id="doc_123",
- metadata="invalid" # type: ignore
- )
-
- def test_has_content_update_returns_true_when_content_provided(self):
- """测试当提供内容时 has_content_update 返回 True"""
- # Arrange
- command = UpdateDocumentCommand(
- document_id="doc_123",
- content="Updated content"
- )
-
- # Act & Assert
- assert command.has_content_update() is True
-
- def test_has_content_update_returns_false_when_content_not_provided(self):
- """测试当不提供内容时 has_content_update 返回 False"""
- # Arrange
- command = UpdateDocumentCommand(
- document_id="doc_123",
- metadata={"version": "2.0"}
- )
-
- # Act & Assert
- assert command.has_content_update() is False
-
- def test_has_metadata_update_returns_true_when_metadata_provided(self):
- """测试当提供元数据时 has_metadata_update 返回 True"""
- # Arrange
- command = UpdateDocumentCommand(
- document_id="doc_123",
- metadata={"version": "2.0"}
- )
-
- # Act & Assert
- assert command.has_metadata_update() is True
-
- def test_has_metadata_update_returns_false_when_metadata_not_provided(self):
- """测试当不提供元数据时 has_metadata_update 返回 False"""
- # Arrange
- command = UpdateDocumentCommand(
- document_id="doc_123",
- content="Updated content"
- )
-
- # Act & Assert
- assert command.has_metadata_update() is False
- class TestDeleteDocumentCommand:
- """DeleteDocumentCommand 单元测试"""
-
- def test_delete_with_valid_document_id(self):
- """测试使用有效文档 ID 创建删除命令"""
- # Act
- command = DeleteDocumentCommand(document_id="doc_123")
-
- # Assert
- assert command.document_id == "doc_123"
-
- def test_delete_with_empty_document_id_raises_error(self):
- """测试使用空文档 ID 删除会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document ID cannot be empty"):
- DeleteDocumentCommand(document_id="")
-
- def test_delete_with_non_string_document_id_raises_error(self):
- """测试使用非字符串文档 ID 删除会抛出错误"""
- # Act & Assert
- with pytest.raises(ValueError, match="Document ID must be a string"):
- DeleteDocumentCommand(document_id=123) # type: ignore
|