mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 08:26:21 +02:00
* Contract tests * Testing embeedings * Agent unit tests * Knowledge pipeline tests * Turn on contract tests
278 lines
No EOL
9.4 KiB
Python
278 lines
No EOL
9.4 KiB
Python
"""
|
|
Unit tests for embedding business logic
|
|
|
|
Tests the core embedding functionality without external dependencies,
|
|
focusing on data processing, validation, and business rules.
|
|
"""
|
|
|
|
import pytest
|
|
import numpy as np
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
class TestEmbeddingBusinessLogic:
|
|
"""Test embedding business logic and data processing"""
|
|
|
|
def test_embedding_vector_validation(self):
|
|
"""Test validation of embedding vectors"""
|
|
# Arrange
|
|
valid_vectors = [
|
|
[0.1, 0.2, 0.3],
|
|
[-0.5, 0.0, 0.8],
|
|
[], # Empty vector
|
|
[1.0] * 1536 # Large vector
|
|
]
|
|
|
|
invalid_vectors = [
|
|
None,
|
|
"not a vector",
|
|
[1, 2, "string"],
|
|
[[1, 2], [3, 4]] # Nested
|
|
]
|
|
|
|
# Act & Assert
|
|
def is_valid_vector(vec):
|
|
if not isinstance(vec, list):
|
|
return False
|
|
return all(isinstance(x, (int, float)) for x in vec)
|
|
|
|
for vec in valid_vectors:
|
|
assert is_valid_vector(vec), f"Should be valid: {vec}"
|
|
|
|
for vec in invalid_vectors:
|
|
assert not is_valid_vector(vec), f"Should be invalid: {vec}"
|
|
|
|
def test_dimension_consistency_check(self):
|
|
"""Test dimension consistency validation"""
|
|
# Arrange
|
|
same_dimension_vectors = [
|
|
[0.1, 0.2, 0.3, 0.4, 0.5],
|
|
[0.6, 0.7, 0.8, 0.9, 1.0],
|
|
[-0.1, -0.2, -0.3, -0.4, -0.5]
|
|
]
|
|
|
|
mixed_dimension_vectors = [
|
|
[0.1, 0.2, 0.3],
|
|
[0.4, 0.5, 0.6, 0.7],
|
|
[0.8, 0.9]
|
|
]
|
|
|
|
# Act
|
|
def check_dimension_consistency(vectors):
|
|
if not vectors:
|
|
return True
|
|
expected_dim = len(vectors[0])
|
|
return all(len(vec) == expected_dim for vec in vectors)
|
|
|
|
# Assert
|
|
assert check_dimension_consistency(same_dimension_vectors)
|
|
assert not check_dimension_consistency(mixed_dimension_vectors)
|
|
|
|
def test_text_preprocessing_logic(self):
|
|
"""Test text preprocessing for embeddings"""
|
|
# Arrange
|
|
test_cases = [
|
|
("Simple text", "Simple text"),
|
|
("", ""),
|
|
("Text with\nnewlines", "Text with\nnewlines"),
|
|
("Unicode: 世界 🌍", "Unicode: 世界 🌍"),
|
|
(" Whitespace ", " Whitespace ")
|
|
]
|
|
|
|
# Act & Assert
|
|
for input_text, expected in test_cases:
|
|
# Simple preprocessing (identity in this case)
|
|
processed = str(input_text) if input_text is not None else ""
|
|
assert processed == expected
|
|
|
|
def test_batch_processing_logic(self):
|
|
"""Test batch processing logic for multiple texts"""
|
|
# Arrange
|
|
texts = ["Text 1", "Text 2", "Text 3"]
|
|
|
|
def mock_embed_single(text):
|
|
# Simulate embedding generation based on text length
|
|
return [len(text) / 10.0] * 5
|
|
|
|
# Act
|
|
results = []
|
|
for text in texts:
|
|
embedding = mock_embed_single(text)
|
|
results.append((text, embedding))
|
|
|
|
# Assert
|
|
assert len(results) == len(texts)
|
|
for i, (original_text, embedding) in enumerate(results):
|
|
assert original_text == texts[i]
|
|
assert len(embedding) == 5
|
|
expected_value = len(texts[i]) / 10.0
|
|
assert all(abs(val - expected_value) < 0.001 for val in embedding)
|
|
|
|
def test_numpy_array_conversion_logic(self):
|
|
"""Test numpy array to list conversion"""
|
|
# Arrange
|
|
test_arrays = [
|
|
np.array([1, 2, 3], dtype=np.int32),
|
|
np.array([1.0, 2.0, 3.0], dtype=np.float64),
|
|
np.array([0.1, 0.2, 0.3], dtype=np.float32)
|
|
]
|
|
|
|
# Act
|
|
converted = []
|
|
for arr in test_arrays:
|
|
result = arr.tolist()
|
|
converted.append(result)
|
|
|
|
# Assert
|
|
assert converted[0] == [1, 2, 3]
|
|
assert converted[1] == [1.0, 2.0, 3.0]
|
|
# Float32 might have precision differences, so check approximately
|
|
assert len(converted[2]) == 3
|
|
assert all(isinstance(x, float) for x in converted[2])
|
|
|
|
def test_error_response_generation(self):
|
|
"""Test error response generation logic"""
|
|
# Arrange
|
|
error_scenarios = [
|
|
("model_not_found", "Model 'xyz' not found"),
|
|
("connection_error", "Failed to connect to service"),
|
|
("rate_limit", "Rate limit exceeded"),
|
|
("invalid_input", "Invalid input format")
|
|
]
|
|
|
|
# Act & Assert
|
|
for error_type, error_message in error_scenarios:
|
|
error_response = {
|
|
"error": {
|
|
"type": error_type,
|
|
"message": error_message
|
|
},
|
|
"vectors": None
|
|
}
|
|
|
|
assert error_response["error"]["type"] == error_type
|
|
assert error_response["error"]["message"] == error_message
|
|
assert error_response["vectors"] is None
|
|
|
|
def test_success_response_generation(self):
|
|
"""Test success response generation logic"""
|
|
# Arrange
|
|
test_vectors = [0.1, 0.2, 0.3, 0.4, 0.5]
|
|
|
|
# Act
|
|
success_response = {
|
|
"error": None,
|
|
"vectors": test_vectors
|
|
}
|
|
|
|
# Assert
|
|
assert success_response["error"] is None
|
|
assert success_response["vectors"] == test_vectors
|
|
assert len(success_response["vectors"]) == 5
|
|
|
|
def test_model_parameter_handling(self):
|
|
"""Test model parameter validation and handling"""
|
|
# Arrange
|
|
valid_models = {
|
|
"ollama": ["mxbai-embed-large", "nomic-embed-text"],
|
|
"fastembed": ["sentence-transformers/all-MiniLM-L6-v2", "BAAI/bge-small-en-v1.5"]
|
|
}
|
|
|
|
# Act & Assert
|
|
for provider, models in valid_models.items():
|
|
for model in models:
|
|
assert isinstance(model, str)
|
|
assert len(model) > 0
|
|
if provider == "fastembed":
|
|
assert "/" in model or "-" in model
|
|
|
|
def test_concurrent_processing_simulation(self):
|
|
"""Test concurrent processing simulation"""
|
|
# Arrange
|
|
import asyncio
|
|
|
|
async def mock_async_embed(text, delay=0.001):
|
|
await asyncio.sleep(delay)
|
|
return [ord(text[0]) / 255.0] if text else [0.0]
|
|
|
|
# Act
|
|
async def run_concurrent():
|
|
texts = ["A", "B", "C", "D", "E"]
|
|
tasks = [mock_async_embed(text) for text in texts]
|
|
results = await asyncio.gather(*tasks)
|
|
return list(zip(texts, results))
|
|
|
|
# Run test
|
|
results = asyncio.run(run_concurrent())
|
|
|
|
# Assert
|
|
assert len(results) == 5
|
|
for i, (text, embedding) in enumerate(results):
|
|
expected_char = chr(ord('A') + i)
|
|
assert text == expected_char
|
|
expected_value = ord(expected_char) / 255.0
|
|
assert abs(embedding[0] - expected_value) < 0.001
|
|
|
|
def test_empty_and_edge_cases(self):
|
|
"""Test empty inputs and edge cases"""
|
|
# Arrange
|
|
edge_cases = [
|
|
("", "empty string"),
|
|
(" ", "single space"),
|
|
("a", "single character"),
|
|
("A" * 10000, "very long string"),
|
|
("\\n\\t\\r", "special characters"),
|
|
("混合English中文", "mixed languages")
|
|
]
|
|
|
|
# Act & Assert
|
|
for text, description in edge_cases:
|
|
# Basic validation that text can be processed
|
|
assert isinstance(text, str), f"Failed for {description}"
|
|
assert len(text) >= 0, f"Failed for {description}"
|
|
|
|
# Simulate embedding generation would work
|
|
mock_embedding = [len(text) % 10] * 3
|
|
assert len(mock_embedding) == 3, f"Failed for {description}"
|
|
|
|
def test_vector_normalization_logic(self):
|
|
"""Test vector normalization calculations"""
|
|
# Arrange
|
|
test_vectors = [
|
|
[3.0, 4.0], # Should normalize to [0.6, 0.8]
|
|
[1.0, 0.0], # Should normalize to [1.0, 0.0]
|
|
[0.0, 0.0], # Zero vector edge case
|
|
]
|
|
|
|
# Act & Assert
|
|
for vector in test_vectors:
|
|
magnitude = sum(x**2 for x in vector) ** 0.5
|
|
|
|
if magnitude > 0:
|
|
normalized = [x / magnitude for x in vector]
|
|
# Check unit length (approximately)
|
|
norm_magnitude = sum(x**2 for x in normalized) ** 0.5
|
|
assert abs(norm_magnitude - 1.0) < 0.0001
|
|
else:
|
|
# Zero vector case
|
|
assert all(x == 0 for x in vector)
|
|
|
|
def test_cosine_similarity_calculation(self):
|
|
"""Test cosine similarity computation"""
|
|
# Arrange
|
|
vector_pairs = [
|
|
([1, 0], [0, 1], 0.0), # Orthogonal
|
|
([1, 0], [1, 0], 1.0), # Identical
|
|
([1, 1], [-1, -1], -1.0), # Opposite
|
|
]
|
|
|
|
# Act & Assert
|
|
def cosine_similarity(v1, v2):
|
|
dot = sum(a * b for a, b in zip(v1, v2))
|
|
mag1 = sum(x**2 for x in v1) ** 0.5
|
|
mag2 = sum(x**2 for x in v2) ** 0.5
|
|
return dot / (mag1 * mag2) if mag1 * mag2 > 0 else 0
|
|
|
|
for v1, v2, expected in vector_pairs:
|
|
similarity = cosine_similarity(v1, v2)
|
|
assert abs(similarity - expected) < 0.0001 |