Example #1
0
    def setUp(self):
        super(SQLStatementExecutorTest, self).setUp()
        database_credentials = TemplateLoader.load_database_credentials()
        self.expected_admin_credentials = "--user='******' --password='******'" % (
            database_credentials.admin_user,
            database_credentials.admin_password)

        self.mock_feedback = self.mox.CreateMock(ExecutionFeedback)
        self.mock_host_controller = self.mox.CreateMock(RemoteHostController)
        self.mock_host_controller.feedback = self.mock_feedback

        self.statement_executor = SQLStatementExecutor(
            database_credentials, self.mock_host_controller)
class SQLStatementExecutorTest(mox.MoxTestBase):

    def setUp(self):
        super(SQLStatementExecutorTest, self).setUp()
        database_credentials = TemplateLoader.load_database_credentials()
        self.expected_admin_credentials = "--user='******' --password='******'" % (database_credentials.admin_user, database_credentials.admin_password)

        self.mock_feedback = self.mox.CreateMock(ExecutionFeedback)
        self.mock_host_controller = self.mox.CreateMock(RemoteHostController)
        self.mock_host_controller.feedback = self.mock_feedback

        self.statement_executor = SQLStatementExecutor(database_credentials, self.mock_host_controller)

    def test_can_execute_single_statement(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute a single statement"""

        self._set_expectations_for_executing_statements("show databases", hide_output=False)

        self.statement_executor.execute(["show databases"])

    def test_can_execute_single_statement_without_output(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute a single statement without output"""

        self._set_expectations_for_executing_statements("show databases", hide_output=True)

        self.statement_executor.execute_without_output(["show databases"])

    def test_can_execute_multiple_statements(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute multiple statements"""

        statements = ["use dev_db", "show tables", "select * from projects"]
        expected_statement_sequence = "; ".join(statements)

        self._set_expectations_for_executing_statements(expected_statement_sequence, hide_output=False)

        self.statement_executor.execute(statements)

    def test_can_execute_multiple_statements_without_output(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute multiple statements without output"""

        statements = ["use dev_db", "show tables", "select * from projects"]
        expected_statement_sequence = "; ".join(statements)

        self._set_expectations_for_executing_statements(expected_statement_sequence, hide_output=True)

        self.statement_executor.execute_without_output(statements)

    def _set_expectations_for_executing_statements(self, expected_statement_sequence, hide_output):
        if hide_output:
            self.mock_host_controller.hide_command_and_output().AndReturn(fabric.api.hide('running', 'stdout'))
        else:
            self.mock_feedback.comment("Executing SQL: %s" % expected_statement_sequence)
            self.mock_host_controller.hide_command().AndReturn(fabric.api.hide('running'))
        self.mock_host_controller.run('mysql %s -e "%s"' % (self.expected_admin_credentials, expected_statement_sequence))
        self.mox.ReplayAll()
    def setUp(self):
        super(SQLStatementExecutorTest, self).setUp()
        database_credentials = TemplateLoader.load_database_credentials()
        self.expected_admin_credentials = "--user='******' --password='******'" % (database_credentials.admin_user, database_credentials.admin_password)

        self.mock_feedback = self.mox.CreateMock(ExecutionFeedback)
        self.mock_host_controller = self.mox.CreateMock(RemoteHostController)
        self.mock_host_controller.feedback = self.mock_feedback

        self.statement_executor = SQLStatementExecutor(database_credentials, self.mock_host_controller)
Example #4
0
 def create_with(database_credentials, host_controller):
     return DatabaseAdminCommand(
         SQLStatementExecutor(database_credentials, host_controller),
         host_controller.feedback)
Example #5
0
class SQLStatementExecutorTest(mox.MoxTestBase):
    def setUp(self):
        super(SQLStatementExecutorTest, self).setUp()
        database_credentials = TemplateLoader.load_database_credentials()
        self.expected_admin_credentials = "--user='******' --password='******'" % (
            database_credentials.admin_user,
            database_credentials.admin_password)

        self.mock_feedback = self.mox.CreateMock(ExecutionFeedback)
        self.mock_host_controller = self.mox.CreateMock(RemoteHostController)
        self.mock_host_controller.feedback = self.mock_feedback

        self.statement_executor = SQLStatementExecutor(
            database_credentials, self.mock_host_controller)

    def test_can_execute_single_statement(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute a single statement"""

        self._set_expectations_for_executing_statements("show databases",
                                                        hide_output=False)

        self.statement_executor.execute(["show databases"])

    def test_can_execute_single_statement_without_output(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute a single statement without output"""

        self._set_expectations_for_executing_statements("show databases",
                                                        hide_output=True)

        self.statement_executor.execute_without_output(["show databases"])

    def test_can_execute_multiple_statements(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute multiple statements"""

        statements = ["use dev_db", "show tables", "select * from projects"]
        expected_statement_sequence = "; ".join(statements)

        self._set_expectations_for_executing_statements(
            expected_statement_sequence, hide_output=False)

        self.statement_executor.execute(statements)

    def test_can_execute_multiple_statements_without_output(self):
        """fab.tests.database.mysql.sql_statement_executor_test  Can execute multiple statements without output"""

        statements = ["use dev_db", "show tables", "select * from projects"]
        expected_statement_sequence = "; ".join(statements)

        self._set_expectations_for_executing_statements(
            expected_statement_sequence, hide_output=True)

        self.statement_executor.execute_without_output(statements)

    def _set_expectations_for_executing_statements(self,
                                                   expected_statement_sequence,
                                                   hide_output):
        if hide_output:
            self.mock_host_controller.hide_command_and_output().AndReturn(
                fabric.api.hide('running', 'stdout'))
        else:
            self.mock_feedback.comment("Executing SQL: %s" %
                                       expected_statement_sequence)
            self.mock_host_controller.hide_command().AndReturn(
                fabric.api.hide('running'))
        self.mock_host_controller.run(
            'mysql %s -e "%s"' %
            (self.expected_admin_credentials, expected_statement_sequence))
        self.mox.ReplayAll()