-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_scan.py
More file actions
591 lines (509 loc) · 19.2 KB
/
test_scan.py
File metadata and controls
591 lines (509 loc) · 19.2 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
from __future__ import annotations
import os
import tempfile
import textwrap
from unittest.mock import patch
import pytest
from datacustomcode.scan import (
DataAccessLayerCalls,
dc_config_json_from_file,
scan_file,
scan_file_for_imports,
write_requirements_file,
)
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
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:
@patch(
"datacustomcode.scan.DATA_TRANSFORM_CONFIG_TEMPLATE",
{
"sdkVersion": "1.2.3",
"entryPoint": "",
"dataspace": "default",
"permissions": {
"read": {},
"write": {},
},
},
)
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)
try:
result = dc_config_json_from_file(temp_path)
assert result["entryPoint"] == os.path.basename(temp_path)
assert result["dataspace"] == "default"
assert result["sdkVersion"] == "1.2.3" # From mocked version
assert result["permissions"]["read"]["dlo"] == ["input_dlo"]
assert result["permissions"]["write"]["dlo"] == ["output_dlo"]
finally:
os.remove(temp_path)
@patch(
"datacustomcode.scan.DATA_TRANSFORM_CONFIG_TEMPLATE",
{
"sdkVersion": "1.2.3",
"entryPoint": "",
"dataspace": "default",
"permissions": {
"read": {},
"write": {},
},
},
)
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)
try:
config = dc_config_json_from_file(temp_path)
assert config["entryPoint"] == os.path.basename(temp_path)
assert config["dataspace"] == "default"
assert config["sdkVersion"] == "1.2.3" # From mocked version
assert config["permissions"]["read"]["dmo"] == ["input_dmo"]
assert config["permissions"]["write"]["dmo"] == ["output_dmo"]
finally:
os.remove(temp_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
config = dc_config_json_from_file(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)