<repo_name>sauravsrijan/collective.collectionfilter<file_sep>/src/collective/collectionfilter/testing.py
# -*- coding: utf-8 -*-
from datetime import datetime
from datetime import timedelta
from plone import api
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.robotframework.testing import REMOTE_LIBRARY_BUNDLE_FIXTURE
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from plone.app.testing import applyProfile
from plone.app.textfield.value import RichTextValue
from plone.testing import z2


class CollectiveCollectionFilterLayer(PloneSandboxLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        # Load any other ZCML that is required for your tests.
        # The z3c.autoinclude feature is disabled in the Plone fixture base
        # layer.
        import plone.app.mosaic
        self.loadZCML(package=plone.app.mosaic)
        import collective.geolocationbehavior
        self.loadZCML(package=collective.geolocationbehavior)
        import collective.collectionfilter
        self.loadZCML(package=collective.collectionfilter)
        self.loadZCML(package=collective.collectionfilter.tests)

    def setUpPloneSite(self, portal):
        applyProfile(portal, 'plone.app.mosaic:default')
        applyProfile(portal, 'collective.geolocationbehavior:default')
        applyProfile(portal, 'collective.collectionfilter:default')
        applyProfile(portal, 'collective.collectionfilter.tests:testing')

        with api.env.adopt_roles(['Manager']):
            portal.invokeFactory(
                'Collection',
                id='testcollection',
                title=u'Test Collection',
                query=[{
                    'i': 'portal_type',
                    'o': 'plone.app.querystring.operation.selection.any',
                    'v': ['Document', 'Event']
                }],
            )
            portal.invokeFactory(
                'Event',
                id='testevent',
                title=u'Test Event',
                start=datetime.now() + timedelta(days=1),
                end=datetime.now() + timedelta(days=2),
                subject=[u'Süper', u'Evänt'],
            )
            portal.invokeFactory(
                'Document',
                id='testdoc',
                title=u'Test Document 😉',
                text=RichTextValue(u'Ein heißes Test Dokument'),
                subject=[u'Süper', u'Dokumänt'],
            )
            doc = portal['testdoc']
            # doc.geolocation.latitude = 47.4048832
            # doc.geolocation.longitude = 9.7587760701108
            doc.reindexObject()


COLLECTIVE_COLLECTIONFILTER_FIXTURE = CollectiveCollectionFilterLayer()


COLLECTIVE_COLLECTIONFILTER_INTEGRATION_TESTING = IntegrationTesting(
    bases=(COLLECTIVE_COLLECTIONFILTER_FIXTURE,),
    name='CollectiveCollectionFilterLayer:IntegrationTesting',
)


COLLECTIVE_COLLECTIONFILTER_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(COLLECTIVE_COLLECTIONFILTER_FIXTURE,),
    name='CollectiveCollectionFilterLayer:FunctionalTesting',
)


COLLECTIVE_COLLECTIONFILTER_ACCEPTANCE_TESTING = FunctionalTesting(
    bases=(
        COLLECTIVE_COLLECTIONFILTER_FIXTURE,
        REMOTE_LIBRARY_BUNDLE_FIXTURE,
        z2.ZSERVER_FIXTURE,
    ),
    name='CollectiveCollectionFilterLayer:AcceptanceTesting',
)
<file_sep>/src/collective/collectionfilter/tests/test_filteritems.py
# -*- coding: utf-8 -*-
"""Setup tests for this package."""
import unittest

from collective.collectionfilter.testing import COLLECTIVE_COLLECTIONFILTER_INTEGRATION_TESTING  # noqa
from collective.collectionfilter.filteritems import get_filter_items


def get_data_by_val(result, val):
    for r in result:
        if r['value'] == val:
            return r


class TestFilteritems(unittest.TestCase):

    layer = COLLECTIVE_COLLECTIONFILTER_INTEGRATION_TESTING

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.collection = self.portal['testcollection']
        self.collection_uid = self.collection.UID()

    def test_filteritems(self):
        self.assertEqual(len(self.collection.results()), 2)

        result = get_filter_items(
            self.collection_uid, 'Subject', cache_enabled=False)

        self.assertEqual(len(result), 4)
        self.assertEqual(get_data_by_val(result, 'all')['count'], 2)
        self.assertEqual(get_data_by_val(result, 'all')['selected'], True)
        self.assertEqual(get_data_by_val(result, u'Süper')['count'], 2)
        self.assertEqual(get_data_by_val(result, u'Evänt')['count'], 1)
        self.assertEqual(get_data_by_val(result, u'Dokumänt')['count'], 1)

        result = get_filter_items(
            self.collection_uid, 'Subject',
            request_params={'Subject': u'Süper'},
            cache_enabled=False)

        self.assertEqual(len(result), 4)
        self.assertEqual(get_data_by_val(result, u'Süper')['selected'], True)

        result = get_filter_items(
            self.collection_uid, 'Subject',
            request_params={'Subject': u'Dokumänt'},
            cache_enabled=False)

        self.assertEqual(len(result), 4)
        self.assertEqual(
            get_data_by_val(result, u'Dokumänt')['selected'], True)

        # test narrowed down results
        narrowed_down_result = get_filter_items(
            self.collection_uid, 'Subject',
            request_params={'Subject': u'Dokumänt'},
            narrow_down=True,
            show_count=True,
            cache_enabled=False)

        self.assertEqual(
            len(narrowed_down_result), 3,
            msg=u"narrowed result length should be 3")
        self.assertEqual(
            get_data_by_val(narrowed_down_result, u'Dokumänt')['selected'], True,  # noqa
            msg=u"Test that 'Dokumänt' is selected, matching the query")
        self.assertEqual(
            get_data_by_val(narrowed_down_result, u'all')['count'], 2,
            msg=u"Test that there are 2 Subjects")

    def test_portal_type_filter(self):
        self.assertEqual(len(self.collection.results()), 2)

        result = get_filter_items(
            self.collection_uid, 'portal_type', cache_enabled=False)

        self.assertEqual(len(result), 3)
        self.assertEqual(get_data_by_val(result, 'all')['count'], 2)
        self.assertEqual(get_data_by_val(result, 'all')['selected'], True)
        self.assertEqual(get_data_by_val(result, u'Event')['count'], 1)
        self.assertEqual(get_data_by_val(result, u'Document')['count'], 1)

        result = get_filter_items(
            self.collection_uid, 'portal_type',
            request_params={'portal_type': u'Event'},
            cache_enabled=False)

        self.assertEqual(len(result), 3)
        self.assertEqual(get_data_by_val(result, u'Event')['selected'], True)

        # test narrowed down results
        result = get_filter_items(
            self.collection_uid, 'portal_type',
            request_params={'portal_type': u'Event'},
            narrow_down=True,
            show_count=True,
            cache_enabled=False)

        self.assertEqual(len(result), 2)
        self.assertEqual(
            get_data_by_val(result, u'all')['count'], 2,
            msg=u"Test that the number of portal_types in the collection is 2")
        self.assertEqual(
            get_data_by_val(result, u'Event')['selected'], True,
            msg=u"Test that Event portal_type is selected matching the query")
