from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks, returnValue

from vumi.worker import BaseConfig, BaseWorker
from vumi.tests.utils import VumiWorkerTestCase, LogCatcher, get_stubbed_worker
from vumi.message import TransportUserMessage
from vumi.middleware.tests.utils import RecordingMiddleware


class DummyWorker(BaseWorker):
    def setup_connectors(self):
        pass

    def setup_worker(self):
        pass

    def teardown_worker(self):
        pass


class TestBaseConfig(TestCase):
    def test_no_amqp_prefetch(self):
        config = BaseConfig({})
        self.assertEqual(config.amqp_prefetch_count, 20)

    def test_amqp_prefetch(self):
        config = BaseConfig({'amqp_prefetch_count': 10})
        self.assertEqual(config.amqp_prefetch_count, 10)


class TestBaseWorker(VumiWorkerTestCase):

    @inlineCallbacks
    def setUp(self):
        yield super(TestBaseWorker, self).setUp()
        self.worker = yield self.get_worker({}, DummyWorker)

    # TODO: complete tests

    def test_start_worker(self):
        pass

    def test_stop_worker(self):
        pass

    def test_setup_connectors_raises(self):
        worker = get_stubbed_worker(BaseWorker, {}, None)  # None -> dummy AMQP
        self.assertRaises(NotImplementedError, worker.setup_connectors)

    def test_teardown_connectors(self):
        pass

    def test_setup_worker_raises(self):
        worker = get_stubbed_worker(BaseWorker, {}, None)  # None -> dummy AMQP
        self.assertRaises(NotImplementedError, worker.setup_worker)

    def test_teardown_worker_raises(self):
        worker = get_stubbed_worker(BaseWorker, {}, None)  # None -> dummy AMQP
        self.assertRaises(NotImplementedError, worker.teardown_worker)

    def test_setup_middleware(self):
        pass

    def test_teardown_middleware(self):
        pass

    def test_get_static_config(self):
        pass

    def test_get_config(self):
        pass

    def test__validate_config(self):
        pass

    def test_validate_config(self):
        pass

    def test_setup_connector(self):
        pass

    def test_teardown_connector(self):
        pass

    def test_setup_ri_connector(self):
        pass

    def test_setup_ro_connector(self):
        pass

    def test_pause_connectors(self):
        pass

    def test_unpause_connectors(self):
        pass

@@ -145,24 +145,102 @@
  BaseWorker%0A
+from vumi.connectors import ReceiveInboundConnector, ReceiveOutboundConnector%0A
 from vumi.te
@@ -2240,32 +2240,53 @@
 :%0A        pass%0A%0A
+    @inlineCallbacks%0A
     def test_set
@@ -2320,65 +2320,429 @@
     
-pass%0A%0A    def test_setup_ro_connector(self):%0A        pass
+connector = yield self.worker.setup_ri_connector('foo')%0A        self.assertTrue(isinstance(connector, ReceiveInboundConnector))%0A        self.assertEqual(connector.name, 'foo')%0A%0A    @inlineCallbacks%0A    def test_setup_ro_connector(self):%0A        connector = yield self.worker.setup_ro_connector('foo')%0A        self.assertTrue(isinstance(connector, ReceiveOutboundConnector))%0A        self.assertEqual(connector.name, 'foo')
 %0A%0A  

Add tests for setup_ri_connector and setup_ro_connector.