import os

from mock import Mock, PropertyMock, patch
from moto import mock_ec2
from tornado.testing import AsyncTestCase


class Test_broker(AsyncTestCase):
    db_uri = "sqlite:////tmp/loads_test.db"

    def setUp(self):
        super().setUp()
        from loadsbroker.db import Database
        self.db = Database(self.db_uri, echo=True)
        self.db_session = self.db.session()

    def tearDown(self):
        super().tearDown()
        try:
            os.unlink(self.db_uri)
        except FileNotFoundError:
            pass

    def _createFUT(self):
        from loadsbroker.broker import Broker
        from loadsbroker.options import InfluxOptions, HekaOptions
        heka_options = HekaOptions("172.31.34.9", 6745, False)
        influx_options = InfluxOptions("localhost", 8086, "root", "root",
                                       False)
        return Broker(self.io_loop, self.db_uri, None,
                      heka_options, influx_options,
                      aws_use_filters=False, initial_db=None)

    @mock_ec2
    def test_broker_creation(self):
        broker = self._createFUT()
        self.assertNotEqual(broker, None)
        broker.shutdown()

    @mock_ec2
    def test_broker_run_plan(self):
        from tornado.concurrent import Future
        # Setup all the mocks
        mock_future = Mock(spec=Future)

        # Setup the mock RunManager instance, and properties needed
        mock_rm_inst = Mock()

        mock_run = Mock()
        type(mock_run).uuid = PropertyMock(return_value="asdf")

        type(mock_rm_inst).run = PropertyMock(return_value=mock_run)

        with patch('loadsbroker.broker.RunManager',
                   new_callable=Mock) as mock_rm:
            broker = self._createFUT()
            mock_rm.new_run.return_value = (mock_rm_inst, mock_future)
            uuid = broker.run_plan("bleh", create_db=False)
            self.assertEqual(uuid, "asdf")

@@ -208,657 +208,139 @@
 def 
-setUp(self):%0A        super().setUp()%0A        from loadsbroker.db import Database%0A        self.db = Database(self.db_uri, echo=True)%0A        self.db_session = self.db.session()%0A%0A    def tearDown(self):%0A        super().tearDown()%0A        try:%0A            os.unlink(self.db_uri)%0A        except FileNotFoundError:%0A            pass%0A%0A    def _createFUT(self):%0A        from loadsbroker.broker import Broker%0A        from loadsbroker.options import InfluxOptions, HekaOptions%0A        heka_options = HekaOptions(%22172.31.34.9%22, 6745, False)%0A        influx_options = InfluxOptions(%22localhost%22, 8086, %22root%22, %22root%22,%0A                                       False)
+_createFUT(self):%0A        from loadsbroker.broker import Broker%0A        from loadsbroker.options import InfluxOptions, HekaOptions%0A
 %0A   
@@ -417,36 +417,78 @@
     
-heka_o
+Mock(spec=HekaO
 ptions
-, i
+),%0A                      Mock(spec=I
 nflux
-_o
+O
 ptions
+)
 ,%0A  
@@ -1436,16 +1436,437 @@
 l(uuid, %22asdf%22)%0A
+%0A%0Aclass Test_run_manager(AsyncTestCase):%0A    db_uri = %22sqlite:////tmp/loads_test.db%22%0A%0A    def setUp(self):%0A        super().setUp()%0A        from loadsbroker.db import Database%0A        self.db = Database(self.db_uri, echo=True)%0A        self.db_session = self.db.session()%0A%0A    def tearDown(self):%0A        super().tearDown()%0A        try:%0A            os.unlink(self.db_uri)%0A        except FileNotFoundError:%0A            pass%0A

Move more to mocks, prep run manager test