🔍 Code Extractor

function test_complex_url_hyperlink

Maturity: 45

A test function that validates the creation of Word documents with complex FileCloud URLs containing special characters, query parameters, and URL fragments as clickable hyperlinks.

File:
/tf/active/vicechatdev/test_complex_hyperlink.py
Lines:
8 - 48
Complexity:
moderate

Purpose

This function serves as an integration test to verify that the new_app.add_hyperlink_to_paragraph() function correctly handles complex FileCloud URLs with special characters (ampersands, commas, spaces, percent-encoding) and properly embeds them as clickable hyperlinks in Word documents. It creates a test document with a sample FileCloud URL to validate the hyperlink functionality.

Source Code

def test_complex_url_hyperlink():
    """Test Word document export with complex FileCloud URLs"""
    print("Testing Word export with complex FileCloud URL...")
    
    try:
        from docx import Document
        import logging
        
        # Set up logging to see what's happening
        logging.basicConfig(level=logging.INFO)
        logger = logging.getLogger(__name__)
        
        # Import the hyperlink function
        import new_app
        
        doc = Document()
        paragraph = doc.add_paragraph()
        
        # Test with the exact URL format from your example
        test_text = "3.5.1 Cost model for WBPK022&K024,K034_20240624.xlsx"
        test_url = "https://filecloud.vicebio.com/ui/core/index.html?filter=3.5.1+Cost+model+for+WBPK022&K024,K034_20240624.xlsx#expl-tabl./SHARED/vicebio_shares/Wuxi/3%20WO-CO%20&%20invoice%20plan/3.5%20Cost%20Model/"
        
        print(f"Link text: {test_text}")
        print(f"URL: {test_url}")
        print()
        
        # Add some context text
        paragraph.add_run("References\n[1]: ")
        
        # Add the hyperlink
        new_app.add_hyperlink_to_paragraph(paragraph, test_text, test_url)
        
        # Save test document
        doc.save('/tf/active/test_complex_hyperlink.docx')
        print("✅ Word document with complex hyperlink created successfully: test_complex_hyperlink.docx")
        print("\nPlease check if the hyperlink is now clickable and properly formatted.")
        
    except Exception as e:
        print(f"❌ Test failed: {e}")
        import traceback
        traceback.print_exc()

Return Value

This function does not return any value (implicitly returns None). It produces side effects by creating a Word document file at '/tf/active/test_complex_hyperlink.docx' and printing status messages to stdout.

Dependencies

  • python-docx
  • logging
  • new_app
  • urllib.parse
  • traceback
  • sys
  • re

Required Imports

import sys
import re
import logging
import urllib.parse
import traceback

Conditional/Optional Imports

These imports are only needed under specific conditions:

from docx import Document

Condition: required for Word document creation functionality

Required (conditional)
import new_app

Condition: required to access the add_hyperlink_to_paragraph function being tested

Required (conditional)

Usage Example

# Direct execution
test_complex_url_hyperlink()

# The function will:
# 1. Create a Word document with a complex FileCloud hyperlink
# 2. Save it to '/tf/active/test_complex_hyperlink.docx'
# 3. Print success/failure messages
# 4. Display the test URL and link text used

# Expected output:
# Testing Word export with complex FileCloud URL...
# Link text: 3.5.1 Cost model for WBPK022&K024,K034_20240624.xlsx
# URL: https://filecloud.vicebio.com/ui/core/index.html?filter=...
# ✅ Word document with complex hyperlink created successfully: test_complex_hyperlink.docx

Best Practices

  • This is a test function and should not be used in production code
  • Ensure the '/tf/active/' directory exists and is writable before running
  • The function uses hardcoded file paths which may need adjustment for different environments
  • The new_app module dependency must be properly configured with the add_hyperlink_to_paragraph function
  • Review the generated Word document manually to verify hyperlink functionality
  • The function includes comprehensive error handling with traceback printing for debugging
  • Consider parameterizing the output path and test URLs for more flexible testing

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_markdown_link_parsing 64.7% similar

    A test function that validates markdown link parsing capabilities, specifically testing extraction and URL encoding of complex URLs containing special characters from Quill editor format.

    From: /tf/active/vicechatdev/test_complex_hyperlink.py
  • function test_fixes 58.9% similar

    A comprehensive test function that validates email template rendering and CDocs application link presence in a document management system's email notification templates.

    From: /tf/active/vicechatdev/test_comprehensive_fixes.py
  • function test_upload_modalities 55.4% similar

    Integration test function that validates FileCloud upload functionality by testing both new file creation and existing file update scenarios.

    From: /tf/active/vicechatdev/SPFCsync/test_upload_modalities.py
  • function main_v4 51.1% similar

    A test function that validates email template rendering by testing multiple HTML email templates with sample data structures for document review and approval workflows.

    From: /tf/active/vicechatdev/test_comprehensive_templates.py
  • function test_document_extractor 50.7% similar

    A test function that validates the DocumentExtractor class by testing file type support detection, text extraction from various document formats, and error handling.

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