""" This module holds the tests models of the ``brainstormers`` app. """

from mtp.apps.brainstormers.constants import DEFAULT_GROUP_ATTRIBUTES
from mtp.apps.brainstormers.factories import (
    BrainstormerFactory,
    BrainstormerPropositionFactory,
    BrainstormerPropositionGroupFactory,
)
from mtp.apps.common.tests.test_cases import MTPTestCase

from .test_cases import BrainstormerActivityTestCase


class BrainstormerModelTestCase(MTPTestCase):
    """ Test case that tests the ``Brainstormer`` model. """

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

        cls.brainstormer = BrainstormerFactory()

    def test_default_group_should_be_created(self):
        """Test that the default group is created."""
        self.assertTrue(self.brainstormer.groups.filter(
            name=DEFAULT_GROUP_ATTRIBUTES.get('name')
        ).exists())

    def test_get_propositions(self):
        """Test that ``get_propositions`` return all instances expected instances ``is_deleted`` """
        group = BrainstormerPropositionGroupFactory(brainstormer=self.brainstormer)

        # Test return all instances
        BrainstormerPropositionFactory.create_batch(2, group=group)
        self.assertEqual(len(self.brainstormer.get_propositions()), 2)

        # Test not return instances ``is deleted``
        BrainstormerPropositionFactory.create_batch(3, group=group, is_deleted=True)
        self.assertEqual(len(self.brainstormer.get_propositions()), 2)

    def test_get_groups(self):
        """Test that ``get_groups`` return all instances excluding instances ``is_deleted`` """
        nb_group = len(self.brainstormer.get_groups())

        # Test return all instances
        BrainstormerPropositionGroupFactory.create_batch(2, brainstormer=self.brainstormer)
        self.assertEqual(len(self.brainstormer.get_groups()), nb_group+2)

        # Test not return instances ``is deleted``
        BrainstormerPropositionGroupFactory.create_batch(
            3, brainstormer=self.brainstormer, is_deleted=True
        )
        self.assertEqual(len(self.brainstormer.get_groups()), nb_group+2)

    def test_compute_hash(self):
        """Test that brainstormer creation, compute hash fields"""
        self.assertIsNotNone(self.brainstormer.activity.hash_preview)


class BrainstormerActivityModelTestCase(BrainstormerActivityTestCase):
    """Test cases for the ``BrainstormerActivity`` model"""

    def test_media_files(self):
        """Test that the media file property should return the paths of the media files"""

        self.assertCountEqual(
            self.brainstormer_activity.media_files,
            [
                self.brainstormer_activity.preview.name,
                self.brainstormer_activity.image.name,
            ]
        )
