# Path: arbalest/redshift/step.py
# class SqlStep(PipelineStep):
#     def __init__(self, database, *args):
#         self.database = database
#         self.statements = list(args)
# 
#     def run(self):
#         self.database.open()
#         self.execute(self.statements)
#         self.database.commit()
# 
#     def validate(self):
#         self.database.open()
#         self.execute(self.statements)
#         self.database.rollback()
# 
#     def execute(self, statements):
#         if not isinstance(statements, list):
#             self.__execute(statements)
#         else:
#             for statement in statements:
#                 self.__execute(statement)
# 
#     def __execute(self, statement):
#         if isinstance(statement, basestring):
#             self.database.execute(statement)
#         else:
#             query = statement[0]
#             params = statement[1:]
#             self.database.execute(query, tuple([AsIs(x) for x in params]))


import unittest
from mock import Mock
from arbalest.redshift.step import SqlStep


class SqlStepShould(unittest.TestCase):
    def assert_sql(self, database, select, select_with_where):
        actual_select_with_where = database.execute.call_args_list[0][0]
        actual_select = database.execute.call_args_list[1][0]
        self.assertEqual(2, len(actual_select_with_where))
        self.assertEqual(1, len(actual_select))
        self.assertEqual(select_with_where[0], actual_select_with_where[0])
        self.assertEqual(select, actual_select[0])

    def test_execute_statements_and_commit_on_run(self):
        database = Mock()
        select_with_where = (
            'SELECT * FROM some_table WHERE some_column1 = %s', "some_value")
        select = ('SELECT * FROM some_table')
        step = SqlStep(database, select_with_where, select)

        step.run()

        database.open.assert_called_once_with()
        self.assert_sql(database, select, select_with_where)
        database.commit.assert_called_once_with()

    def test_execute_statements_and_rollback_on_validate(self):
        database = Mock()
        select_with_where = (
            'SELECT * FROM some_table WHERE some_column1 = %s', "some_value")
        select = ('SELECT * FROM some_table')

# Path: arbalest/redshift/step.py
# class SqlStep(PipelineStep):
#     def __init__(self, database, *args):
#         self.database = database
#         self.statements = list(args)
# 
#     def run(self):
#         self.database.open()
#         self.execute(self.statements)
#         self.database.commit()
# 
#     def validate(self):
#         self.database.open()
#         self.execute(self.statements)
#         self.database.rollback()
# 
#     def execute(self, statements):
#         if not isinstance(statements, list):
#             self.__execute(statements)
#         else:
#             for statement in statements:
#                 self.__execute(statement)
# 
#     def __execute(self, statement):
#         if isinstance(statement, basestring):
#             self.database.execute(statement)
#         else:
#             query = statement[0]
#             params = statement[1:]
#             self.database.execute(query, tuple([AsIs(x) for x in params]))


# Path: test/redshift/test_sql_step.py
import unittest
from mock import Mock
from arbalest.redshift.step import SqlStep



class SqlStepShould(unittest.TestCase):
    def assert_sql(self, database, select, select_with_where):
        actual_select_with_where = database.execute.call_args_list[0][0]
        actual_select = database.execute.call_args_list[1][0]
        self.assertEqual(2, len(actual_select_with_where))
        self.assertEqual(1, len(actual_select))
        self.assertEqual(select_with_where[0], actual_select_with_where[0])
        self.assertEqual(select, actual_select[0])

    def test_execute_statements_and_commit_on_run(self):
        database = Mock()
        select_with_where = (
            'SELECT * FROM some_table WHERE some_column1 = %s', "some_value")
        select = ('SELECT * FROM some_table')
        step = SqlStep(database, select_with_where, select)

        step.run()

        database.open.assert_called_once_with()
        self.assert_sql(database, select, select_with_where)
        database.commit.assert_called_once_with()

    def test_execute_statements_and_rollback_on_validate(self):
        database = Mock()
        select_with_where = (
            'SELECT * FROM some_table WHERE some_column1 = %s', "some_value")
        select = ('SELECT * FROM some_table')

        step = SqlStep(database, select_with_where, select)