Files
wtismycode/wtismycode-core/tests/integration_detection.rs

138 lines
4.7 KiB
Rust

//! Integration detection tests for WTIsMyCode
//!
//! These tests verify that the integration detection functionality works correctly.
//! Integration detection now happens at module level during resolve_symbols,
//! based on actual imports rather than AST body inspection.
use std::fs;
use tempfile::TempDir;
use wtismycode_core::{Config, python_analyzer::PythonAnalyzer};
#[test]
fn test_http_integration_detection() {
let mut config = Config::default();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
config.project.root = temp_dir.path().to_string_lossy().to_string();
config.python.src_roots = vec![".".to_string()];
let analyzer = PythonAnalyzer::new(config);
let temp_file = temp_dir.path().join("test.py");
let python_code = r#"
import requests
def fetch_data():
response = requests.get("https://api.example.com/data")
return response.json()
"#;
fs::write(&temp_file, python_code).expect("Failed to write test file");
let parsed_module = analyzer.parse_module(&temp_file)
.expect("Failed to parse module");
let model = analyzer.resolve_symbols(&[parsed_module])
.expect("Failed to resolve symbols");
// Find the symbol (now prefixed with module id)
let symbol = model.symbols.values().find(|s| s.qualname == "fetch_data")
.expect("fetch_data symbol not found");
assert!(symbol.integrations_flags.http);
assert!(!symbol.integrations_flags.db);
assert!(!symbol.integrations_flags.queue);
}
#[test]
fn test_db_integration_detection() {
let mut config = Config::default();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
config.project.root = temp_dir.path().to_string_lossy().to_string();
config.python.src_roots = vec![".".to_string()];
let analyzer = PythonAnalyzer::new(config);
let temp_file = temp_dir.path().join("test.py");
let python_code = r#"
import sqlite3
def get_user(user_id):
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
return cursor.fetchone()
"#;
fs::write(&temp_file, python_code).expect("Failed to write test file");
let parsed_module = analyzer.parse_module(&temp_file)
.expect("Failed to parse module");
let model = analyzer.resolve_symbols(&[parsed_module])
.expect("Failed to resolve symbols");
let symbol = model.symbols.values().find(|s| s.qualname == "get_user")
.expect("get_user symbol not found");
assert!(!symbol.integrations_flags.http);
assert!(symbol.integrations_flags.db);
assert!(!symbol.integrations_flags.queue);
}
#[test]
fn test_queue_integration_detection() {
let mut config = Config::default();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
config.project.root = temp_dir.path().to_string_lossy().to_string();
config.python.src_roots = vec![".".to_string()];
let analyzer = PythonAnalyzer::new(config);
let temp_file = temp_dir.path().join("test.py");
let python_code = r#"
import redis
def process_job(job_data):
client = redis.Redis()
client.lpush("job_queue", job_data)
"#;
fs::write(&temp_file, python_code).expect("Failed to write test file");
let parsed_module = analyzer.parse_module(&temp_file)
.expect("Failed to parse module");
let model = analyzer.resolve_symbols(&[parsed_module])
.expect("Failed to resolve symbols");
let symbol = model.symbols.values().find(|s| s.qualname == "process_job")
.expect("process_job symbol not found");
assert!(!symbol.integrations_flags.http);
assert!(!symbol.integrations_flags.db);
assert!(symbol.integrations_flags.queue);
}
#[test]
fn test_no_integration_detection() {
let mut config = Config::default();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
config.project.root = temp_dir.path().to_string_lossy().to_string();
config.python.src_roots = vec![".".to_string()];
let analyzer = PythonAnalyzer::new(config);
let temp_file = temp_dir.path().join("test.py");
let python_code = r#"
def calculate_sum(a, b):
return a + b
"#;
fs::write(&temp_file, python_code).expect("Failed to write test file");
let parsed_module = analyzer.parse_module(&temp_file)
.expect("Failed to parse module");
let model = analyzer.resolve_symbols(&[parsed_module])
.expect("Failed to resolve symbols");
let symbol = model.symbols.values().find(|s| s.qualname == "calculate_sum")
.expect("calculate_sum symbol not found");
assert!(!symbol.integrations_flags.http);
assert!(!symbol.integrations_flags.db);
assert!(!symbol.integrations_flags.queue);
}