<commit_before><commit_msg>test event init and config<commit_after>from pyramid import testing

from git import Repo

from elasticgit.tests.base import ModelBaseTest

from unicore.distribute.events import (
    RepositoryEvent, RepositoryCloned, RepositoryUpdated,
    ContentTypeObjectUpdated)


class TestEvents(ModelBaseTest):

    def setUp(self):
        self.workspace = self.mk_workspace()
        self.config = testing.setUp()
        self.config.include('unicore.distribute.api')

    def tearDown(self):
        testing.tearDown()

    def get_handlers_for(self, config, event_cls):
        return map(
            lambda h: h.handler.__name__,
            filter(
                lambda h: h.required[0].implementedBy(event_cls),
                config.registry.registeredHandlers())
            )

    def test_repository_event(self):
        event_obj = RepositoryEvent(
            config='foo config',
            repo_dir=self.workspace.working_dir,
            repo_url='foo url')
        self.assertIsInstance(event_obj.repo, Repo)
        self.assertEqual(event_obj.repo, self.workspace.repo)
        self.assertEqual(event_obj.config, 'foo config')
        self.assertEqual(event_obj.repo_url, 'foo url')

    def test_configuration(self):
        self.assertNotIn('initialize_repo_index',
                         self.get_handlers_for(self.config, RepositoryCloned))
        self.assertNotIn('update_repo_index',
                         self.get_handlers_for(self.config, RepositoryUpdated))
        self.assertNotIn('index_content_type_object',
                         self.get_handlers_for(
                            self.config, ContentTypeObjectUpdated))

        config = testing.setUp(settings={
            'es.indexing_enabled': 'true'
        })
        config.include('unicore.distribute.api')
        self.assertIn('initialize_repo_index',
                      self.get_handlers_for(config, RepositoryCloned))
        self.assertIn('update_repo_index',
                      self.get_handlers_for(config, RepositoryUpdated))
        self.assertIn('index_content_type_object',
                      self.get_handlers_for(config, ContentTypeObjectUpdated))
{'fraction_non_alphanumeric': 0.09078467153284671, 'fraction_numerical': 0.0004562043795620438, 'mean_word_length': 2.4920382165605095, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5382087', 'n_tokens_mistral': 615, 'n_tokens_neox': 596, 'n_words': 94}