from odoo.exceptions import ValidationError
from odoo.tests.common import tagged

from .common import QuickTemplateCommon


@tagged('post_install', '-at_install')
class TestMailQuickTemplateCategory(QuickTemplateCommon):

    def test_unique_name_per_company(self):
        with self.assertRaises(Exception):
            self.Category.create({'name': 'General'})

    def test_empty_name(self):
        with self.assertRaises(ValidationError):
            self.Category.create({'name': '   '})

    def test_tn06_delete_category_with_templates(self):
        self._make_template(name='InCat', category_id=self.category.id)
        with self.assertRaises(ValidationError):
            self.category.unlink()

    def test_delete_empty_category(self):
        cat = self.Category.create({'name': 'Empty'})
        cat.unlink()
        self.assertFalse(cat.exists())

    def test_template_count(self):
        self._make_template(name='C1', category_id=self.category.id)
        self._make_template(name='C2', category_id=self.category.id)
        self.category.invalidate_recordset(['template_count'])
        self.assertEqual(self.category.template_count, 2)

    def test_delete_category_with_archived_template(self):
        tmpl = self._make_template(name='Arch', category_id=self.category.id)
        tmpl.action_archive()
        with self.assertRaises(ValidationError):
            self.category.unlink()
