survey_custom_certificate_t.../tests/test_template_update_deletion.py
2025-11-29 08:46:04 +07:00

166 lines
7.0 KiB
Python

# -*- coding: utf-8 -*-
import json
import base64
import logging
from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
class TestTemplateUpdateDeletion(TransactionCase):
"""Test template update and deletion functionality."""
def setUp(self):
super(TestTemplateUpdateDeletion, self).setUp()
# Create a test survey
self.survey = self.env['survey.survey'].create({
'title': 'Test Survey for Template Update',
'certification_report_layout': 'custom',
})
# Create a simple DOCX template (mock binary data)
self.template_content = b'PK\x03\x04' + b'\x00' * 100 # Minimal DOCX-like header
self.template_binary = base64.b64encode(self.template_content)
# Create initial mappings
self.initial_mappings = {
'placeholders': [
{
'key': '{key.name}',
'value_type': 'user_field',
'value_field': 'partner_name',
'custom_text': ''
},
{
'key': '{key.course_name}',
'value_type': 'survey_field',
'value_field': 'survey_title',
'custom_text': ''
}
]
}
def test_wizard_loads_existing_template_on_update(self):
"""Test that wizard loads existing template when opening for a survey with custom certificate."""
# Set up survey with custom certificate
self.survey.write({
'custom_cert_template': self.template_binary,
'custom_cert_template_filename': 'test_template.docx',
'custom_cert_mappings': json.dumps(self.initial_mappings),
'has_custom_certificate': True,
})
# Create wizard with survey context
wizard = self.env['survey.custom.certificate.wizard'].with_context(
default_survey_id=self.survey.id
).create({
'survey_id': self.survey.id,
})
# Verify wizard loaded existing template
self.assertTrue(wizard.is_update, "Wizard should be in update mode")
self.assertEqual(wizard.template_file, self.template_binary, "Template file should be loaded")
self.assertEqual(wizard.template_filename, 'test_template.docx', "Template filename should be loaded")
def test_delete_custom_certificate_clears_fields(self):
"""Test that deleting custom certificate clears all related fields."""
# Set up survey with custom certificate
self.survey.write({
'custom_cert_template': self.template_binary,
'custom_cert_template_filename': 'test_template.docx',
'custom_cert_mappings': json.dumps(self.initial_mappings),
'has_custom_certificate': True,
'certification_report_layout': 'custom',
})
# Delete the custom certificate
self.survey.action_delete_custom_certificate()
# Verify all fields are cleared
self.assertFalse(self.survey.custom_cert_template, "Template should be cleared")
self.assertFalse(self.survey.custom_cert_template_filename, "Filename should be cleared")
self.assertFalse(self.survey.custom_cert_mappings, "Mappings should be cleared")
self.assertFalse(self.survey.has_custom_certificate, "Has custom certificate flag should be False")
self.assertFalse(self.survey.certification_report_layout, "Layout should be reverted to default")
def test_delete_without_custom_certificate_raises_error(self):
"""Test that deleting when no custom certificate exists raises an error."""
# Ensure survey has no custom certificate
self.survey.write({
'has_custom_certificate': False,
})
# Attempt to delete should raise error
with self.assertRaises(UserError) as context:
self.survey.action_delete_custom_certificate()
self.assertIn('No custom certificate template to delete', str(context.exception))
def test_wizard_preserves_mappings_on_update(self):
"""Test that wizard preserves existing mappings when updating template."""
# Set up survey with custom certificate
self.survey.write({
'custom_cert_template': self.template_binary,
'custom_cert_template_filename': 'test_template.docx',
'custom_cert_mappings': json.dumps(self.initial_mappings),
'has_custom_certificate': True,
})
# Create wizard in update mode
wizard = self.env['survey.custom.certificate.wizard'].with_context(
default_survey_id=self.survey.id
).create({
'survey_id': self.survey.id,
})
# Verify is_update flag is set
self.assertTrue(wizard.is_update, "Wizard should be in update mode")
# Note: Full mapping preservation testing requires the parser service
# which would need a real DOCX file. This test verifies the flag is set correctly.
def test_multiple_surveys_independent_deletion(self):
"""Test that deleting template from one survey doesn't affect others."""
# Create second survey with custom certificate
survey2 = self.env['survey.survey'].create({
'title': 'Test Survey 2',
'certification_report_layout': 'custom',
'custom_cert_template': self.template_binary,
'custom_cert_template_filename': 'test_template2.docx',
'custom_cert_mappings': json.dumps(self.initial_mappings),
'has_custom_certificate': True,
})
# Set up first survey with custom certificate
self.survey.write({
'custom_cert_template': self.template_binary,
'custom_cert_template_filename': 'test_template.docx',
'custom_cert_mappings': json.dumps(self.initial_mappings),
'has_custom_certificate': True,
})
# Delete from first survey
self.survey.action_delete_custom_certificate()
# Verify first survey is cleared
self.assertFalse(self.survey.has_custom_certificate)
# Verify second survey is unaffected
self.assertTrue(survey2.has_custom_certificate, "Second survey should still have custom certificate")
self.assertEqual(survey2.custom_cert_template_filename, 'test_template2.docx')
def test_update_mode_flag_false_for_new_template(self):
"""Test that is_update flag is False when creating wizard for survey without custom certificate."""
# Create wizard for survey without custom certificate
wizard = self.env['survey.custom.certificate.wizard'].with_context(
default_survey_id=self.survey.id
).create({
'survey_id': self.survey.id,
})
# Verify is_update flag is False
self.assertFalse(wizard.is_update, "Wizard should not be in update mode for new template")