44from st2common .runners .base_action import Action
55from update import SQLUpdateAction
66
7- # import mock
7+ import mock
88
99__all__ = [
1010 'TestActionSQLUpdateAction'
@@ -20,3 +20,94 @@ def test_init(self):
2020 self .assertIsInstance (action , SQLUpdateAction )
2121 self .assertIsInstance (action , BaseAction )
2222 self .assertIsInstance (action , Action )
23+
24+ @mock .patch ('lib.base_action.BaseAction.connect_to_db' )
25+ @mock .patch ('update.sqlalchemy' )
26+ def test_run_object (self , mock_sqlalchemy , mock_connect_to_db ):
27+ action = self .get_action_instance (self .config_good )
28+ connection_name = 'full'
29+ connection_config = self .config_good ['sql' ][connection_name ]
30+ test_dict = {
31+ 'table' : 'Test_Table' ,
32+ 'where_data' : {
33+ 'column_1' : 'value_1'
34+ },
35+ 'update_data' : {
36+ 'column_2' : 'value_2'
37+ }
38+ }
39+ test_dict .update (connection_config )
40+ execute_dict = {'_column_1' : 'value_1' , 'column_2' : 'value_2' }
41+ expected_result = {'affected_rows' : 1 }
42+ action_meta = 'MetaData'
43+ action_engine = "Engine Data"
44+ mock_connect_to_db .return_value = "Successfully connected"
45+ mock_values = mock .Mock ()
46+ mock_values_return = "Where and Values object"
47+ mock_values .values .return_value = mock_values_return
48+ mock_where = mock .Mock ()
49+ mock_where .where .return_value = mock_values
50+ mock_sql_table = mock .Mock ()
51+ mock_sql_table .update .return_value = mock_where
52+ mock_sql_table .c .get .return_value = "column return"
53+ mock_query_results = mock .Mock (rowcount = 1 )
54+ mock_connection = mock .Mock ()
55+ mock_connection .execute .return_value = mock_query_results
56+ mock_connection .close .return_value = "Successfully disconnected"
57+
58+ action .conn = mock_connection
59+ action .meta = action_meta
60+ action .engine = action_engine
61+ mock_sqlalchemy .Table .return_value = mock_sql_table
62+
63+ result = action .run (** test_dict )
64+ self .assertEqual (result , expected_result )
65+ mock_connect_to_db .assert_called_once_with (connection_config )
66+ mock_sqlalchemy .Table .assert_called_once_with (test_dict ['table' ],
67+ action_meta ,
68+ autoload = True ,
69+ autoload_with = action_engine )
70+ mock_connection .execute .assert_called_once_with (mock_values_return , execute_dict )
71+
72+ @mock .patch ('lib.base_action.BaseAction.connect_to_db' )
73+ @mock .patch ('update.sqlalchemy' )
74+ def test_run_array (self , mock_sqlalchemy , mock_connect_to_db ):
75+ action = self .get_action_instance (self .config_good )
76+ connection_name = 'full'
77+ connection_config = self .config_good ['sql' ][connection_name ]
78+ test_dict = {
79+ 'connection' : connection_name ,
80+ 'table' : 'Test_Table' ,
81+ 'update_data' : {
82+ 'column_2' : 'value_2'
83+ }
84+ }
85+ expected_result = {'affected_rows' : 20 }
86+ action_meta = 'MetaData'
87+ action_engine = "Engine Data"
88+ mock_connect_to_db .return_value = "Successfully connected"
89+ mock_values = mock .Mock ()
90+ mock_values_return = "Where and Values object"
91+ mock_values .values .return_value = mock_values_return
92+ mock_sql_table = mock .Mock ()
93+ mock_sql_table .update .return_value = mock_values
94+ mock_sql_table .c .get .return_value = "column return"
95+ mock_query_results = mock .Mock (rowcount = 20 )
96+ mock_connection = mock .Mock ()
97+ mock_connection .execute .return_value = mock_query_results
98+ mock_connection .close .return_value = "Successfully disconnected"
99+
100+ action .conn = mock_connection
101+ action .meta = action_meta
102+ action .engine = action_engine
103+ mock_sqlalchemy .Table .return_value = mock_sql_table
104+
105+ result = action .run (** test_dict )
106+ self .assertEqual (result , expected_result )
107+ mock_connect_to_db .assert_called_once_with (connection_config )
108+ mock_sqlalchemy .Table .assert_called_once_with (test_dict ['table' ],
109+ action_meta ,
110+ autoload = True ,
111+ autoload_with = action_engine )
112+ mock_connection .execute .assert_called_once_with (mock_values_return ,
113+ test_dict ['update_data' ])
0 commit comments