<commit_before>import mock
from unittest import TestCase
from devour.handlers import ClientHandler
from devour.producers import _ProducerProxy
from threading import local

class TestClientHandler(TestCase):
    def setUp(self):
        self.config = {
            "hosts":"fakehost:fakeport",
            "ssl_config": {
                'cafile': './fake/path/',
                'certfile': './fake/path/',
                'keyfile': './fake/path/',
                'password': <PASSWORD>/'
            }
        }

        conf = mock.MagicMock()
        conf.__getitem__.return_value = self.config
        self.settings = mock.MagicMock()
        type(self.settings).KAFKA_CONFIG = mock.PropertyMock(return_value=conf)

    def test_handler_init(self):
        handler = ClientHandler()

        self.assertTrue(isinstance(handler._client, local))
        self.assertTrue(isinstance(handler.producers, local))

        self.assertEqual(len(handler._client.__dict__.keys()), 0)
        self.assertEqual(len(handler.producers.__dict__.keys()), 0)

    @mock.patch('devour.handlers.pykafka.SslConfig')
    @mock.patch('devour.handlers.load_module')
    @mock.patch('devour.handlers.pykafka.KafkaClient')
    def test_check_status(self, mocked_client, mocked_load, mocked_ssl):
        mocked_client.reset_mock()
        mocked_load.reset_mock()
        self.settings.reset_mock()

        mocked_load.return_value = self.settings

        handler = ClientHandler()

        # call twice
        handler._check_status()
        self.assertEqual(len(handler._client.__dict__.keys()), 1)

        handler._check_status()
        self.assertEqual(len(handler._client.__dict__.keys()), 1)

        self.assertTrue(handler._client, 'pykafka')
        mocked_client.assert_called_once_with(
            hosts='fakehost:fakeport',
            zookeeper_hosts=None,
            ssl_config=mocked_ssl.return_value
        )
        mocked_ssl.assert_called_once_with(**self.config['ssl_config'])

    @mock.patch('devour.handlers.load_module')
    @mock.patch('devour.handlers.pykafka.KafkaClient')
    def test_get_topic(self, mocked_client, mocked_load, mocked_ssl):
        mocked_client.reset_mock()
        mocked_load.reset_mock()
        self.settings.reset_mock()

        config = {
            "hosts":"fakehost:fakeport",
            "ssl_config": None
        }

        mocked_load.return_value = self.settings

        mocked_topic = mock.MagicMock()
        mocked_client.return_value.topics.__getitem__.return_value = mocked_topic

        handler = ClientHandler()
        handler.get_topic('test')

        mocked_client.return_value.topics.__getitem__.assert_called_once_with('test')

    @mock.patch('devour.handlers.load_module')
    @mock.patch('devour.handlers.pykafka.KafkaClient')
    def test_get_producer_sync(self, mocked_client, mocked_load):
        mocked_client.reset_mock()
        mocked_load.reset_mock()
        self.settings.reset_mock()

        mocked_load.return_value = self.settings

        mocked_topic = mock.MagicMock()<commit_msg>Remove extra arg from unrelated test case<commit_after>import mock
from unittest import TestCase
from devour.handlers import ClientHandler
from devour.producers import _ProducerProxy
from threading import local

class TestClientHandler(TestCase):
    def setUp(self):
        self.config = {
            "hosts":"fakehost:fakeport",
            "ssl_config": {
                'cafile': './fake/path/',
                'certfile': './fake/path/',
                'keyfile': './fake/path/',
                'password': <PASSWORD>/'
            }
        }

        conf = mock.MagicMock()
        conf.__getitem__.return_value = self.config
        self.settings = mock.MagicMock()
        type(self.settings).KAFKA_CONFIG = mock.PropertyMock(return_value=conf)

    def test_handler_init(self):
        handler = ClientHandler()

        self.assertTrue(isinstance(handler._client, local))
        self.assertTrue(isinstance(handler.producers, local))

        self.assertEqual(len(handler._client.__dict__.keys()), 0)
        self.assertEqual(len(handler.producers.__dict__.keys()), 0)

    @mock.patch('devour.handlers.pykafka.SslConfig')
    @mock.patch('devour.handlers.load_module')
    @mock.patch('devour.handlers.pykafka.KafkaClient')
    def test_check_status(self, mocked_client, mocked_load, mocked_ssl):
        mocked_client.reset_mock()
        mocked_load.reset_mock()
        self.settings.reset_mock()

        mocked_load.return_value = self.settings

        handler = ClientHandler()

        # call twice
        handler._check_status()
        self.assertEqual(len(handler._client.__dict__.keys()), 1)

        handler._check_status()
        self.assertEqual(len(handler._client.__dict__.keys()), 1)

        self.assertTrue(handler._client, 'pykafka')
        mocked_client.assert_called_once_with(
            hosts='fakehost:fakeport',
            zookeeper_hosts=None,
            ssl_config=mocked_ssl.return_value
        )
        mocked_ssl.assert_called_once_with(**self.config['ssl_config'])

    @mock.patch('devour.handlers.load_module')
    @mock.patch('devour.handlers.pykafka.KafkaClient')
    def test_get_topic(self, mocked_client, mocked_load):
        mocked_client.reset_mock()
        mocked_load.reset_mock()
        self.settings.reset_mock()

        config = {
            "hosts":"fakehost:fakeport",
            "ssl_config": None
        }

        mocked_load.return_value = self.settings

        mocked_topic = mock.MagicMock()
        mocked_client.return_value.topics.__getitem__.return_value = mocked_topic

        handler = ClientHandler()
        handler.get_topic('test')

        mocked_client.return_value.topics.__getitem__.assert_called_once_with('test')

    @mock.patch('devour.handlers.load_module')
    @mock.patch('devour.handlers.pykafka.KafkaClient')
    def test_get_producer_sync(self, mocked_client, mocked_load):
        mocked_client.reset_mock()
        mocked_load.reset_mock()
        self.settings.reset_mock()

        mocked_load.return_value = self.settings

        mocked_topic = mock.MagicMock()
{'fraction_non_alphanumeric': 0.1127195836044242, 'fraction_numerical': 0.0013012361743656475, 'mean_word_length': 2.805074257425743, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, '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': '200944', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1825, 'n_words': 244}