-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_scan.py
More file actions
929 lines (794 loc) · 31.6 KB
/
test_scan.py
File metadata and controls
929 lines (794 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
from __future__ import annotations
import json
import os
import tempfile
import textwrap
import pytest
from datacustomcode.scan import (
SDK_CONFIG_DIR,
SDK_CONFIG_FILE,
DataAccessLayerCalls,
dc_config_json_from_file,
scan_file,
scan_file_for_imports,
update_config,
write_requirements_file,
write_sdk_config,
)
from datacustomcode.version import get_version
def create_test_script(content: str) -> str:
"""Create a temporary Python file with the given content."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as temp:
temp.write(content)
temp_path = temp.name
return temp_path
def create_sdk_config(base_directory: str, package_type: str = "script") -> str:
"""Create SDK config file for testing.
Args:
base_directory: The base directory where .datacustomcode should be created
package_type: The package type ("script" or "function")
Returns:
Path to the created SDK config file
"""
sdk_config = {"type": package_type}
write_sdk_config(base_directory, sdk_config)
return os.path.join(base_directory, SDK_CONFIG_DIR, SDK_CONFIG_FILE)
class TestClientMethodVisitor:
def test_variable_tracking(self):
"""Test that the visitor can track variable assignments."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
dlo_name = "my_dlo"
dmo_name = "my_dmo"
client.read_dlo(dlo_name)
# Don't mix with DMO reads in the same test
"""
)
temp_path = create_test_script(content)
try:
result = scan_file(temp_path)
assert "my_dlo" in result.read_dlo
assert len(result.read_dmo) == 0 # No DMO reads
finally:
os.unlink(temp_path)
def test_string_literals(self):
"""Test that the visitor can track string literals in method calls."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
client.read_dlo("direct_dlo_1")
client.read_dlo("direct_dlo_2")
"""
)
temp_path = create_test_script(content)
try:
result = scan_file(temp_path)
assert "direct_dlo_1" in result.read_dlo
assert "direct_dlo_2" in result.read_dlo
finally:
os.unlink(temp_path)
def test_cannot_mix_dlo_dmo_reads(self):
"""Test that mixing DLO and DMO reads in the same file raises an error."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
client.read_dlo("direct_dlo")
client.read_dmo("direct_dmo")
"""
)
temp_path = create_test_script(content)
try:
# This should raise a validation error due to mixing DLO and DMO reads
with pytest.raises(
Exception, match="Cannot read from DLO and DMO in the same file"
):
scan_file(temp_path)
finally:
os.unlink(temp_path)
def test_read_both_dlo_dmo_throws_error(self):
"""Test that reading from both DLO and DMO throws an error."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from both DLO and DMO in the same file
df1 = client.read_dlo("some_dlo")
df2 = client.read_dmo("some_dmo")
# This operation should never happen as validation should fail
result = df1.join(df2, "key")
client.write_to_dlo("output", result, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
with pytest.raises(
ValueError, match="Cannot read from DLO and DMO in the same file"
):
scan_file(temp_path)
finally:
os.unlink(temp_path)
class TestScanFile:
def test_valid_dlo_to_dlo(self):
"""Test scanning a file with valid DLO to DLO operations."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from DLO
df = client.read_dlo("input_dlo")
# Transform data
df = df.filter(df.col > 10)
# Write to DLO
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
result = scan_file(temp_path)
assert "input_dlo" in result.read_dlo
assert "output_dlo" in result.write_to_dlo
assert len(result.read_dmo) == 0
assert len(result.write_to_dmo) == 0
finally:
os.unlink(temp_path)
def test_valid_dmo_to_dmo(self):
"""Test scanning a file with valid DMO to DMO operations."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from DMO
df = client.read_dmo("input_dmo")
# Transform data
df = df.filter(df.col > 10)
# Write to DMO
client.write_to_dmo("output_dmo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
result = scan_file(temp_path)
assert "input_dmo" in result.read_dmo
assert "output_dmo" in result.write_to_dmo
assert len(result.read_dlo) == 0
assert len(result.write_to_dlo) == 0
finally:
os.unlink(temp_path)
def test_multiple_reads(self):
"""Test scanning a file with multiple read operations."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from multiple DLOs
df1 = client.read_dlo("input_dlo_1")
df2 = client.read_dlo("input_dlo_2")
# Join dataframes
result_df = df1.join(df2, "key_column")
# Write to DLO
client.write_to_dlo("output_dlo", result_df, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
result = scan_file(temp_path)
assert "input_dlo_1" in result.read_dlo
assert "input_dlo_2" in result.read_dlo
assert "output_dlo" in result.write_to_dlo
assert len(result.read_dmo) == 0
assert len(result.write_to_dmo) == 0
finally:
os.unlink(temp_path)
def test_invalid_mix_dlo_dmo(self):
"""Test scanning a file with invalid mix of DLO and DMO operations."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from DLO
df = client.read_dlo("input_dlo")
# Write to DMO - invalid operation
client.write_to_dmo("output_dmo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
with pytest.raises(
ValueError, match="Cannot read from DLO and write to DMO"
):
scan_file(temp_path)
finally:
os.unlink(temp_path)
def test_read_dmo_write_dlo_throws_error(self):
"""Test that reading from DMO and writing to DLO throws an error."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from DMO
df = client.read_dmo("input_dmo")
# Write to DLO - invalid operation
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
with pytest.raises(
ValueError, match="Cannot read from DMO and write to DLO"
):
scan_file(temp_path)
finally:
os.unlink(temp_path)
def test_multiple_writes(self):
"""Test scanning a file with multiple write operations."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from DLO
df = client.read_dlo("input_dlo")
# Transform data for different outputs
df_filtered = df.filter(df.col > 10)
df_aggregated = df.groupBy("category").agg({"value": "sum"})
# Write to multiple DLOs
client.write_to_dlo("output_filtered", df_filtered, "overwrite")
client.write_to_dlo("output_aggregated", df_aggregated, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
result = scan_file(temp_path)
assert "input_dlo" in result.read_dlo
assert "output_filtered" in result.write_to_dlo
assert "output_aggregated" in result.write_to_dlo
assert len(result.read_dlo) == 1
assert len(result.write_to_dlo) == 2
assert len(result.read_dmo) == 0
assert len(result.write_to_dmo) == 0
finally:
os.unlink(temp_path)
class TestDcConfigJson:
def test_dlo_to_dlo_config(self):
"""Test generating config JSON for DLO to DLO operations."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from DLO
df = client.read_dlo("input_dlo")
# Write to DLO
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
# Create SDK config in the same directory as the script
sdk_config_path = create_sdk_config(file_dir, "script")
try:
result = dc_config_json_from_file(temp_path, "script")
assert result["entryPoint"] == os.path.basename(temp_path)
assert result["dataspace"] == "default"
assert result["sdkVersion"] == get_version()
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
with open(config_path, "w") as f:
json.dump(result, f)
result = update_config(temp_path)
assert result["permissions"]["read"]["dlo"] == ["input_dlo"]
assert result["permissions"]["write"]["dlo"] == ["output_dlo"]
finally:
os.remove(temp_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
def test_dmo_to_dmo_config(self):
"""Test generating config JSON for DMO to DMO operations."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
# Read from DMO
df = client.read_dmo("input_dmo")
# Write to DMO
client.write_to_dmo("output_dmo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
# Create SDK config in the same directory as the script
sdk_config_path = create_sdk_config(file_dir, "script")
try:
config = dc_config_json_from_file(temp_path, "script")
assert config["entryPoint"] == os.path.basename(temp_path)
assert config["dataspace"] == "default"
assert config["sdkVersion"] == get_version()
config = update_config(temp_path)
assert config["permissions"]["read"]["dmo"] == ["input_dmo"]
assert config["permissions"]["write"]["dmo"] == ["output_dmo"]
finally:
os.remove(temp_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
def test_preserves_existing_dataspace(self):
"""Test that existing dataspace value is preserved when config.json exists."""
import json
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
df = client.read_dlo("input_dlo")
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
try:
# Create SDK config
sdk_config_path = create_sdk_config(file_dir, "script")
# Create an existing config.json with a custom dataspace
# (without type field)
existing_config = {
"sdkVersion": "1.0.0",
"entryPoint": "test.py",
"type": "script",
"dataspace": "my_custom_dataspace",
"permissions": {
"read": {"dlo": ["old_dlo"]},
"write": {"dlo": ["old_output"]},
},
}
with open(config_path, "w") as f:
json.dump(existing_config, f)
# Generate new config - should preserve dataspace
result = update_config(temp_path)
assert result["dataspace"] == "my_custom_dataspace"
assert result["permissions"]["read"]["dlo"] == ["input_dlo"]
assert result["permissions"]["write"]["dlo"] == ["output_dlo"]
finally:
os.remove(temp_path)
if os.path.exists(config_path):
os.remove(config_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
def test_uses_default_for_empty_dataspace(self):
"""Test that empty dataspace value uses default and logs warning."""
import json
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
df = client.read_dlo("input_dlo")
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
try:
# Create SDK config
sdk_config_path = create_sdk_config(file_dir, "script")
# Create an existing config.json with empty dataspace (without type field)
existing_config = {
"sdkVersion": "1.0.0",
"entryPoint": "test.py",
"dataspace": "",
"permissions": {
"read": {"dlo": ["old_dlo"]},
"write": {"dlo": ["old_output"]},
},
}
with open(config_path, "w") as f:
json.dump(existing_config, f)
# Should use "default" for empty dataspace (not raise error)
result = update_config(temp_path)
assert result["dataspace"] == "default"
assert result["permissions"]["read"]["dlo"] == ["input_dlo"]
assert result["permissions"]["write"]["dlo"] == ["output_dlo"]
finally:
os.remove(temp_path)
if os.path.exists(config_path):
os.remove(config_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
def test_uses_default_dataspace_when_no_config(self):
"""Test missing config.json uses default dataspace."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
df = client.read_dlo("input_dlo")
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
try:
# No existing config.json - should use "default" dataspace
result = dc_config_json_from_file(temp_path, "script")
assert result["dataspace"] == "default"
finally:
os.remove(temp_path)
def test_rejects_missing_dataspace(self):
"""Test that config.json missing dataspace field raises ValueError."""
import json
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
df = client.read_dlo("input_dlo")
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
try:
# Create SDK config
sdk_config_path = create_sdk_config(file_dir, "script")
# Create an existing config.json without dataspace field
# (without type field)
existing_config = {
"sdkVersion": "1.0.0",
"entryPoint": "test.py",
"permissions": {
"read": {"dlo": ["old_dlo"]},
"write": {"dlo": ["old_output"]},
},
}
with open(config_path, "w") as f:
json.dump(existing_config, f)
# Should raise ValueError when dataspace field is missing
with pytest.raises(
ValueError,
match="dataspace must be defined. Please add a 'dataspace' field to "
"the config.json file.",
):
update_config(temp_path)
finally:
os.remove(temp_path)
if os.path.exists(config_path):
os.remove(config_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
def test_raises_error_on_invalid_json(self):
"""Test that invalid JSON in config.json raises an error."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
df = client.read_dlo("input_dlo")
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
try:
# Create an invalid JSON file
with open(config_path, "w") as f:
f.write("{ invalid json }")
# Should raise ValueError for invalid JSON
with pytest.raises(ValueError, match="Failed to parse JSON"):
update_config(temp_path)
finally:
os.remove(temp_path)
if os.path.exists(config_path):
os.remove(config_path)
def test_update_config_updates_entrypoint(self):
"""
Test that update_config() updates the entryPoint field
when scanning a renamed file.
"""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
df = client.read_dlo("input_dlo")
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
try:
sdk_config_path = create_sdk_config(file_dir, "script")
initial_config = {
"sdkVersion": "1.0.0",
"entryPoint": "old_entrypoint.py",
"dataspace": "custom_dataspace",
"permissions": {
"read": {"dlo": ["old_dlo"]},
"write": {"dlo": ["old_output"]},
},
}
with open(config_path, "w") as f:
json.dump(initial_config, f)
updated_config = update_config(temp_path)
assert updated_config["entryPoint"] == os.path.basename(temp_path)
assert updated_config["dataspace"] == "custom_dataspace"
assert updated_config["permissions"]["read"]["dlo"] == ["input_dlo"]
assert updated_config["permissions"]["write"]["dlo"] == ["output_dlo"]
finally:
os.remove(temp_path)
if os.path.exists(config_path):
os.remove(config_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
def test_update_entrypoint_with_absolute_path(self):
"""Test that entryPoint uses basename even when file_path is absolute."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
client = Client()
df = client.read_dlo("input_dlo")
client.write_to_dlo("output_dlo", df, "overwrite")
"""
)
temp_path = create_test_script(content)
assert os.path.isabs(temp_path), "Test requires absolute path"
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
try:
sdk_config_path = create_sdk_config(file_dir, "script")
initial_config = {
"sdkVersion": "1.0.0",
"entryPoint": "old.py",
"dataspace": "default",
"permissions": {"read": {}, "write": {}},
}
with open(config_path, "w") as f:
json.dump(initial_config, f)
updated_config = update_config(temp_path)
assert updated_config["entryPoint"] == os.path.basename(temp_path)
assert "/" not in updated_config["entryPoint"]
finally:
os.remove(temp_path)
if os.path.exists(config_path):
os.remove(config_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
def test_update_entrypoint_preserves_function_type(self):
"""Test that entryPoint update works for 'function' package type."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
def my_function(event, context):
return {"statusCode": 200}
"""
)
temp_path = create_test_script(content)
file_dir = os.path.dirname(temp_path)
config_path = os.path.join(file_dir, "config.json")
try:
sdk_config_path = create_sdk_config(file_dir, "function")
initial_config = {
"entryPoint": "old_function.py",
}
with open(config_path, "w") as f:
json.dump(initial_config, f)
updated_config = update_config(temp_path)
assert updated_config["entryPoint"] == os.path.basename(temp_path)
finally:
os.remove(temp_path)
if os.path.exists(config_path):
os.remove(config_path)
if os.path.exists(sdk_config_path):
os.remove(sdk_config_path)
os.rmdir(os.path.dirname(sdk_config_path))
class TestDataAccessLayerCalls:
"""Tests for the DataAccessLayerCalls class directly."""
def test_input_str_output_str_properties(self):
"""Test the input_str and output_str properties."""
# Test with DLO
dlo_calls = DataAccessLayerCalls(
read_dlo=frozenset(["input_dlo"]),
read_dmo=frozenset(),
write_to_dlo=frozenset(["output_dlo"]),
write_to_dmo=frozenset(),
)
assert dlo_calls.input_str == "input_dlo"
assert dlo_calls.output_str == "output_dlo"
# Test with DMO
dmo_calls = DataAccessLayerCalls(
read_dlo=frozenset(),
read_dmo=frozenset(["input_dmo"]),
write_to_dlo=frozenset(),
write_to_dmo=frozenset(["output_dmo"]),
)
assert dmo_calls.input_str == "input_dmo"
assert dmo_calls.output_str == "output_dmo"
def test_real_world_example():
"""Test scanning a more complex, real-world example script."""
content = textwrap.dedent(
"""
from datacustomcode.client import Client
from pyspark.sql.functions import col, expr
# Constants
SOURCE_DLO = "customer_data_raw"
TARGET_DLO = "customer_data_enriched"
def process_customer_data():
# Initialize client
client = Client()
# Read data from DLO
customer_df = client.read_dlo(SOURCE_DLO)
# Perform transformations
enriched_df = (
customer_df
.filter(col("age") > 18)
.withColumn("full_name", expr("concat(first_name, ' ', last_name)"))
.withColumn("age_group", expr("case when age < 30 then 'Young' " +
"when age < 60 then 'Middle-aged' " +
"else 'Senior' end"))
.drop("first_name", "last_name")
)
# Write transformed data back to a different DLO
client.write_to_dlo(TARGET_DLO, enriched_df, "overwrite")
return "Processing complete"
if __name__ == "__main__":
process_customer_data()
"""
)
temp_path = create_test_script(content)
try:
result = scan_file(temp_path)
assert "customer_data_raw" in result.read_dlo
assert "customer_data_enriched" in result.write_to_dlo
result = dc_config_json_from_file(temp_path, "script")
config_path = os.path.join(os.path.dirname(temp_path), "config.json")
with open(config_path, "w") as f:
json.dump(result, f)
config = update_config(temp_path)
assert config["permissions"]["read"]["dlo"] == ["customer_data_raw"]
assert config["permissions"]["write"]["dlo"] == ["customer_data_enriched"]
finally:
os.unlink(temp_path)
class TestRequirementsFile:
def test_scan_file_for_imports(self):
"""Test scanning a file for external package imports."""
content = textwrap.dedent(
"""
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import os # Standard library
import sys # Standard library
from datacustomcode.client import Client # Internal package
"""
)
temp_path = create_test_script(content)
try:
imports = scan_file_for_imports(temp_path)
assert "pandas" in imports
assert "numpy" in imports
assert "sklearn" in imports
assert "os" not in imports # Standard library
assert "sys" not in imports # Standard library
assert "datacustomcode" not in imports # Internal package
finally:
os.unlink(temp_path)
def test_write_requirements_file_new(self):
"""Test writing a new requirements.txt file."""
# Create a temporary directory structure
temp_dir = tempfile.mkdtemp()
script_dir = os.path.join(temp_dir, "script_dir")
os.makedirs(script_dir)
content = textwrap.dedent(
"""
import pandas as pd
import numpy as np
"""
)
temp_path = os.path.join(script_dir, "test_script.py")
with open(temp_path, "w") as f:
f.write(content)
requirements_path = None
try:
requirements_path = write_requirements_file(temp_path)
assert os.path.exists(requirements_path)
assert (
os.path.dirname(requirements_path) == temp_dir
) # Should be in parent directory
with open(requirements_path, "r") as f:
requirements = {line.strip() for line in f}
assert "pandas" in requirements
assert "numpy" in requirements
finally:
if os.path.exists(temp_path):
os.unlink(temp_path)
if requirements_path and os.path.exists(requirements_path):
os.unlink(requirements_path)
os.rmdir(script_dir)
os.rmdir(temp_dir)
def test_write_requirements_file_merge(self):
"""Test merging with existing requirements.txt file."""
# Create a temporary directory structure
temp_dir = tempfile.mkdtemp()
script_dir = os.path.join(temp_dir, "script_dir")
os.makedirs(script_dir)
# Create existing requirements.txt in parent directory
existing_requirements = os.path.join(temp_dir, "requirements.txt")
with open(existing_requirements, "w") as f:
f.write("pandas\nnumpy\n")
# Create a new Python file with additional imports
content = textwrap.dedent(
"""
import pandas as pd
import numpy as np
import scipy
import matplotlib
"""
)
temp_path = os.path.join(script_dir, "test_script.py")
with open(temp_path, "w") as f:
f.write(content)
requirements_path = None
try:
requirements_path = write_requirements_file(temp_path)
assert os.path.exists(requirements_path)
assert (
os.path.dirname(requirements_path) == temp_dir
) # Should be in parent directory
with open(requirements_path, "r") as f:
requirements = {line.strip() for line in f}
# Check that both existing and new requirements are present
assert "pandas" in requirements
assert "numpy" in requirements
assert "scipy" in requirements
assert "matplotlib" in requirements
finally:
if os.path.exists(temp_path):
os.unlink(temp_path)
if requirements_path and os.path.exists(requirements_path):
os.unlink(requirements_path)
if os.path.exists(existing_requirements):
os.unlink(existing_requirements)
os.rmdir(script_dir)
os.rmdir(temp_dir)
def test_standard_library_exclusion(self):
"""Test that standard library imports are excluded."""
content = textwrap.dedent(
"""
import os
import sys
import json
import datetime
import pandas as pd
"""
)
temp_path = create_test_script(content)
try:
imports = scan_file_for_imports(temp_path)
assert "pandas" in imports
assert "os" not in imports
assert "sys" not in imports
assert "json" not in imports
assert "datetime" not in imports
finally:
os.unlink(temp_path)
def test_excluded_packages(self):
"""Test that excluded packages are not included in requirements."""
content = textwrap.dedent(
"""
import datacustomcode
import pyspark
import pandas as pd
"""
)
temp_path = create_test_script(content)
try:
imports = scan_file_for_imports(temp_path)
assert "pandas" in imports
assert "datacustomcode" not in imports
assert "pyspark" not in imports
finally:
os.unlink(temp_path)