1010from fabric_cli .core .fab_exceptions import FabricCLIError
1111from fabric_cli .errors import ErrorMessages
1212from fabric_cli .utils .fab_cmd_mkdir_utils import (
13+ add_type_specific_payload ,
1314 find_mpe_connection ,
1415 get_connection_config_from_params ,
16+ get_params_per_item_type ,
1517)
18+ from fabric_cli .core .fab_types import ItemType
1619
1720
1821def test_fabric_data_pipelines_workspace_identity_no_params_success ():
@@ -209,4 +212,147 @@ def test_find_mpe_connection_return_403_success(self):
209212 called_url = call_args .args [1 ] if len (call_args .args ) > 1 else call_args .kwargs ['url' ]
210213 assert "privateEndpointConnections" in called_url
211214 assert "api-version=2023-11-01" in called_url
215+
216+
217+ class TestGetParamsPerItemTypeSqlDatabase :
218+ """Test cases for get_params_per_item_type with SQL_DATABASE."""
219+
220+ def test_sql_database_returns_optional_params (self ):
221+ """Test that SQL_DATABASE returns the correct optional params."""
222+ mock_item = Mock ()
223+ mock_item .item_type = ItemType .SQL_DATABASE
224+
225+ required , optional = get_params_per_item_type (mock_item )
226+
227+ assert required == []
228+ assert optional == ["mode" , "backupRetentionDays" , "collation" ]
229+
230+ def test_sql_database_has_no_required_params (self ):
231+ """Test that SQL_DATABASE has no required params."""
232+ mock_item = Mock ()
233+ mock_item .item_type = ItemType .SQL_DATABASE
234+
235+ required , _ = get_params_per_item_type (mock_item )
236+
237+ assert len (required ) == 0
238+
239+
240+ class TestAddTypeSpecificPayloadSqlDatabase :
241+ """Test cases for add_type_specific_payload with SQL_DATABASE."""
242+
243+ def _make_item_and_args (self , params ):
244+ """Helper to create mock item and args for SQL_DATABASE."""
245+ mock_item = Mock ()
246+ mock_item .item_type = ItemType .SQL_DATABASE
247+ mock_args = Namespace (params = params )
248+ return mock_item , mock_args
249+
250+ def test_all_params_success (self ):
251+ """Test SQL_DATABASE with all params provided."""
252+ item , args = self ._make_item_and_args (
253+ {
254+ "mode" : "new" ,
255+ "backupretentiondays" : "21" ,
256+ "collation" : "SQL_Latin1_General_CP1_CI_AS" ,
257+ }
258+ )
259+ payload = {"displayName" : "testdb" }
260+
261+ result = add_type_specific_payload (item , args , payload )
262+
263+ assert "creationPayload" in result
264+ cp = result ["creationPayload" ]
265+ assert cp ["creationMode" ] == "new"
266+ assert cp ["backupRetentionDays" ] == 21
267+ assert cp ["collation" ] == "SQL_Latin1_General_CP1_CI_AS"
268+
269+ def test_partial_params_backup_only_success (self ):
270+ """Test SQL_DATABASE with only backupRetentionDays provided."""
271+ item , args = self ._make_item_and_args ({"backupretentiondays" : "7" })
272+ payload = {"displayName" : "testdb" }
273+
274+ result = add_type_specific_payload (item , args , payload )
275+
276+ assert "creationPayload" in result
277+ cp = result ["creationPayload" ]
278+ assert cp ["creationMode" ] == "new" # Default
279+ assert cp ["backupRetentionDays" ] == 7
280+ assert "collation" not in cp
281+
282+ def test_partial_params_collation_only_success (self ):
283+ """Test SQL_DATABASE with only collation provided."""
284+ item , args = self ._make_item_and_args (
285+ {"collation" : "SQL_Latin1_General_CP1_CI_AS" }
286+ )
287+ payload = {"displayName" : "testdb" }
288+
289+ result = add_type_specific_payload (item , args , payload )
290+
291+ assert "creationPayload" in result
292+ cp = result ["creationPayload" ]
293+ assert cp ["creationMode" ] == "new" # Default
294+ assert "backupRetentionDays" not in cp
295+ assert cp ["collation" ] == "SQL_Latin1_General_CP1_CI_AS"
296+
297+ def test_no_recognized_params_no_creation_payload (self ):
298+ """Test SQL_DATABASE with no recognized params produces no creationPayload."""
299+ item , args = self ._make_item_and_args ({"unrelated" : "value" })
300+ payload = {"displayName" : "testdb" }
301+
302+ result = add_type_specific_payload (item , args , payload )
303+
304+ assert "creationPayload" not in result
305+
306+ def test_empty_params_no_creation_payload (self ):
307+ """Test SQL_DATABASE with empty params dict produces no creationPayload."""
308+ item , args = self ._make_item_and_args ({})
309+ payload = {"displayName" : "testdb" }
310+
311+ result = add_type_specific_payload (item , args , payload )
312+
313+ assert "creationPayload" not in result
314+
315+ def test_invalid_backup_retention_days_failure (self ):
316+ """Test SQL_DATABASE with non-integer backupRetentionDays raises FabricCLIError."""
317+ item , args = self ._make_item_and_args ({"backupretentiondays" : "abc" })
318+ payload = {"displayName" : "testdb" }
319+
320+ with pytest .raises (FabricCLIError ) as exc_info :
321+ add_type_specific_payload (item , args , payload )
322+
323+ assert "backupRetentionDays" in str (exc_info .value .message )
324+ assert "abc" in str (exc_info .value .message )
325+ assert exc_info .value .status_code == fab_constant .ERROR_INVALID_INPUT
326+
327+ def test_explicit_mode_success (self ):
328+ """Test SQL_DATABASE with explicit mode value."""
329+ item , args = self ._make_item_and_args ({"mode" : "copy" })
330+ payload = {"displayName" : "testdb" }
331+
332+ result = add_type_specific_payload (item , args , payload )
333+
334+ assert result ["creationPayload" ]["creationMode" ] == "copy"
335+
336+ def test_mode_only_success (self ):
337+ """Test SQL_DATABASE with only mode provided."""
338+ item , args = self ._make_item_and_args ({"mode" : "new" })
339+ payload = {"displayName" : "testdb" }
340+
341+ result = add_type_specific_payload (item , args , payload )
342+
343+ cp = result ["creationPayload" ]
344+ assert cp ["creationMode" ] == "new"
345+ assert "backupRetentionDays" not in cp
346+ assert "collation" not in cp
347+
348+ def test_preserves_existing_payload_fields (self ):
349+ """Test that SQL_DATABASE creation doesn't remove existing payload fields."""
350+ item , args = self ._make_item_and_args ({"mode" : "new" })
351+ payload = {"displayName" : "testdb" , "description" : "A test database" }
352+
353+ result = add_type_specific_payload (item , args , payload )
354+
355+ assert result ["displayName" ] == "testdb"
356+ assert result ["description" ] == "A test database"
357+ assert "creationPayload" in result
212358
0 commit comments