Files

175 lines
6.4 KiB
Python

"""
Integration tests for markitect-utils capability.
Tests the overall functionality and integration of the utility modules.
"""
import tempfile
import os
from pathlib import Path
import pytest
from markitect_utils import (
slugify, safe_filename, is_valid_email, validate_required_fields,
truncate, normalize_path
)
class TestUtilityIntegration:
"""Test integration between different utility functions."""
def test_filename_processing_workflow(self):
"""Test a complete filename processing workflow."""
# Start with user input
user_title = "My Great Article: A Case Study!"
user_email = "author@example.com"
# Validate email
assert is_valid_email(user_email) is True
# Create a slug for URL
slug = slugify(user_title)
assert slug == "my-great-article-a-case-study"
# Create a safe filename
filename = safe_filename(f"{slug}.md")
assert filename == "my-great-article-a-case-study.md"
# Truncate if too long
if len(filename) > 30:
filename = truncate(filename, 30, "….md")
assert len(filename) <= 30
assert filename.endswith(".md") or filename.endswith("….md")
def test_content_validation_workflow(self):
"""Test a content validation workflow."""
# Simulate form data
form_data = {
"title": "My Article",
"content": "This is the content of my article.",
"author_email": "author@example.com",
"category": "", # Empty field
"tags": "python,utils,testing"
}
required_fields = ["title", "content", "author_email", "category"]
# Validate required fields
validation_result = validate_required_fields(form_data, required_fields)
assert validation_result["missing"] == []
assert validation_result["empty"] == ["category"]
# Validate email format
if form_data.get("author_email"):
assert is_valid_email(form_data["author_email"]) is True
# Process tags
if form_data.get("tags"):
tag_list = [slugify(tag.strip()) for tag in form_data["tags"].split(",")]
assert tag_list == ["python", "utils", "testing"]
def test_file_operations_workflow(self):
"""Test a file operations workflow."""
# Create temporary directory for testing
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create some test files
test_content = "This is test content for the file."
# Process filename through multiple utilities
original_name = "Test File: With Special/Characters!"
safe_name = safe_filename(original_name)
slug_name = slugify(safe_name.rsplit('.', 1)[0]) + '.txt'
# Write file
file_path = temp_path / slug_name
file_path.write_text(test_content)
# Verify file exists and get normalized path
normalized_path = normalize_path(file_path)
assert Path(normalized_path).exists()
# Verify content length matches expectations
content_length = len(test_content)
assert content_length > 0
def test_data_processing_pipeline(self):
"""Test a complete data processing pipeline."""
# Raw data from external source
raw_data = [
{
"userName": "JohnDoe123",
"emailAddress": "john.doe@example.com",
"websiteURL": "https://johndoe.example.com",
"projectVersion": "1.2.0",
"description": "This is a very long description that might need to be truncated for display purposes in certain UI components."
},
{
"userName": "Jane_Smith",
"emailAddress": "invalid-email",
"websiteURL": "not-a-url",
"projectVersion": "invalid-version",
"description": "Short desc"
}
]
processed_data = []
for item in raw_data:
# Convert camelCase to snake_case (would need the function imported)
# For now, just demonstrate with available functions
processed_item = {
"slug": slugify(item["userName"]),
"email_valid": is_valid_email(item["emailAddress"]),
"short_desc": truncate(item["description"], 50),
"safe_filename": safe_filename(f"{item['userName']}_profile.json")
}
processed_data.append(processed_item)
# Verify processing results
assert processed_data[0]["slug"] == "johndoe123"
assert processed_data[0]["email_valid"] is True
assert len(processed_data[0]["short_desc"]) <= 50
assert processed_data[0]["safe_filename"] == "JohnDoe123_profile.json"
assert processed_data[1]["slug"] == "jane-smith"
assert processed_data[1]["email_valid"] is False
assert processed_data[1]["short_desc"] == "Short desc"
assert processed_data[1]["safe_filename"] == "Jane_Smith_profile.json"
def test_configuration_validation(self):
"""Test configuration validation using multiple utilities."""
# Simulate application configuration
config = {
"app_name": "My Application",
"version": "1.0.0",
"admin_email": "admin@myapp.com",
"base_url": "https://myapp.example.com",
"debug": True,
"secret_key": "", # Empty - should be flagged
}
required_fields = ["app_name", "version", "admin_email", "base_url", "secret_key"]
# Validate required fields
validation_result = validate_required_fields(config, required_fields)
assert validation_result["empty"] == ["secret_key"]
# Validate specific formats
email_valid = is_valid_email(config["admin_email"])
assert email_valid is True
# Create safe directory name from app name
app_slug = slugify(config["app_name"])
safe_dir_name = safe_filename(app_slug)
assert app_slug == "my-application"
assert safe_dir_name == "my-application"
# Validate version format would use is_valid_semver
# (assuming we had imported it in the integration test)