<commit_before>from unittest.mock import Mock
from pyramid import testing
from pytest import fixture
from pytest import mark


@fixture
def integration(config):
    config.include('adhocracy_core.events')
    config.include('adhocracy_core.content')
    config.include('adhocracy_core.graph')
    config.include('adhocracy_core.catalog')
    config.include('adhocracy_core.sheets.metadata')
    config.include('adhocracy_core.sheets.rate')


def test_create_adhocracy_catalog_indexes():
    from substanced.catalog import Keyword
    from .adhocracy import AdhocracyCatalogIndexes
    from .adhocracy import Reference
    inst = AdhocracyCatalogIndexes()
    assert isinstance(inst.tag, Keyword)
    assert isinstance(inst.reference, Reference)


@mark.usefixtures('integration')
def test_create_adhocracy_catalog(pool_graph, registry):
    from substanced.catalog import Catalog
    context = pool_graph
    catalogs = registry.content.create('Catalogs')
    context.add_service('catalogs', catalogs, registry=registry)
    catalogs.add_catalog('adhocracy')

    assert isinstance(catalogs['adhocracy'], Catalog)
    assert 'tag' in catalogs['adhocracy']
    assert 'reference' in catalogs['adhocracy']
    assert 'rate' in catalogs['adhocracy']
    assert 'rates' in catalogs['adhocracy']
    assert 'creator' in catalogs['adhocracy']
    assert 'private_visibility' in catalogs['adhocracy']


class TestIndexCreator:

    @fixture
    def registry(self, registry_with_content):
        return registry_with_content

    @fixture
    def mock_sheet(self, mock_sheet, registry):
        from adhocracy_core.sheets.metadata import IMetadata
        mock_sheet.meta = mock_sheet.meta._replace(isheet=IMetadata)
        registry.content.get_sheet.return_value = mock_sheet
        return mock_sheet

    def test_creator_exists(self, context, mock_sheet):
        from .adhocracy import index_creator
        context['user1'] = testing.DummyResource()
        mock_sheet.get.return_value = {'creator': context['user1']}
        assert index_creator(context, 'default') == '/user1'

    def test_creator_does_not_exists(self, context, mock_sheet):
        from .adhocracy import index_creator
        context['user1'] = testing.DummyResource()
        mock_sheet.get.return_value = {'creator': ''}
        assert index_creator(context, 'default') == ''


@mark.usefixtures('integration')
def test_includeme_register_index_creator(registry):
    from adhocracy_core.sheets.metadata import IMetadata
    from substanced.interfaces import IIndexView
    assert registry.adapters.lookup((IMetadata,), IIndexView,
                                    name='adhocracy|creator')

<commit_msg>Add unit test for item_creation_date index<commit_after>from unittest.mock import Mock
from pyramid import testing
from pytest import fixture
from pytest import mark


@fixture
def integration(config):
    config.include('adhocracy_core.events')
    config.include('adhocracy_core.content')
    config.include('adhocracy_core.graph')
    config.include('adhocracy_core.catalog')
    config.include('adhocracy_core.sheets.metadata')
    config.include('adhocracy_core.sheets.rate')


def test_create_adhocracy_catalog_indexes():
    from substanced.catalog import Keyword
    from .adhocracy import AdhocracyCatalogIndexes
    from .adhocracy import Reference
    inst = AdhocracyCatalogIndexes()
    assert isinstance(inst.tag, Keyword)
    assert isinstance(inst.reference, Reference)


@mark.usefixtures('integration')
def test_create_adhocracy_catalog(pool_graph, registry):
    from substanced.catalog import Catalog
    context = pool_graph
    catalogs = registry.content.create('Catalogs')
    context.add_service('catalogs', catalogs, registry=registry)
    catalogs.add_catalog('adhocracy')

    assert isinstance(catalogs['adhocracy'], Catalog)
    assert 'tag' in catalogs['adhocracy']
    assert 'reference' in catalogs['adhocracy']
    assert 'rate' in catalogs['adhocracy']
    assert 'rates' in catalogs['adhocracy']
    assert 'creator' in catalogs['adhocracy']
    assert 'item_creation_date' in catalogs['adhocracy']
    assert 'private_visibility' in catalogs['adhocracy']


class TestIndexMetadata:

    @fixture
    def registry(self, registry_with_content):
        return registry_with_content

    @fixture
    def mock_sheet(self, mock_sheet, registry):
        from adhocracy_core.sheets.metadata import IMetadata
        mock_sheet.meta = mock_sheet.meta._replace(isheet=IMetadata)
        registry.content.get_sheet.return_value = mock_sheet
        return mock_sheet

    def test_creator_exists(self, context, mock_sheet):
        from .adhocracy import index_creator
        context['user1'] = testing.DummyResource()
        mock_sheet.get.return_value = {'creator': context['user1']}
        assert index_creator(context, 'default') == '/user1'

    def test_creator_does_not_exists(self, context, mock_sheet):
        from .adhocracy import index_creator
        context['user1'] = testing.DummyResource()
        mock_sheet.get.return_value = {'creator': ''}
        assert index_creator(context, 'default') == ''

    def test_item_creation_date(self, context, mock_sheet):
        import datetime
        from .adhocracy import index_item_creation_date
        context['user1'] = testing.DummyResource()
        some_date = datetime.datetime(2009, 7, 12)
        mock_sheet.get.return_value = {'item_creation_date': some_date}
        assert index_item_creation_date(context, 'default') == some_date


@mark.usefixtures('integration')
def test_includeme_register_index_creator(registry):
    from adhocracy_core.sheets.metadata import IMetadata
    from substanced.interfaces import IIndexView
    assert registry.adapters.lookup((IMetadata,), IIndexView,
                                    name='adhocracy|creator')


{'fraction_non_alphanumeric': 0.09508309062874765, 'fraction_numerical': 0.0027411341442521845, 'mean_word_length': 4.2171581769437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1887706', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1668, 'n_words': 352}