File size: 9,150 Bytes
b4856f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
"""
Test script for Vectorization Pipeline Integration
Tests that DataTransformation correctly invokes the Vectorizer Agent Graph
"""
import os
import sys
import json
import tempfile
import pandas as pd
import numpy as np
from datetime import datetime
from pathlib import Path
# Add project roots to path
PROJECT_ROOT = Path(__file__).parent
sys.path.insert(0, str(PROJECT_ROOT))
sys.path.insert(0, str(PROJECT_ROOT / "models" / "anomaly-detection"))
def create_test_data():
"""Create sample test data with multilingual content"""
data = [
{
"id": "test_001",
"text": "Heavy rainfall expected in Colombo district tomorrow. Residents advised to stay indoors.",
"source": "DMC",
"timestamp": datetime.now().isoformat(),
"engagement_score": 100
},
{
"id": "test_002",
"text": "Sinhala text sample for testing language detection",
"source": "twitter",
"timestamp": datetime.now().isoformat(),
"engagement_score": 50
},
{
"id": "test_003",
"text": "Tamil text sample for testing language detection",
"source": "facebook",
"timestamp": datetime.now().isoformat(),
"engagement_score": 75
},
{
"id": "test_004",
"text": "Stock market showing positive trends. Tourism sector recovering well after monsoon season.",
"source": "news",
"timestamp": datetime.now().isoformat(),
"engagement_score": 200
}
]
return pd.DataFrame(data)
def test_vectorizer_agent_graph_standalone():
"""Test 1: Verify vectorizer agent graph works independently"""
print("\n" + "="*60)
print("TEST 1: Vectorizer Agent Graph (Standalone)")
print("="*60)
try:
from src.graphs.vectorizationAgentGraph import graph as vectorization_graph
print("[OK] Vectorizer Agent Graph loaded successfully")
# Prepare test input
test_input = {
"input_texts": [
{"post_id": "test_001", "text": "Heavy rainfall in Colombo"},
{"post_id": "test_002", "text": "Sinhala test text"},
{"post_id": "test_003", "text": "Tamil test text"}
],
"batch_id": "test_standalone"
}
print(f" Input: {len(test_input['input_texts'])} texts")
# Invoke graph
result = vectorization_graph.invoke(test_input)
print(f" [OK] Graph executed successfully")
print(f" Keys in result: {list(result.keys())}")
# Check outputs
lang_results = result.get("language_detection_results", [])
embeddings = result.get("vector_embeddings", [])
expert_summary = result.get("expert_summary", "")
print(f" [OK] Language detection: {len(lang_results)} results")
print(f" [OK] Vector embeddings: {len(embeddings)} vectors")
print(f" [OK] Expert summary: {len(expert_summary)} chars")
# Show language distribution
if lang_results:
langs = [r.get("language", "unknown") for r in lang_results]
print(f" Languages detected: {set(langs)}")
return True
except Exception as e:
print(f"[FAIL] Error: {e}")
import traceback
traceback.print_exc()
return False
def test_data_transformation_with_agent():
"""Test 2: Verify DataTransformation integrates with agent graph"""
print("\n" + "="*60)
print("TEST 2: DataTransformation with Agent Graph")
print("="*60)
try:
# Import with correct path
sys.path.insert(0, str(PROJECT_ROOT / "models" / "anomaly-detection"))
from src.components import DataTransformation
from src.entity import DataTransformationConfig
# Create temp directory for outputs
with tempfile.TemporaryDirectory() as tmpdir:
# Create config
config = DataTransformationConfig()
config.output_directory = tmpdir
# Initialize with agent graph enabled
transformer = DataTransformation(config, use_agent_graph=True)
print(f" [OK] DataTransformation initialized")
print(f" Agent graph available: {transformer.vectorizer_graph is not None}")
# Create test data
df = create_test_data()
test_data_path = Path(tmpdir) / "test_data.parquet"
df.to_parquet(test_data_path, index=False)
print(f" [OK] Test data created: {len(df)} records")
# Run transformation
artifact = transformer.transform(str(test_data_path))
print(f" [OK] Transformation complete")
print(f" Total records: {artifact.total_records}")
print(f" Languages: {artifact.language_distribution}")
# Check if LLM insights were saved
insights_files = list(Path(tmpdir).glob("llm_insights_*.json"))
if insights_files:
print(f" [OK] LLM insights saved: {insights_files[0].name}")
with open(insights_files[0], "r", encoding="utf-8") as f:
insights = json.load(f)
print(f" Expert summary: {len(insights.get('expert_summary', ''))} chars")
print(f" Opportunities: {len(insights.get('opportunities', []))}")
print(f" Threats: {len(insights.get('threats', []))}")
else:
print(" [WARN] No LLM insights file found (agent graph may not have run)")
# Check embeddings
embeddings_files = list(Path(tmpdir).glob("embeddings_*.npy"))
if embeddings_files:
embeddings = np.load(embeddings_files[0])
print(f" [OK] Embeddings saved: shape {embeddings.shape}")
return True
except Exception as e:
print(f"[FAIL] Error: {e}")
import traceback
traceback.print_exc()
return False
def test_data_transformation_fallback():
"""Test 3: Verify fallback works when agent graph is disabled"""
print("\n" + "="*60)
print("TEST 3: DataTransformation Fallback (Agent Disabled)")
print("="*60)
try:
sys.path.insert(0, str(PROJECT_ROOT / "models" / "anomaly-detection"))
from src.components import DataTransformation
from src.entity import DataTransformationConfig
with tempfile.TemporaryDirectory() as tmpdir:
config = DataTransformationConfig()
config.output_directory = tmpdir
# Initialize with agent graph DISABLED
transformer = DataTransformation(config, use_agent_graph=False)
print(f" [OK] DataTransformation initialized (fallback mode)")
print(f" Agent graph: {transformer.vectorizer_graph}")
# Create test data
df = create_test_data()
test_data_path = Path(tmpdir) / "test_data.parquet"
df.to_parquet(test_data_path, index=False)
# Run transformation
artifact = transformer.transform(str(test_data_path))
print(f" [OK] Fallback transformation complete")
print(f" Total records: {artifact.total_records}")
# Verify no LLM insights (since agent was disabled)
insights_files = list(Path(tmpdir).glob("llm_insights_*.json"))
if not insights_files:
print(f" [OK] Correctly no LLM insights (agent disabled)")
else:
print(f" [WARN] Unexpected LLM insights file found")
return True
except Exception as e:
print(f"[FAIL] Error: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
print("\n" + "="*60)
print("VECTORIZATION PIPELINE INTEGRATION TESTS")
print("="*60)
results = []
# Run tests
results.append(("Vectorizer Agent Graph Standalone", test_vectorizer_agent_graph_standalone()))
results.append(("DataTransformation with Agent", test_data_transformation_with_agent()))
results.append(("DataTransformation Fallback", test_data_transformation_fallback()))
# Summary
print("\n" + "="*60)
print("TEST SUMMARY")
print("="*60)
passed = 0
for name, result in results:
status = "[PASS]" if result else "[FAIL]"
print(f" {status}: {name}")
if result:
passed += 1
print(f"\nTotal: {passed}/{len(results)} tests passed")
if passed == len(results):
print("\n[SUCCESS] All tests passed! Pipeline integration is working.")
else:
print("\n[WARNING] Some tests failed. Check the output above for details.")
|