@@ -62,6 +62,43 @@ def test_execute_query_with_exception(
6262 test_instance .conn .rollback .assert_called_once ()
6363 mock_cursor .close .assert_called_once ()
6464
65+ def test_execute_transaction (self , mock_redshift_conn , test_instance ,
66+ mocker ):
67+ test_instance .connect ()
68+
69+ mock_cursor = mocker .MagicMock ()
70+ test_instance .conn .cursor .return_value = mock_cursor
71+
72+ test_instance .execute_transaction ([('query 1' , None ),
73+ ('query 2 %s %s' , ('a' , 1 ))])
74+ mock_cursor .execute .assert_has_calls ([
75+ mocker .call ('BEGIN TRANSACTION;' ),
76+ mocker .call ('query 1' , None ),
77+ mocker .call ('query 2 %s %s' , ('a' , 1 )),
78+ mocker .call ('END TRANSACTION;' )])
79+ test_instance .conn .commit .assert_called_once ()
80+ mock_cursor .close .assert_called_once ()
81+
82+ def test_execute_transaction_with_exception (
83+ self , mock_redshift_conn , test_instance , mocker ):
84+ test_instance .connect ()
85+
86+ mock_cursor = mocker .MagicMock ()
87+ mock_cursor .execute .side_effect = [None , None , Exception ()]
88+ test_instance .conn .cursor .return_value = mock_cursor
89+
90+ with pytest .raises (RedshiftClientError ):
91+ test_instance .execute_transaction (
92+ [('query 1' , None ), ('query 2' , None )])
93+
94+ mock_cursor .execute .assert_has_calls ([
95+ mocker .call ('BEGIN TRANSACTION;' ),
96+ mocker .call ('query 1' , None ),
97+ mocker .call ('query 2' , None )])
98+ test_instance .conn .commit .assert_not_called ()
99+ test_instance .conn .rollback .assert_called_once ()
100+ mock_cursor .close .assert_called_once ()
101+
65102 def test_close_connection (self , mock_redshift_conn , test_instance ):
66103 test_instance .connect ()
67104 test_instance .close_connection ()
0 commit comments