function test_complex_url_hyperlink
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.
/tf/active/vicechatdev/test_complex_hyperlink.py
8 - 48
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-docxloggingnew_appurllib.parsetracebacksysre
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 and its add_hyperlink_to_paragraph function must be properly implemented
- 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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_markdown_link_parsing 64.7% similar
-
function add_hyperlink_to_paragraph 60.5% similar
-
function test_fixes 58.7% similar
-
function test_filecloud_connection 56.9% similar
-
function test_upload_modalities 55.4% similar