class TestDB(unittest.TestCase):
    @patch('randtalkbot.db.RetryingDB', create_autospec(RetryingDB))
    @patch('randtalkbot.db.stats')
    @patch('randtalkbot.db.stranger')
    @patch('randtalkbot.db.talk')
    def setUp(self, stats_module_mock, stranger_module_mock, talk_module_mock):
        from randtalkbot.db import RetryingDB as retrying_db_cls_mock
        self.stats_module_mock = stats_module_mock
        self.stranger_module_mock = stranger_module_mock
        self.talk_module_mock = talk_module_mock
        self.database = Mock()
        self.retrying_db_cls_mock = retrying_db_cls_mock
        self.retrying_db_cls_mock.return_value = self.database
        self.configuration = Mock()
        self.configuration.database_host = 'foo_host'
        self.configuration.database_name = 'foo_name'
        self.configuration.database_user = '******'
        self.configuration.database_password = '******'
        self.retrying_db_cls_mock.reset_mock()
        self.db = DB(self.configuration)

    def test_init__ok(self):
        self.retrying_db_cls_mock.assert_called_once_with(
            'foo_name',
            host='foo_host',
            user='******',
            password='******',
        )
        self.stats_module_mock.DATABASE_PROXY.initialize.assert_called_once_with(
            self.database)
        self.stranger_module_mock.DATABASE_PROXY.initialize.assert_called_once_with(
            self.database)
        self.talk_module_mock.DATABASE_PROXY.initialize.assert_called_once_with(
            self.database)

    def test_init__database_troubles(self):
        self.retrying_db_cls_mock.return_value.connect.side_effect = DatabaseError(
        )
        with self.assertRaises(DBError):
            DB(self.configuration)

    def test_install__ok(self):
        self.db.install()
        self.database.create_tables.assert_called_once_with(
            [Stats, Stranger, Talk])

    def test_install__database_error(self):
        self.database.create_tables.side_effect = DatabaseError()
        with self.assertRaises(DBError):
            self.db.install()
Exemple #2
0
 def setUp(self, stats_module_mock, stranger_module_mock, talk_module_mock):
     from randtalkbot.db import RetryingDB
     self.stats_module_mock = stats_module_mock
     self.stranger_module_mock = stranger_module_mock
     self.talk_module_mock = talk_module_mock
     self.database = Mock()
     self.RetryingDB = RetryingDB
     self.RetryingDB.return_value = self.database
     self.configuration = Mock()
     self.configuration.database_host = 'foo_host'
     self.configuration.database_name = 'foo_name'
     self.configuration.database_user = '******'
     self.configuration.database_password = '******'
     self.RetryingDB.reset_mock()
     self.db = DB(self.configuration)
Exemple #3
0
class TestDB(unittest.TestCase):
    @patch('randtalkbot.db.RetryingDB', create_autospec(RetryingDB))
    @patch('randtalkbot.db.stats')
    @patch('randtalkbot.db.stranger')
    @patch('randtalkbot.db.talk')
    def setUp(self, stats_module_mock, stranger_module_mock, talk_module_mock):
        from randtalkbot.db import RetryingDB
        self.stats_module_mock = stats_module_mock
        self.stranger_module_mock = stranger_module_mock
        self.talk_module_mock = talk_module_mock
        self.database = Mock()
        self.RetryingDB = RetryingDB
        self.RetryingDB.return_value = self.database
        self.configuration = Mock()
        self.configuration.database_host = 'foo_host'
        self.configuration.database_name = 'foo_name'
        self.configuration.database_user = '******'
        self.configuration.database_password = '******'
        self.RetryingDB.reset_mock()
        self.db = DB(self.configuration)

    def test_init__ok(self):
        self.RetryingDB.assert_called_once_with(
            'foo_name',
            host='foo_host',
            user='******',
            password='******',
            )
        self.stats_module_mock.database_proxy.initialize.assert_called_once_with(self.database)
        self.stranger_module_mock.database_proxy.initialize.assert_called_once_with(self.database)
        self.talk_module_mock.database_proxy.initialize.assert_called_once_with(self.database)

    def test_init__database_troubles(self):
        self.RetryingDB.return_value.connect.side_effect = DatabaseError()
        with self.assertRaises(DBError):
            DB(self.configuration)

    def test_install__ok(self):
        self.db.install()
        self.database.create_tables.assert_called_once_with([Stats, Stranger, Talk])

    def test_install__database_error(self):
        self.database.create_tables.side_effect = DatabaseError()
        with self.assertRaises(DBError):
            self.db.install()
Exemple #4
0
 def setUp(self, stats_module_mock, stranger_module_mock, talk_module_mock):
     from randtalkbot.db import RetryingDB
     self.stats_module_mock = stats_module_mock
     self.stranger_module_mock = stranger_module_mock
     self.talk_module_mock = talk_module_mock
     self.database = Mock()
     self.RetryingDB = RetryingDB
     self.RetryingDB.return_value = self.database
     self.configuration = Mock()
     self.configuration.database_host = 'foo_host'
     self.configuration.database_name = 'foo_name'
     self.configuration.database_user = '******'
     self.configuration.database_password = '******'
     self.RetryingDB.reset_mock()
     self.db = DB(self.configuration)
Exemple #5
0
 def test_init__database_troubles(self):
     self.RetryingDB.return_value.connect.side_effect = DatabaseError()
     with self.assertRaises(DBError):
         DB(self.configuration)