diff options
author | Dana Powers <dana.powers@rd.io> | 2016-01-03 16:28:49 -0800 |
---|---|---|
committer | Dana Powers <dana.powers@rd.io> | 2016-01-03 16:28:49 -0800 |
commit | 6d48a1cc5fce549757fe306fad54a0f3a4f4444b (patch) | |
tree | 54134ce08b396bf925d39732080129cb87b3d357 | |
parent | fa7ecdaf3fd1ee9d54f8c39e98c99e439353d426 (diff) | |
download | kafka-python-6d48a1cc5fce549757fe306fad54a0f3a4f4444b.tar.gz |
Use parameterized pytests in test_client_async; add pytest-mocker plugin
-rw-r--r-- | test/test_client_async.py | 164 | ||||
-rw-r--r-- | tox.ini | 1 |
2 files changed, 95 insertions, 70 deletions
diff --git a/test/test_client_async.py b/test/test_client_async.py index 5f0ccb0..aa8ff11 100644 --- a/test/test_client_async.py +++ b/test/test_client_async.py @@ -1,6 +1,5 @@ -from mock import patch -from . import unittest +import pytest from kafka.client_async import KafkaClient from kafka.common import BrokerMetadata @@ -9,95 +8,120 @@ from kafka.future import Future from kafka.protocol.metadata import MetadataResponse, MetadataRequest -class TestAsyncKafkaClient(unittest.TestCase): +@pytest.mark.parametrize("bootstrap,expected_hosts", [ + (None, [('localhost', 9092)]), + ('foobar:1234', [('foobar', 1234)]), + ('fizzbuzz', [('fizzbuzz', 9092)]), + ('foo:12,bar:34', [('foo', 12), ('bar', 34)]), + (['fizz:56', 'buzz'], [('fizz', 56), ('buzz', 9092)]), +]) +def test_bootstrap_servers(mocker, bootstrap, expected_hosts): + mocker.patch.object(KafkaClient, '_bootstrap') + if bootstrap is None: + KafkaClient() + else: + KafkaClient(bootstrap_servers=bootstrap) - def test_init(self): - with patch.object(KafkaClient, '_bootstrap') as bootstrap: + # host order is randomized internally, so resort before testing + (hosts,), _ = KafkaClient._bootstrap.call_args # pylint: disable=no-member + assert sorted(hosts) == sorted(expected_hosts) - KafkaClient() - bootstrap.assert_called_with([('localhost', 9092)]) - other_test_cases = [ - ('foobar:1234', [('foobar', 1234)]), - ('fizzbuzz', [('fizzbuzz', 9092)]), - ('foo:12,bar:34', [('foo', 12), ('bar', 34)]), - (['fizz:56', 'buzz'], [('fizz', 56), ('buzz', 9092)]) - ] - for arg, test in other_test_cases: - KafkaClient(bootstrap_servers=arg) - # host order is randomized internally, so resort before testing - (hosts,), _ = bootstrap.call_args - assert sorted(hosts) == sorted(test) +@pytest.fixture +def conn(mocker): + conn = mocker.patch('kafka.client_async.BrokerConnection') + conn.return_value = conn + conn.state = ConnectionStates.CONNECTED + conn.send.return_value = Future().success( + MetadataResponse( + [(0, 'foo', 12), (1, 'bar', 34)], # brokers + [])) # topics + return conn - @patch('kafka.client_async.BrokerConnection') - def test_bootstrap(self, conn): - conn.return_value = conn - conn.state = ConnectionStates.CONNECTED - conn.send.return_value = Future().success(MetadataResponse( - [(0, 'foo', 12), (1, 'bar', 34)], [])) - cli = KafkaClient() - conn.assert_called_once_with('localhost', 9092, **cli.config) - conn.connect.assert_called_with() - conn.send.assert_called_once_with(MetadataRequest([])) - assert cli._bootstrap_fails == 0 - assert cli.cluster.brokers() == set([BrokerMetadata(0, 'foo', 12), - BrokerMetadata(1, 'bar', 34)]) +def test_bootstrap_success(conn): + conn.state = ConnectionStates.CONNECTED + cli = KafkaClient() + conn.assert_called_once_with('localhost', 9092, **cli.config) + conn.connect.assert_called_with() + conn.send.assert_called_once_with(MetadataRequest([])) + assert cli._bootstrap_fails == 0 + assert cli.cluster.brokers() == set([BrokerMetadata(0, 'foo', 12), + BrokerMetadata(1, 'bar', 34)]) - conn.state = ConnectionStates.DISCONNECTED - cli = KafkaClient() - conn.connect.assert_called_with() - conn.close.assert_called_with() - assert cli._bootstrap_fails == 1 +def test_bootstrap_failure(conn): + conn.state = ConnectionStates.DISCONNECTED + cli = KafkaClient() + conn.assert_called_once_with('localhost', 9092, **cli.config) + conn.connect.assert_called_with() + conn.close.assert_called_with() + assert cli._bootstrap_fails == 1 + assert cli.cluster.brokers() == set() - def test_can_connect(self): - pass - def test_initiate_connect(self): - pass +def test_can_connect(): + pass - def test_finish_connect(self): - pass - def test_ready(self): - pass +def test_initiate_connect(): + pass - def test_close(self): - pass - def test_is_disconnected(self): - pass +def test_finish_connect(): + pass - def test_is_ready(self): - pass - def test_can_send_request(self): - pass +def test_ready(): + pass - def test_send(self): - pass - def test_poll(self): - pass +def test_close(): + pass - def test__poll(self): - pass - def test_in_flight_request_count(self): - pass +def test_is_disconnected(): + pass - def test_least_loaded_node(self): - pass - def test_set_topics(self): - pass +def test_is_ready(): + pass - def test_maybe_refresh_metadata(self): - pass - def test_schedule(self): - pass +def test_can_send_request(): + pass - def test_unschedule(self): - pass +def test_send(): + pass + + +def test_poll(): + pass + + +def test__poll(): + pass + + +def test_in_flight_request_count(): + pass + + +def test_least_loaded_node(): + pass + + +def test_set_topics(): + pass + + +def test_maybe_refresh_metadata(): + pass + + +def test_schedule(): + pass + + +def test_unschedule(): + pass @@ -8,6 +8,7 @@ deps = pytest-catchlog pytest-pylint pytest-sugar + pytest-mock mock python-snappy py{26,27}: six |