From 6afc37ed59fb3b6eea88d5cce282e48119e0bafe Mon Sep 17 00:00:00 2001 From: perf3ct Date: Sun, 22 Jun 2025 15:49:14 +0000 Subject: [PATCH] feat(tests): fix more broken tests, incorrect types --- tests/ocr_corruption_tests.rs | 10 +++++++--- tests/ocr_pipeline_integration_test.rs | 14 ++++++++------ tests/ocr_queue_management_tests.rs | 17 ++++++++++++++++- tests/webdav_integration_tests.rs | 18 ++++++++---------- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/tests/ocr_corruption_tests.rs b/tests/ocr_corruption_tests.rs index 0a0e555..fd2a036 100644 --- a/tests/ocr_corruption_tests.rs +++ b/tests/ocr_corruption_tests.rs @@ -178,9 +178,11 @@ impl OcrTestClient { async move { // Create multipart form + let part = reqwest::multipart::Part::text(content_owned.clone()) + .file_name(filename_owned.clone()) + .mime_str("text/plain")?; let form = reqwest::multipart::Form::new() - .text("file", content_owned.clone()) - .text("filename", filename_owned); + .part("file", part); let response = client .post(&format!("{}/api/documents", base_url)) @@ -457,7 +459,7 @@ Every document should retain its own unique signature and number. Any mixing of content between documents indicates corruption. Random data: {} End of Document {} -"#, i, i, timestamp, i*7, i, timestamp * i, i, i); +"#, i, i, i, timestamp, i*7, timestamp * i, i, i); documents.push((content, format!("doc_{}.txt", i))); } @@ -486,6 +488,8 @@ End of Document {} let actual_ocr_text = ocr_result["ocr_text"].as_str().unwrap_or(""); let filename = ocr_result["filename"].as_str().unwrap_or("unknown"); + println!("📝 OCR Text for {}: {}", filename, actual_ocr_text); + // Determine which document this should be based on filename if let Some(doc_num_str) = filename.strip_prefix("doc_").and_then(|s| s.strip_suffix(".txt")) { if let Ok(doc_num) = doc_num_str.parse::() { diff --git a/tests/ocr_pipeline_integration_test.rs b/tests/ocr_pipeline_integration_test.rs index 212c5e6..6729bf7 100644 --- a/tests/ocr_pipeline_integration_test.rs +++ b/tests/ocr_pipeline_integration_test.rs @@ -22,8 +22,6 @@ use readur::{ db_guardrails_simple::DocumentTransactionManager, }; -const TEST_DB_URL: &str = "postgresql://readur_user:readur_password@localhost:5432/readur"; - struct OCRPipelineTestHarness { db: Database, pool: PgPool, @@ -35,14 +33,18 @@ struct OCRPipelineTestHarness { impl OCRPipelineTestHarness { async fn new() -> Result { + let database_url = std::env::var("TEST_DATABASE_URL") + .or_else(|_| std::env::var("DATABASE_URL")) + .unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string()); + // Initialize database connection with higher limits for stress testing let pool = sqlx::postgres::PgPoolOptions::new() .max_connections(50) // Increased to support high concurrency tests .acquire_timeout(std::time::Duration::from_secs(10)) - .connect(TEST_DB_URL) + .connect(&database_url) .await?; - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&database_url).await?; // Initialize services let file_service = FileService::new("./test_uploads".to_string()); @@ -516,9 +518,9 @@ struct DocumentDetails { file_path: String, ocr_status: Option, ocr_text: Option, - ocr_confidence: Option, + ocr_confidence: Option, ocr_word_count: Option, - ocr_processing_time_ms: Option, + ocr_processing_time_ms: Option, ocr_error: Option, original_content: Option, } diff --git a/tests/ocr_queue_management_tests.rs b/tests/ocr_queue_management_tests.rs index 310708c..9c165ca 100644 --- a/tests/ocr_queue_management_tests.rs +++ b/tests/ocr_queue_management_tests.rs @@ -41,6 +41,17 @@ impl OCRQueueTestClient { /// Register and login a test user async fn register_and_login(&mut self, role: UserRole) -> Result> { + // First check if server is running + let health_check = self.client + .get(&format!("{}/api/health", get_base_url())) + .send() + .await; + + if let Err(e) = health_check { + eprintln!("Health check failed: {}. Is the server running at {}?", e, get_base_url()); + return Err(format!("Server not running: {}", e).into()); + } + let timestamp = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() @@ -64,7 +75,11 @@ impl OCRQueueTestClient { .await?; if !register_response.status().is_success() { - return Err(format!("Registration failed: {}", register_response.text().await?).into()); + let status = register_response.status(); + let text = register_response.text().await?; + eprintln!("Registration failed with status {}: {}", status, text); + eprintln!("Attempted to register user: {} with email: {}", username, email); + return Err(format!("Registration failed: {}", text).into()); } // Login to get token diff --git a/tests/webdav_integration_tests.rs b/tests/webdav_integration_tests.rs index 6f7b577..d37cd8e 100644 --- a/tests/webdav_integration_tests.rs +++ b/tests/webdav_integration_tests.rs @@ -16,8 +16,7 @@ use readur::{ AppState, }; -// Test database URL - in real tests you'd use a separate test database -const TEST_DATABASE_URL: &str = "postgres://postgres:password@localhost:5432/readur_test"; +// Removed constant - will use environment variables instead fn create_empty_update_settings() -> UpdateSettings { UpdateSettings { @@ -74,8 +73,12 @@ fn create_empty_update_settings() -> UpdateSettings { } async fn setup_test_app() -> (Router, Arc) { + let database_url = std::env::var("TEST_DATABASE_URL") + .or_else(|_| std::env::var("DATABASE_URL")) + .unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string()); + let config = Config { - database_url: TEST_DATABASE_URL.to_string(), + database_url: database_url.clone(), server_address: "127.0.0.1:0".to_string(), upload_path: "/tmp/test_uploads".to_string(), watch_folder: "/tmp/test_watch".to_string(), @@ -92,13 +95,8 @@ async fn setup_test_app() -> (Router, Arc) { ocr_timeout_seconds: 300, }; - // Try to connect to test database, fall back to regular database if not available - let db_url = if Database::new(TEST_DATABASE_URL).await.is_ok() { - TEST_DATABASE_URL.to_string() - } else { - std::env::var("DATABASE_URL").unwrap_or_else(|_| - "postgres://postgres:password@localhost:5432/readur".to_string()) - }; + // Use the environment-based database URL + let db_url = database_url; let db = Database::new(&db_url).await.expect("Failed to connect to test database"); let queue_service = Arc::new(readur::ocr_queue::OcrQueueService::new(db.clone(), db.pool.clone(), 2));