"""This module contains the test cases for the model of the ``metaplan`` app"""

from mtp.apps.common.tests.test_cases import MTPTestCase

from ..factories import MetaplanTemplateFactory
from ..models import Metaplan, MetaplanTemplate


class MetaplanTemplateTestCase(MTPTestCase):
    """Test case for the ``MetaplanTemplate`` model"""

    def test_save_a_metaplan_template_with_extra_long_name(self):
        """Test that save a template with a long name should crop it"""

        # Create a metaplan template with extra long name
        template = MetaplanTemplate(
            name='x' * 101,
            training_id=self.training.id
        )
        template.save()

        # The name field should be cropped
        self.assertEqual(
            template.name,
            'x' * 95 + '(...)'
        )


class MetaplanModelTestCase(MTPTestCase):
    """Test case for the ``Metaplan`` model"""

    def test_create_metaplan_from_metaplan_template_with_long_name(self):
        """Test creating a new metaplan from a template with a long name"""

        metaplan_template = MetaplanTemplateFactory(
            name='x' * 101  # This should be truncated (length > 100)
        )

        # Create the metaplan from this template
        metaplan = Metaplan(session=self.training_session)
        metaplan.save(template=metaplan_template)

        # The field "name" on the metaplan should be truncated
        self.assertEqual(
            metaplan.name,
            'x' * 95 + '(...)'
        )
