99import pytest
1010
1111from datacustomcode .scan import (
12+ SDK_CONFIG_DIR ,
1213 DataAccessLayerCalls ,
1314 dc_config_json_from_file ,
1415 scan_file ,
1516 scan_file_for_imports ,
1617 update_config ,
1718 write_requirements_file ,
19+ write_sdk_config ,
1820)
1921from datacustomcode .version import get_version
2022
@@ -27,6 +29,21 @@ def create_test_script(content: str) -> str:
2729 return temp_path
2830
2931
32+ def create_sdk_config (base_directory : str , package_type : str = "script" ) -> str :
33+ """Create SDK config file for testing.
34+
35+ Args:
36+ base_directory: The base directory where .datacustomcode should be created
37+ package_type: The package type ("script" or "function")
38+
39+ Returns:
40+ Path to the created SDK config file
41+ """
42+ sdk_config = {"type" : package_type }
43+ write_sdk_config (base_directory , sdk_config )
44+ return os .path .join (base_directory , SDK_CONFIG_DIR , "config.json" )
45+
46+
3047class TestClientMethodVisitor :
3148 def test_variable_tracking (self ):
3249 """Test that the visitor can track variable assignments."""
@@ -301,6 +318,9 @@ def test_dlo_to_dlo_config(self):
301318 """
302319 )
303320 temp_path = create_test_script (content )
321+ file_dir = os .path .dirname (temp_path )
322+ # Create SDK config in the same directory as the script
323+ sdk_config_path = create_sdk_config (file_dir , "script" )
304324 try :
305325 result = dc_config_json_from_file (temp_path , "script" )
306326 assert result ["entryPoint" ] == os .path .basename (temp_path )
@@ -315,6 +335,9 @@ def test_dlo_to_dlo_config(self):
315335 assert result ["permissions" ]["write" ]["dlo" ] == ["output_dlo" ]
316336 finally :
317337 os .remove (temp_path )
338+ if os .path .exists (sdk_config_path ):
339+ os .remove (sdk_config_path )
340+ os .rmdir (os .path .dirname (sdk_config_path ))
318341
319342 def test_dmo_to_dmo_config (self ):
320343 """Test generating config JSON for DMO to DMO operations."""
@@ -332,6 +355,9 @@ def test_dmo_to_dmo_config(self):
332355 """
333356 )
334357 temp_path = create_test_script (content )
358+ file_dir = os .path .dirname (temp_path )
359+ # Create SDK config in the same directory as the script
360+ sdk_config_path = create_sdk_config (file_dir , "script" )
335361 try :
336362 config = dc_config_json_from_file (temp_path , "script" )
337363 assert config ["entryPoint" ] == os .path .basename (temp_path )
@@ -342,6 +368,9 @@ def test_dmo_to_dmo_config(self):
342368 assert config ["permissions" ]["write" ]["dmo" ] == ["output_dmo" ]
343369 finally :
344370 os .remove (temp_path )
371+ if os .path .exists (sdk_config_path ):
372+ os .remove (sdk_config_path )
373+ os .rmdir (os .path .dirname (sdk_config_path ))
345374
346375 @patch (
347376 "datacustomcode.scan.DATA_TRANSFORM_CONFIG_TEMPLATE" ,
@@ -373,7 +402,10 @@ def test_preserves_existing_dataspace(self):
373402 config_path = os .path .join (file_dir , "config.json" )
374403
375404 try :
405+ # Create SDK config
406+ sdk_config_path = create_sdk_config (file_dir , "script" )
376407 # Create an existing config.json with a custom dataspace
408+ # (without type field)
377409 existing_config = {
378410 "sdkVersion" : "1.0.0" ,
379411 "entryPoint" : "test.py" ,
@@ -396,11 +428,13 @@ def test_preserves_existing_dataspace(self):
396428 os .remove (temp_path )
397429 if os .path .exists (config_path ):
398430 os .remove (config_path )
431+ if os .path .exists (sdk_config_path ):
432+ os .remove (sdk_config_path )
433+ os .rmdir (os .path .dirname (sdk_config_path ))
399434
400- def test_uses_default_for_empty_dataspace (self , caplog ):
435+ def test_uses_default_for_empty_dataspace (self ):
401436 """Test that empty dataspace value uses default and logs warning."""
402437 import json
403- import logging
404438
405439 content = textwrap .dedent (
406440 """
@@ -416,11 +450,12 @@ def test_uses_default_for_empty_dataspace(self, caplog):
416450 config_path = os .path .join (file_dir , "config.json" )
417451
418452 try :
419- # Create an existing config.json with empty dataspace
453+ # Create SDK config
454+ sdk_config_path = create_sdk_config (file_dir , "script" )
455+ # Create an existing config.json with empty dataspace (without type field)
420456 existing_config = {
421457 "sdkVersion" : "1.0.0" ,
422458 "entryPoint" : "test.py" ,
423- "type" : "script" ,
424459 "dataspace" : "" ,
425460 "permissions" : {
426461 "read" : {"dlo" : ["old_dlo" ]},
@@ -431,24 +466,19 @@ def test_uses_default_for_empty_dataspace(self, caplog):
431466 json .dump (existing_config , f )
432467
433468 # Should use "default" for empty dataspace (not raise error)
434- with caplog .at_level (logging .WARNING ):
435- result = update_config (temp_path )
469+ result = update_config (temp_path )
436470
437471 assert result ["dataspace" ] == "default"
438472 assert result ["permissions" ]["read" ]["dlo" ] == ["input_dlo" ]
439473 assert result ["permissions" ]["write" ]["dlo" ] == ["output_dlo" ]
440474
441- # Verify that a warning was logged
442- assert len (caplog .records ) > 0
443- assert any (
444- "dataspace" in record .message .lower ()
445- and "empty" in record .message .lower ()
446- for record in caplog .records
447- )
448475 finally :
449476 os .remove (temp_path )
450477 if os .path .exists (config_path ):
451478 os .remove (config_path )
479+ if os .path .exists (sdk_config_path ):
480+ os .remove (sdk_config_path )
481+ os .rmdir (os .path .dirname (sdk_config_path ))
452482
453483 def test_uses_default_dataspace_when_no_config (self ):
454484 """Test missing config.json uses default dataspace."""
@@ -488,11 +518,13 @@ def test_rejects_missing_dataspace(self):
488518 config_path = os .path .join (file_dir , "config.json" )
489519
490520 try :
521+ # Create SDK config
522+ sdk_config_path = create_sdk_config (file_dir , "script" )
491523 # Create an existing config.json without dataspace field
524+ # (without type field)
492525 existing_config = {
493526 "sdkVersion" : "1.0.0" ,
494527 "entryPoint" : "test.py" ,
495- "type" : "script" ,
496528 "permissions" : {
497529 "read" : {"dlo" : ["old_dlo" ]},
498530 "write" : {"dlo" : ["old_output" ]},
@@ -512,6 +544,9 @@ def test_rejects_missing_dataspace(self):
512544 os .remove (temp_path )
513545 if os .path .exists (config_path ):
514546 os .remove (config_path )
547+ if os .path .exists (sdk_config_path ):
548+ os .remove (sdk_config_path )
549+ os .rmdir (os .path .dirname (sdk_config_path ))
515550
516551 def test_raises_error_on_invalid_json (self ):
517552 """Test that invalid JSON in config.json raises an error."""
0 commit comments