from plone import api
from ploneintranet.workspace.tests.base import BaseTestCase
from zope.component import getUtility
from zope.schema.interfaces import IVocabularyFactory

vocab = "ploneintranet.workspace.vocabularies.Divisions"


class TestDivisions(BaseTestCase):
    """
    Test the DivisionsVocabulary
    """

    def create_division(self, title, description=""):
        """Create a division given a title and a description"""
        obj = api.content.create(
            self.workspace_container,
            "ploneintranet.workspace.workspacefolder",
            title=title,
            description=description,
            is_division=True,
        )
        return obj

    def test_divisions_vocabulary(self):
        """Test that we have divisions"""
        # create a couple of divisions with the same name and description
        # to check that the vocabulary does not screw up!
        division_folder_1 = self.create_division(
            "Division Workspace (™)", "I ♡ unicode"
        )
        division_folder_2 = self.create_division(
            "Division Workspace (™)", "I ♡ unicode"
        )
        division_folder_3 = self.create_division("AAA I have to go first!")

        divisions = getUtility(IVocabularyFactory, vocab)(self.portal)
        self.assertEqual(len(divisions), 3)
        observed = [(x.title, x.description) for x in divisions]
        expected = [
            ("AAA I have to go first!", ""),
            ("Division Workspace (\u2122)", "I \u2661 unicode"),
            ("Division Workspace (\u2122)", "I \u2661 unicode"),
        ]
        self.assertListEqual(observed, expected)
        observed = {(x.value, x.token) for x in divisions}
        expected = {
            (division_folder_1.UID(), division_folder_1.UID()),
            (division_folder_2.UID(), division_folder_2.UID()),
            (division_folder_3.UID(), division_folder_3.UID()),
        }
        self.assertSetEqual(observed, expected)
