Browse Source

Add raise unit test for search tuple in sqlalchemy

Change-Id: I60a2305b97f73b011496a8c02e115ab339d130f9
tags/7.1.0
gengchc2 8 months ago
parent
commit
edb258dbb6

+ 27
- 0
freezer_api/tests/unit/sqlalchemy/v1/test_api_common.py View File

@@ -168,3 +168,30 @@ class ApiTestCase(base.DbTestCase):
168 168
         self.assertRaises(freezer_api_exc.StorageEngineError,
169 169
                           api.replace_tuple, models.Job, self.fake_user_id,
170 170
                           self.fake_job_id, mock_tuple_values)
171
+
172
+    @patch('oslo_db.sqlalchemy.utils.model_query')
173
+    def test_raises_search_tuple(self, mock_model_query):
174
+        mock_tablename = mock.MagicMock()
175
+        mock_tablename = mock.MagicMock()
176
+        mock_search = mock.MagicMock()
177
+        mock_model_query.side_effect = Exception('regular test failure')
178
+        self.assertRaises(freezer_api_exc.StorageEngineError,
179
+                          api.search_tuple, mock_tablename, self.fake_user_id,
180
+                          offset=0, limit=100,
181
+                          search=mock_search)
182
+
183
+    def test_valid_and_get_search_option(self):
184
+        search = {'error_key': 'search_info_error'}
185
+        result = api.valid_and_get_search_option(search)
186
+        self.assertEqual({}, result)
187
+
188
+    def test_get_recursively_list_include_dict(self):
189
+        dict1 = {'key1': [{'key11': {'key111': 111}},
190
+                          {'key12': {'key121': 121}}],
191
+                 'key2': 2
192
+                 }
193
+        search_key = {'key111': 111,
194
+                      'key121': 121,
195
+                      'key2': 2}
196
+        search_keys_found = api.get_recursively(dict1, search_key)
197
+        self.assertEqual(search_key, search_keys_found)

+ 27
- 0
freezer_api/tests/unit/sqlalchemy/v2/test_api_common.py View File

@@ -179,3 +179,30 @@ class ApiTestCase(base.DbTestCase):
179 179
                           api.replace_tuple, models.Job, self.fake_user_id,
180 180
                           self.fake_job_id, mock_tuple_values,
181 181
                           project_id=self.fake_project_id)
182
+
183
+    @patch('oslo_db.sqlalchemy.utils.model_query')
184
+    def test_raises_search_tuple(self, mock_model_query):
185
+        mock_tablename = mock.MagicMock()
186
+        mock_tablename = mock.MagicMock()
187
+        mock_search = mock.MagicMock()
188
+        mock_model_query.side_effect = Exception('regular test failure')
189
+        self.assertRaises(freezer_api_exc.StorageEngineError,
190
+                          api.search_tuple, mock_tablename, self.fake_user_id,
191
+                          project_id=self.fake_project_id, offset=0, limit=100,
192
+                          search=mock_search)
193
+
194
+    def test_valid_and_get_search_option(self):
195
+        search = {'error_key': 'search_info_error'}
196
+        result = api.valid_and_get_search_option(search)
197
+        self.assertEqual({}, result)
198
+
199
+    def test_get_recursively_list_include_dict(self):
200
+        dict1 = {'key1': [{'key11': {'key111': 111}},
201
+                          {'key12': {'key121': 121}}],
202
+                 'key2': 2
203
+                 }
204
+        search_key = {'key111': 111,
205
+                      'key121': 121,
206
+                      'key2': 2}
207
+        search_keys_found = api.get_recursively(dict1, search_key)
208
+        self.assertEqual(search_key, search_keys_found)

Loading…
Cancel
Save