86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Property-based tests for placeholder extraction completeness.
|
|
|
|
This module contains property-based tests using Hypothesis to verify that
|
|
the CertificateTemplateParser correctly extracts all placeholders from
|
|
DOCX templates across a wide range of randomly generated inputs.
|
|
"""
|
|
|
|
import unittest
|
|
from hypothesis import given, settings, HealthCheck
|
|
|
|
from odoo.addons.survey_custom_certificate_template.services.certificate_template_parser import (
|
|
CertificateTemplateParser,
|
|
)
|
|
from odoo.addons.survey_custom_certificate_template.tests.hypothesis_strategies import (
|
|
docx_with_placeholders,
|
|
)
|
|
|
|
|
|
class TestPropertyPlaceholderExtraction(unittest.TestCase):
|
|
"""
|
|
Property-based tests for placeholder extraction from DOCX templates.
|
|
|
|
These tests verify correctness properties that should hold across all
|
|
valid inputs, using randomly generated test cases.
|
|
"""
|
|
|
|
def setUp(self):
|
|
"""Set up test fixtures"""
|
|
self.parser = CertificateTemplateParser()
|
|
|
|
@given(docx_data=docx_with_placeholders(min_placeholders=1, max_placeholders=10))
|
|
@settings(
|
|
max_examples=100,
|
|
deadline=None,
|
|
suppress_health_check=[HealthCheck.function_scoped_fixture]
|
|
)
|
|
def test_property_3_placeholder_extraction_completeness(self, docx_data):
|
|
"""
|
|
Feature: survey-custom-certificate-template, Property 3: Placeholder extraction completeness
|
|
|
|
For any DOCX template containing placeholders matching the {key.*} pattern,
|
|
the Template Parser should extract and return all such placeholders without omission.
|
|
|
|
Validates: Requirements 2.1
|
|
|
|
This property test verifies that:
|
|
1. All placeholders present in the generated DOCX are extracted
|
|
2. No placeholders are missed during parsing
|
|
3. The extraction is complete regardless of document structure
|
|
"""
|
|
docx_binary, expected_placeholders = docx_data
|
|
|
|
# Parse the template to extract placeholders
|
|
extracted_placeholders = self.parser.parse_template(docx_binary)
|
|
|
|
# Convert to sets for comparison (order doesn't matter)
|
|
expected_set = set(expected_placeholders)
|
|
extracted_set = set(extracted_placeholders)
|
|
|
|
# Property: All expected placeholders should be extracted
|
|
self.assertEqual(
|
|
expected_set,
|
|
extracted_set,
|
|
msg=f"Placeholder extraction incomplete. "
|
|
f"Expected: {expected_set}, "
|
|
f"Extracted: {extracted_set}, "
|
|
f"Missing: {expected_set - extracted_set}, "
|
|
f"Extra: {extracted_set - expected_set}"
|
|
)
|
|
|
|
# Additional assertion: The count should match
|
|
self.assertEqual(
|
|
len(expected_placeholders),
|
|
len(extracted_placeholders),
|
|
msg=f"Placeholder count mismatch. "
|
|
f"Expected {len(expected_placeholders)} unique placeholders, "
|
|
f"but extracted {len(extracted_placeholders)}"
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|