chore: Issue closure 125 cleanup
This commit is contained in:
210
capabilities/markitect-utils/tests/test_file_utils.py
Normal file
210
capabilities/markitect-utils/tests/test_file_utils.py
Normal file
@@ -0,0 +1,210 @@
|
||||
"""
|
||||
Test suite for file utility functions.
|
||||
"""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from markitect_utils.file_utils import (
|
||||
safe_filename,
|
||||
ensure_extension,
|
||||
get_file_size,
|
||||
is_text_file,
|
||||
normalize_path,
|
||||
)
|
||||
|
||||
|
||||
class TestSafeFilename:
|
||||
"""Test cases for the safe_filename function."""
|
||||
|
||||
def test_basic_sanitization(self):
|
||||
"""Test basic filename sanitization."""
|
||||
assert safe_filename("normal_file.txt") == "normal_file.txt"
|
||||
assert safe_filename("file with spaces.txt") == "file with spaces.txt"
|
||||
|
||||
def test_unsafe_characters(self):
|
||||
"""Test removal of unsafe characters."""
|
||||
assert safe_filename("file<>name.txt") == "file__name.txt"
|
||||
assert safe_filename('file"name.txt') == "file_name.txt"
|
||||
assert safe_filename("file/path\\name.txt") == "file_path_name.txt"
|
||||
assert safe_filename("file|name.txt") == "file_name.txt"
|
||||
|
||||
def test_custom_replacement(self):
|
||||
"""Test custom replacement character."""
|
||||
assert safe_filename("file<>name.txt", "-") == "file--name.txt"
|
||||
assert safe_filename("file/path\\name.txt", "") == "filepathname.txt"
|
||||
|
||||
def test_reserved_names(self):
|
||||
"""Test handling of Windows reserved names."""
|
||||
assert safe_filename("CON") == "file_CON"
|
||||
assert safe_filename("PRN.txt") == "file_PRN.txt"
|
||||
assert safe_filename("COM1") == "file_COM1"
|
||||
assert safe_filename("con") == "file_con" # case insensitive
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert safe_filename("") == "" # Empty input returns empty
|
||||
assert safe_filename(" ") == "file_" # Whitespace only gets prefix
|
||||
assert safe_filename("...") == "file_" # Dots only gets prefix
|
||||
assert safe_filename(".hidden") == "hidden" # Leading dot gets stripped
|
||||
|
||||
|
||||
class TestEnsureExtension:
|
||||
"""Test cases for the ensure_extension function."""
|
||||
|
||||
def test_add_extension(self):
|
||||
"""Test adding extension to filename."""
|
||||
assert ensure_extension("document", ".md") == "document.md"
|
||||
assert ensure_extension("file", "txt") == "file.txt"
|
||||
|
||||
def test_existing_extension(self):
|
||||
"""Test when extension already exists."""
|
||||
assert ensure_extension("document.md", ".md") == "document.md"
|
||||
assert ensure_extension("file.txt", "txt") == "file.txt"
|
||||
|
||||
def test_different_extension(self):
|
||||
"""Test adding extension when different one exists."""
|
||||
assert ensure_extension("document.txt", ".md") == "document.txt.md"
|
||||
assert ensure_extension("file.doc", "pdf") == "file.doc.pdf"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert ensure_extension("", ".md") == ""
|
||||
assert ensure_extension("file", "") == "file"
|
||||
assert ensure_extension("file.md", "") == "file.md"
|
||||
|
||||
|
||||
class TestGetFileSize:
|
||||
"""Test cases for the get_file_size function."""
|
||||
|
||||
def test_existing_file(self):
|
||||
"""Test getting size of existing file."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
|
||||
f.write("Hello, World!")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
size = get_file_size(temp_path)
|
||||
assert size is not None
|
||||
assert size > 0
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_nonexistent_file(self):
|
||||
"""Test getting size of non-existent file."""
|
||||
assert get_file_size("/path/that/does/not/exist") is None
|
||||
|
||||
def test_empty_file(self):
|
||||
"""Test getting size of empty file."""
|
||||
with tempfile.NamedTemporaryFile(delete=False) as f:
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
size = get_file_size(temp_path)
|
||||
assert size == 0
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_path_object(self):
|
||||
"""Test with Path object."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
|
||||
f.write("test content")
|
||||
temp_path = Path(f.name)
|
||||
|
||||
try:
|
||||
size = get_file_size(temp_path)
|
||||
assert size is not None
|
||||
assert size > 0
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
|
||||
class TestIsTextFile:
|
||||
"""Test cases for the is_text_file function."""
|
||||
|
||||
def test_text_file(self):
|
||||
"""Test with actual text file."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
|
||||
f.write("This is a text file with some content.")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
assert is_text_file(temp_path) is True
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_binary_file(self):
|
||||
"""Test with binary file."""
|
||||
with tempfile.NamedTemporaryFile(mode='wb', delete=False, suffix='.bin') as f:
|
||||
f.write(b'\x00\x01\x02\x03\x04\x05')
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
assert is_text_file(temp_path) is False
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_empty_file(self):
|
||||
"""Test with empty file."""
|
||||
with tempfile.NamedTemporaryFile(delete=False) as f:
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
assert is_text_file(temp_path) is True # Empty files are considered text
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_unicode_file(self):
|
||||
"""Test with Unicode text file."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as f:
|
||||
f.write("Hello 世界! This is UTF-8 text.")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
assert is_text_file(temp_path) is True
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_nonexistent_file(self):
|
||||
"""Test with non-existent file."""
|
||||
assert is_text_file("/path/that/does/not/exist") is False
|
||||
|
||||
|
||||
class TestNormalizePath:
|
||||
"""Test cases for the normalize_path function."""
|
||||
|
||||
def test_relative_path(self):
|
||||
"""Test normalizing relative paths."""
|
||||
# Note: These tests are environment-dependent
|
||||
result = normalize_path("./test")
|
||||
assert os.path.isabs(result)
|
||||
assert result.endswith("test")
|
||||
|
||||
def test_path_with_dots(self):
|
||||
"""Test path with dot components."""
|
||||
result = normalize_path("./dir/../file.txt")
|
||||
assert os.path.isabs(result)
|
||||
assert result.endswith("file.txt")
|
||||
|
||||
def test_already_absolute(self):
|
||||
"""Test already absolute path."""
|
||||
abs_path = "/tmp/test/file.txt"
|
||||
result = normalize_path(abs_path)
|
||||
assert result == abs_path
|
||||
|
||||
def test_path_object(self):
|
||||
"""Test with Path object."""
|
||||
path_obj = Path("./test/file.txt")
|
||||
result = normalize_path(path_obj)
|
||||
assert os.path.isabs(result)
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert normalize_path("") == ""
|
||||
|
||||
# Current directory should normalize to absolute path
|
||||
result = normalize_path(".")
|
||||
assert os.path.isabs(result)
|
||||
175
capabilities/markitect-utils/tests/test_integration.py
Normal file
175
capabilities/markitect-utils/tests/test_integration.py
Normal file
@@ -0,0 +1,175 @@
|
||||
"""
|
||||
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)
|
||||
149
capabilities/markitect-utils/tests/test_string_utils.py
Normal file
149
capabilities/markitect-utils/tests/test_string_utils.py
Normal file
@@ -0,0 +1,149 @@
|
||||
"""
|
||||
Test suite for string utility functions.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from markitect_utils.string_utils import (
|
||||
slugify,
|
||||
truncate,
|
||||
camel_to_snake,
|
||||
snake_to_camel,
|
||||
strip_ansi_codes,
|
||||
)
|
||||
|
||||
|
||||
class TestSlugify:
|
||||
"""Test cases for the slugify function."""
|
||||
|
||||
def test_basic_slugify(self):
|
||||
"""Test basic string to slug conversion."""
|
||||
assert slugify("Hello World") == "hello-world"
|
||||
assert slugify("My Great Article") == "my-great-article"
|
||||
|
||||
def test_special_characters(self):
|
||||
"""Test handling of special characters."""
|
||||
assert slugify("Hello World!") == "hello-world"
|
||||
assert slugify("Test@#$%^&*()_+") == "test" # underscore gets converted to separator
|
||||
assert slugify("Multiple---Dashes") == "multiple-dashes"
|
||||
|
||||
def test_custom_separator(self):
|
||||
"""Test custom separator."""
|
||||
assert slugify("Hello World", "_") == "hello_world"
|
||||
assert slugify("My Great Article", ".") == "my.great.article"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert slugify("") == ""
|
||||
assert slugify(" ") == ""
|
||||
assert slugify("---") == ""
|
||||
assert slugify("Single") == "single"
|
||||
|
||||
def test_unicode_handling(self):
|
||||
"""Test unicode character handling."""
|
||||
assert slugify("Café") == "cafe"
|
||||
assert slugify("naïve résumé") == "naive-resume"
|
||||
|
||||
|
||||
class TestTruncate:
|
||||
"""Test cases for the truncate function."""
|
||||
|
||||
def test_basic_truncation(self):
|
||||
"""Test basic string truncation."""
|
||||
text = "This is a long string that needs truncation"
|
||||
assert truncate(text, 20) == "This is a long st..."
|
||||
assert truncate(text, 10) == "This is..."
|
||||
|
||||
def test_no_truncation_needed(self):
|
||||
"""Test when no truncation is needed."""
|
||||
assert truncate("Short", 10) == "Short"
|
||||
assert truncate("Exact", 5) == "Exact"
|
||||
|
||||
def test_custom_suffix(self):
|
||||
"""Test custom suffix."""
|
||||
text = "Long text here"
|
||||
assert truncate(text, 10, "…") == "Long text…"
|
||||
assert truncate(text, 10, " [more]") == "Lon [more]"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert truncate("", 10) == ""
|
||||
assert truncate("Test", 3) == "..."
|
||||
assert truncate("Test", 2) == ".."
|
||||
assert truncate("Test", 1) == "."
|
||||
assert truncate("Test", 0) == ""
|
||||
|
||||
|
||||
class TestCamelToSnake:
|
||||
"""Test cases for the camel_to_snake function."""
|
||||
|
||||
def test_basic_conversion(self):
|
||||
"""Test basic camelCase to snake_case conversion."""
|
||||
assert camel_to_snake("camelCase") == "camel_case"
|
||||
assert camel_to_snake("PascalCase") == "pascal_case"
|
||||
assert camel_to_snake("simpleWord") == "simple_word"
|
||||
|
||||
def test_multiple_words(self):
|
||||
"""Test conversion with multiple words."""
|
||||
assert camel_to_snake("thisIsALongVariableName") == "this_is_a_long_variable_name"
|
||||
assert camel_to_snake("XMLHttpRequest") == "xml_http_request"
|
||||
assert camel_to_snake("JSONData") == "json_data"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert camel_to_snake("") == ""
|
||||
assert camel_to_snake("single") == "single"
|
||||
assert camel_to_snake("ALLCAPS") == "allcaps"
|
||||
assert camel_to_snake("A") == "a"
|
||||
|
||||
|
||||
class TestSnakeToCamel:
|
||||
"""Test cases for the snake_to_camel function."""
|
||||
|
||||
def test_basic_conversion(self):
|
||||
"""Test basic snake_case to camelCase conversion."""
|
||||
assert snake_to_camel("snake_case") == "snakeCase"
|
||||
assert snake_to_camel("simple_word") == "simpleWord"
|
||||
assert snake_to_camel("single") == "single"
|
||||
|
||||
def test_pascal_case(self):
|
||||
"""Test conversion to PascalCase."""
|
||||
assert snake_to_camel("snake_case", pascal_case=True) == "SnakeCase"
|
||||
assert snake_to_camel("simple_word", pascal_case=True) == "SimpleWord"
|
||||
assert snake_to_camel("single", pascal_case=True) == "Single"
|
||||
|
||||
def test_multiple_underscores(self):
|
||||
"""Test handling of multiple underscores."""
|
||||
assert snake_to_camel("this_is_a_long_name") == "thisIsALongName"
|
||||
assert snake_to_camel("this_is_a_long_name", pascal_case=True) == "ThisIsALongName"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert snake_to_camel("") == ""
|
||||
assert snake_to_camel("_") == ""
|
||||
assert snake_to_camel("__") == ""
|
||||
assert snake_to_camel("single") == "single"
|
||||
|
||||
|
||||
class TestStripAnsiCodes:
|
||||
"""Test cases for the strip_ansi_codes function."""
|
||||
|
||||
def test_basic_ansi_removal(self):
|
||||
"""Test basic ANSI code removal."""
|
||||
assert strip_ansi_codes("\033[31mRed text\033[0m") == "Red text"
|
||||
assert strip_ansi_codes("\033[32mGreen\033[0m text") == "Green text"
|
||||
|
||||
def test_complex_ansi_codes(self):
|
||||
"""Test complex ANSI escape sequences."""
|
||||
text_with_ansi = "\033[1;31;40mBold red on black\033[0m"
|
||||
assert strip_ansi_codes(text_with_ansi) == "Bold red on black"
|
||||
|
||||
def test_no_ansi_codes(self):
|
||||
"""Test text without ANSI codes."""
|
||||
plain_text = "Just plain text"
|
||||
assert strip_ansi_codes(plain_text) == plain_text
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert strip_ansi_codes("") == ""
|
||||
assert strip_ansi_codes("\033[0m") == ""
|
||||
assert strip_ansi_codes("Start\033[31mMiddle\033[0mEnd") == "StartMiddleEnd"
|
||||
215
capabilities/markitect-utils/tests/test_validation_utils.py
Normal file
215
capabilities/markitect-utils/tests/test_validation_utils.py
Normal file
@@ -0,0 +1,215 @@
|
||||
"""
|
||||
Test suite for validation utility functions.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from markitect_utils.validation_utils import (
|
||||
is_valid_email,
|
||||
is_valid_url,
|
||||
is_valid_semver,
|
||||
validate_required_fields,
|
||||
)
|
||||
|
||||
|
||||
class TestIsValidEmail:
|
||||
"""Test cases for the is_valid_email function."""
|
||||
|
||||
def test_valid_emails(self):
|
||||
"""Test valid email addresses."""
|
||||
valid_emails = [
|
||||
"user@example.com",
|
||||
"test.email@domain.co.uk",
|
||||
"user+tag@example.org",
|
||||
"user123@test-domain.com",
|
||||
"a@b.co",
|
||||
]
|
||||
for email in valid_emails:
|
||||
assert is_valid_email(email) is True, f"Failed for: {email}"
|
||||
|
||||
def test_invalid_emails(self):
|
||||
"""Test invalid email addresses."""
|
||||
invalid_emails = [
|
||||
"invalid.email",
|
||||
"@example.com",
|
||||
"user@",
|
||||
"user@.com",
|
||||
"user space@example.com",
|
||||
"user@domain",
|
||||
"user@@example.com",
|
||||
"",
|
||||
]
|
||||
for email in invalid_emails:
|
||||
assert is_valid_email(email) is False, f"Should be invalid: {email}"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert is_valid_email(None) is False
|
||||
assert is_valid_email(123) is False
|
||||
assert is_valid_email([]) is False
|
||||
|
||||
|
||||
class TestIsValidUrl:
|
||||
"""Test cases for the is_valid_url function."""
|
||||
|
||||
def test_valid_urls(self):
|
||||
"""Test valid URLs."""
|
||||
valid_urls = [
|
||||
"https://example.com",
|
||||
"http://test.org",
|
||||
"https://sub.domain.com/path",
|
||||
"http://localhost:8000",
|
||||
"https://example.com/path?query=value",
|
||||
"http://192.168.1.1:3000",
|
||||
]
|
||||
for url in valid_urls:
|
||||
assert is_valid_url(url) is True, f"Failed for: {url}"
|
||||
|
||||
def test_invalid_urls(self):
|
||||
"""Test invalid URLs."""
|
||||
invalid_urls = [
|
||||
"not-a-url",
|
||||
"ftp://example.com",
|
||||
"example.com",
|
||||
"://example.com",
|
||||
"https://",
|
||||
"http://.com",
|
||||
"",
|
||||
]
|
||||
for url in invalid_urls:
|
||||
assert is_valid_url(url) is False, f"Should be invalid: {url}"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert is_valid_url(None) is False
|
||||
assert is_valid_url(123) is False
|
||||
assert is_valid_url([]) is False
|
||||
|
||||
|
||||
class TestIsValidSemver:
|
||||
"""Test cases for the is_valid_semver function."""
|
||||
|
||||
def test_valid_versions(self):
|
||||
"""Test valid semantic versions."""
|
||||
valid_versions = [
|
||||
"1.0.0",
|
||||
"0.1.0",
|
||||
"10.20.30",
|
||||
"1.0.0-alpha",
|
||||
"1.0.0-alpha.1",
|
||||
"1.0.0-0.3.7",
|
||||
"1.0.0-x.7.z.92",
|
||||
"1.0.0+20130313144700",
|
||||
"1.0.0-beta+exp.sha.5114f85",
|
||||
]
|
||||
for version in valid_versions:
|
||||
assert is_valid_semver(version) is True, f"Failed for: {version}"
|
||||
|
||||
def test_invalid_versions(self):
|
||||
"""Test invalid semantic versions."""
|
||||
invalid_versions = [
|
||||
"1.0",
|
||||
"1.0.0-",
|
||||
"1.0.0+",
|
||||
"01.0.0",
|
||||
"1.01.0",
|
||||
"1.0.01",
|
||||
"1.0.0-",
|
||||
"1.0.0+",
|
||||
"v1.0.0",
|
||||
"",
|
||||
]
|
||||
for version in invalid_versions:
|
||||
assert is_valid_semver(version) is False, f"Should be invalid: {version}"
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
assert is_valid_semver(None) is False
|
||||
assert is_valid_semver(123) is False
|
||||
assert is_valid_semver([]) is False
|
||||
|
||||
|
||||
class TestValidateRequiredFields:
|
||||
"""Test cases for the validate_required_fields function."""
|
||||
|
||||
def test_all_fields_present(self):
|
||||
"""Test when all required fields are present and valid."""
|
||||
data = {
|
||||
"name": "John Doe",
|
||||
"email": "john@example.com",
|
||||
"age": 30
|
||||
}
|
||||
required = ["name", "email", "age"]
|
||||
result = validate_required_fields(data, required)
|
||||
|
||||
assert result == {"missing": [], "empty": []}
|
||||
|
||||
def test_missing_fields(self):
|
||||
"""Test when some fields are missing."""
|
||||
data = {
|
||||
"name": "John Doe",
|
||||
"email": "john@example.com"
|
||||
}
|
||||
required = ["name", "email", "age", "phone"]
|
||||
result = validate_required_fields(data, required)
|
||||
|
||||
assert set(result["missing"]) == {"age", "phone"}
|
||||
assert result["empty"] == []
|
||||
|
||||
def test_empty_fields(self):
|
||||
"""Test when some fields are empty."""
|
||||
data = {
|
||||
"name": "John Doe",
|
||||
"email": "",
|
||||
"age": 30,
|
||||
"phone": " " # whitespace only
|
||||
}
|
||||
required = ["name", "email", "age", "phone"]
|
||||
result = validate_required_fields(data, required)
|
||||
|
||||
assert result["missing"] == []
|
||||
assert set(result["empty"]) == {"email", "phone"}
|
||||
|
||||
def test_mixed_issues(self):
|
||||
"""Test when there are both missing and empty fields."""
|
||||
data = {
|
||||
"name": "John Doe",
|
||||
"email": "",
|
||||
}
|
||||
required = ["name", "email", "age", "phone"]
|
||||
result = validate_required_fields(data, required)
|
||||
|
||||
assert set(result["missing"]) == {"age", "phone"}
|
||||
assert result["empty"] == ["email"]
|
||||
|
||||
def test_non_string_values(self):
|
||||
"""Test with non-string values."""
|
||||
data = {
|
||||
"name": "John Doe",
|
||||
"age": 0, # Zero should not be considered empty
|
||||
"active": False, # False should not be considered empty
|
||||
"score": None, # None should be considered empty
|
||||
"items": [], # Empty list should be considered empty
|
||||
}
|
||||
required = ["name", "age", "active", "score", "items"]
|
||||
result = validate_required_fields(data, required)
|
||||
|
||||
assert result["missing"] == []
|
||||
assert set(result["empty"]) == {"score", "items"}
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test edge cases."""
|
||||
# Invalid inputs
|
||||
result = validate_required_fields("not a dict", ["field"])
|
||||
assert result == {"missing": [], "empty": []}
|
||||
|
||||
result = validate_required_fields({}, "not a list")
|
||||
assert result == {"missing": [], "empty": []}
|
||||
|
||||
# Empty data and requirements
|
||||
result = validate_required_fields({}, [])
|
||||
assert result == {"missing": [], "empty": []}
|
||||
|
||||
# Empty requirements
|
||||
data = {"name": "John"}
|
||||
result = validate_required_fields(data, [])
|
||||
assert result == {"missing": [], "empty": []}
|
||||
Reference in New Issue
Block a user