test_strict_validation.pyโข3.18 kB
#!/usr/bin/env python3
"""
Test script to verify strict schema validation is working properly.
"""
import sys
import os
import importlib.util
# Import document_schema directly from the file
spec = importlib.util.spec_from_file_location("document_schema",
os.path.join(os.path.dirname(__file__), '..', 'src', 'document_schema.py'))
document_schema = importlib.util.module_from_spec(spec)
spec.loader.exec_module(document_schema)
validate_document_structure = document_schema.validate_document_structure
DocumentValidationError = document_schema.DocumentValidationError
import json
def test_strict_validation():
"""Test strict validation functionality."""
print("๐งช Testing strict schema validation...")
# Test 1: Valid knowledge base document
print("\n๐ Test 1: Valid knowledge base document")
valid_doc = {
"id": "test-doc-1",
"title": "Test Document",
"summary": "A test document",
"file_path": "test.md",
"file_name": "test.md",
"directory": "",
"priority": "medium",
"tags": ["test"],
"source_type": "markdown",
"last_modified": "2025-01-04T10:30:00Z",
"key_points": ["Point 1", "Point 2"],
"related": []
}
try:
result = validate_document_structure(valid_doc, is_knowledge_doc=True)
print("โ
Valid document passed validation")
except DocumentValidationError as e:
print(f"โ Valid document failed: {e}")
# Test 2: Knowledge base document with extra fields (should fail if strict mode enabled)
print("\n๐ Test 2: Knowledge base document with extra fields")
doc_with_extra = valid_doc.copy()
doc_with_extra["extra_field"] = "This should not be allowed"
doc_with_extra["another_extra"] = "Neither should this"
try:
result = validate_document_structure(doc_with_extra, is_knowledge_doc=True)
print("โ ๏ธ Document with extra fields passed (strict mode may be disabled)")
except DocumentValidationError as e:
print(f"โ
Document with extra fields correctly rejected: {e}")
# Test 3: Non-knowledge document
print("\n๐ Test 3: Non-knowledge document")
custom_doc = {
"name": "Custom Document",
"data": {"key": "value"},
"timestamp": "2025-01-04"
}
try:
result = validate_document_structure(custom_doc, is_knowledge_doc=False)
print("โ
Custom document passed validation")
except DocumentValidationError as e:
print(f"โ Custom document failed: {e}")
# Test 4: Missing required fields
print("\n๐ Test 4: Knowledge base document missing required fields")
incomplete_doc = {
"id": "incomplete-doc",
"title": "Incomplete Document"
# Missing other required fields
}
try:
result = validate_document_structure(incomplete_doc, is_knowledge_doc=True)
print("โ ๏ธ Incomplete document passed (validation may be lenient)")
except DocumentValidationError as e:
print(f"โ
Incomplete document correctly rejected: {e}")
if __name__ == "__main__":
test_strict_validation()