🔍 Code Extractor

function test_flask_routes

Maturity: 44

A test function that validates Flask application routes are properly configured by checking for required endpoints.

File:
/tf/active/vicechatdev/docchat/test_model_selection.py
Lines:
85 - 107
Complexity:
simple

Purpose

This function is part of a test suite that verifies the Flask web application has been correctly set up with essential routes. It dynamically imports the Flask app module, extracts all registered URL routes, and asserts that critical endpoints (root, chat API, and models API) exist. The function includes error handling to gracefully skip the test if the Flask app cannot be initialized (e.g., due to ChromaDB accessibility issues).

Source Code

def test_flask_routes():
    """Test Flask routes are properly configured"""
    print("=" * 60)
    print("TEST 3: Flask Application Routes")
    print("=" * 60)
    
    # Import app after other tests to avoid initialization errors
    try:
        import app as flask_app
        
        # Check if app has the routes we need
        routes = [rule.rule for rule in flask_app.app.url_map.iter_rules()]
        
        required_routes = ['/', '/api/chat', '/api/models']
        for route in required_routes:
            assert route in routes, f"Missing route: {route}"
            print(f"✓ Route exists: {route}")
        
        print()
    except Exception as e:
        print(f"⚠️  Flask app test skipped due to initialization error: {e}")
        print("   (This is expected if ChromaDB is not accessible)")
        print()

Return Value

This function does not return any value (implicitly returns None). It performs assertions and prints test results to stdout. If assertions fail, it will raise an AssertionError. If the Flask app cannot be imported, it prints a warning message and continues without raising an error.

Dependencies

  • flask
  • chromadb

Required Imports

import sys
import json
import config
from rag_engine import get_llm_instance

Conditional/Optional Imports

These imports are only needed under specific conditions:

import app as flask_app

Condition: imported inside the function within a try-except block to handle initialization errors gracefully

Required (conditional)

Usage Example

# Run the test function directly
test_flask_routes()

# Expected output on success:
# ============================================================
# TEST 3: Flask Application Routes
# ============================================================
# ✓ Route exists: /
# ✓ Route exists: /api/chat
# ✓ Route exists: /api/models
#

# Expected output on failure:
# ============================================================
# TEST 3: Flask Application Routes
# ============================================================
# ⚠️  Flask app test skipped due to initialization error: [error message]
#    (This is expected if ChromaDB is not accessible)
#

Best Practices

  • This function should be run as part of a test suite, not in production code
  • The function uses lazy import of the Flask app to avoid initialization errors in other tests
  • Error handling is intentionally permissive to allow test suite to continue even if Flask app cannot initialize
  • The function assumes specific route names - update required_routes list if API endpoints change
  • Print statements are used for test output rather than logging framework
  • Consider using pytest or unittest framework for more robust test management
  • The function performs side effects (printing) rather than returning test results, making it less suitable for automated test runners

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_upload 60.4% similar

    Flask route handler that serves a static HTML test page for debugging multiple file upload functionality.

    From: /tf/active/vicechatdev/leexi/app.py
  • function health_v1 60.3% similar

    Flask route handler that provides a health check endpoint returning the operational status of the application and its core components (RAG engine and document indexer).

    From: /tf/active/vicechatdev/docchat/app.py
  • function test_multiple_file_upload 60.1% similar

    A test function that validates multiple file upload functionality to a Flask application endpoint by sending a transcript file and multiple previous report files.

    From: /tf/active/vicechatdev/leexi/test_flask_upload.py
  • function index_v3 59.3% similar

    Flask route handler that serves as the application's main entry point, redirecting users to either the chat page if authenticated or the login page if not.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function health_check 59.1% similar

    A Flask route handler that provides a health check endpoint returning the application's status and current timestamp.

    From: /tf/active/vicechatdev/leexi/app.py
← Back to Browse