-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_test_files.py
More file actions
executable file
·558 lines (461 loc) · 20.5 KB
/
Copy pathgenerate_test_files.py
File metadata and controls
executable file
·558 lines (461 loc) · 20.5 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
#!/usr/bin/env python3
"""
Script to generate test files for untested functions in cosmotech/coal/.
This script identifies functions in the cosmotech/coal/ module that don't have
corresponding tests and generates test files for them based on a template.
"""
import argparse
import ast
import os
import re
from collections import defaultdict
from pathlib import Path
def get_functions_from_file(file_path):
"""Extract all function and class definitions from a Python file."""
with open(file_path, "r", encoding="utf-8") as f:
try:
tree = ast.parse(f.read())
except SyntaxError:
print(f"Syntax error in {file_path}")
return []
functions = []
# Get top-level functions
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.FunctionDef):
# Skip private functions (starting with _)
if not node.name.startswith("_"):
functions.append(node.name)
elif isinstance(node, ast.ClassDef):
# Get class methods
for class_node in ast.iter_child_nodes(node):
if isinstance(class_node, ast.FunctionDef):
# Skip private methods (starting with _)
if not class_node.name.startswith("_"):
functions.append(f"{node.name}.{class_node.name}")
return functions
def get_tests_from_file(file_path):
"""Extract all test functions from a Python test file."""
with open(file_path, "r", encoding="utf-8") as f:
try:
content = f.read()
tree = ast.parse(content)
except SyntaxError:
print(f"Syntax error in {file_path}")
return []
tests = []
# Look for test functions (test_*)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"):
tests.append(node.name)
# Also look for function names in the content (for parameterized tests)
function_pattern = r"def\s+(test_\w+)"
tests.extend(re.findall(function_pattern, content))
return list(set(tests))
def map_module_to_test_file(module_path):
"""Map a module path to its corresponding test file path."""
# Convert module path to test path
# e.g., cosmotech/coal/aws/s3.py -> tests/unit/coal/test_aws/test_aws_s3.py
parts = module_path.parts
if len(parts) < 3 or parts[0] != "cosmotech" or parts[1] != "coal":
return None
# Skip __init__.py files
if parts[-1] == "__init__.py":
return None
# Get the module name without extension
module_name = parts[-1].replace(".py", "")
# Construct the test file path
test_dir = Path("tests/unit/coal")
module_parts = parts[2:-1] # Skip cosmotech/coal and the file name
# Create directory structure
for part in module_parts:
test_dir = test_dir / f"test_{part}"
# Create test file name with module path included
# For example, for cosmotech/coal/azure/adx/ingestion.py, the test file would be test_adx_ingestion.py
# For cosmotech/coal/azure/blob.py, the test file would be test_azure_blob.py
if module_parts:
test_file_name = f"test_{module_parts[-1]}_{module_name}.py"
else:
test_file_name = f"test_{module_name}.py"
test_file = test_dir / test_file_name
return test_file
def find_untested_functions():
"""Find functions in cosmotech/coal/ that don't have corresponding tests."""
coal_dir = Path("cosmotech/coal")
# Dictionary to store functions by module
module_functions = {}
# Dictionary to store tests by module
module_tests = defaultdict(list)
# Find all Python files in cosmotech/coal/
for root, _, files in os.walk(coal_dir):
for file in files:
if file.endswith(".py"):
file_path = Path(root) / file
module_path = file_path.relative_to(".")
# Skip __init__.py files
if file == "__init__.py":
continue
# Get functions from the module
functions = get_functions_from_file(file_path)
if functions:
module_functions[module_path] = functions
# Find all test files in tests/unit/coal/
test_dir = Path("tests/unit/coal")
if test_dir.exists():
for root, _, files in os.walk(test_dir):
for file in files:
if file.startswith("test_") and file.endswith(".py"):
test_file_path = Path(root) / file
tests = get_tests_from_file(test_file_path)
module_tests[test_file_path] = tests
# Check which functions don't have tests
untested_functions = {}
for module_path, functions in module_functions.items():
test_file = map_module_to_test_file(module_path)
if test_file is None:
# Skip modules that don't map to a test file
continue
if not test_file.exists():
# If the test file doesn't exist, all functions are untested
untested_functions[module_path] = functions
continue
# Get tests for this module
tests = module_tests.get(test_file, [])
# Check which functions don't have corresponding tests
untested = []
for func in functions:
# Check if there's a test for this function
has_test = False
for test in tests:
# Look for test_function_name or test_class_function_name
# Also check for test patterns like test_class_method_name_additional_info
# For class methods, also check for test_method_name (without the class name)
if (
test == f"test_{func}"
or test == f"test_{func.replace('.', '_')}"
or test.startswith(f"test_{func}_")
or test.startswith(f"test_{func.replace('.', '_')}_")
):
has_test = True
break
# Special case for class methods: check if there's a test for just the method name
if "." in func:
class_name, method_name = func.split(".")
if test == f"test_{method_name}" or test.startswith(f"test_{method_name}_"):
has_test = True
break
if not has_test:
untested.append(func)
if untested:
untested_functions[module_path] = untested
return untested_functions
def generate_test_file(module_path, functions, overwrite=False):
"""Generate a test file for the given module and functions."""
# Get the test file path
test_file = map_module_to_test_file(module_path)
if test_file is None:
print(f"Could not map {module_path} to a test file")
return
# Create the test directory if it doesn't exist
test_file.parent.mkdir(parents=True, exist_ok=True)
# Get the module name and import path
module_name = module_path.stem
import_path = (
f"cosmotech.coal.{'.'.join(module_path.parts[2:-1])}.{module_name}"
if len(module_path.parts) > 3
else f"cosmotech.coal.{module_name}"
)
# Verify that the functions actually exist in the module
verified_functions = []
try:
module = __import__(import_path, fromlist=["*"])
for func in functions:
if "." in func:
class_name, method_name = func.split(".")
if hasattr(module, class_name) and hasattr(getattr(module, class_name), method_name):
verified_functions.append(func)
else:
print(f"Warning: Function {func} not found in {import_path}")
else:
if hasattr(module, func):
verified_functions.append(func)
else:
print(f"Warning: Function {func} not found in {import_path}")
except ImportError as e:
print(f"Warning: Could not import {import_path}: {e}")
verified_functions = functions # Fall back to using all functions
# If the test file already exists, read it and extract existing tests
existing_content = ""
existing_imports = []
existing_test_classes = {}
existing_test_functions = []
if test_file.exists():
with open(test_file, "r", encoding="utf-8") as f:
existing_content = f.read()
# Parse the existing file to extract imports, test classes, and test functions
try:
tree = ast.parse(existing_content)
# Extract imports
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom):
import_lines = existing_content.splitlines()[node.lineno - 1 : node.end_lineno]
existing_imports.extend(import_lines)
# Extract test classes and their methods
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.ClassDef) and node.name.startswith("Test"):
class_lines = existing_content.splitlines()[node.lineno - 1 : node.end_lineno]
class_content = "\n".join(class_lines)
existing_test_classes[node.name] = {"content": class_content, "methods": []}
# Extract test methods
for method_node in ast.iter_child_nodes(node):
if isinstance(method_node, ast.FunctionDef) and method_node.name.startswith("test_"):
existing_test_classes[node.name]["methods"].append(method_node.name)
# Extract standalone test functions
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"):
existing_test_functions.append(node.name)
except SyntaxError:
print(f"Warning: Could not parse existing test file {test_file}")
# If we can't parse the file, we'll just append our new tests to it
# If the file exists and we're not overwriting, check if we need to add any tests
if test_file.exists() and not overwrite:
# Check if all functions already have tests
all_tested = True
for func in verified_functions:
if "." in func:
class_name, method_name = func.split(".")
test_class_name = f"Test{class_name}"
test_method_name = f"test_{method_name}"
# Check if the test class exists and has a test for this method
if (
test_class_name not in existing_test_classes
or test_method_name not in existing_test_classes[test_class_name]["methods"]
):
all_tested = False
break
else:
test_func_name = f"test_{func}"
test_class_name = f"Test{module_name.capitalize()}Functions"
# Check if there's a standalone test function or a method in a test class
if test_func_name not in existing_test_functions and (
test_class_name not in existing_test_classes
or test_func_name not in existing_test_classes[test_class_name]["methods"]
):
all_tested = False
break
if all_tested:
print(f"All functions in {module_path} already have tests, skipping")
return
print(f"Adding tests for untested functions in {test_file}")
# We'll append our new tests to the existing file
with open(test_file, "a", encoding="utf-8") as f:
f.write("\n\n# Added tests for previously untested functions\n")
# Add imports for verified functions if they're not already imported
top_level_functions = [f for f in verified_functions if "." not in f]
if top_level_functions:
import_line = f"from {import_path} import {', '.join(top_level_functions)}"
if import_line not in existing_content:
f.write(f"\n{import_line}\n")
# Add class definitions for class methods
class_methods = defaultdict(list)
for func in verified_functions:
if "." in func:
class_name, method_name = func.split(".")
test_class_name = f"Test{class_name}"
test_method_name = f"test_{method_name}"
# Only add if the test doesn't already exist
if (
test_class_name not in existing_test_classes
or test_method_name not in existing_test_classes[test_class_name]["methods"]
):
class_methods[class_name].append(method_name)
# Generate test classes for untested methods
for class_name, methods in class_methods.items():
test_class_name = f"Test{class_name}"
# If the class already exists, we'll add methods to it
if test_class_name in existing_test_classes:
f.write(f"\n# Additional test methods for {test_class_name}\n")
for method in methods:
test_method_name = f"test_{method}"
if test_method_name not in existing_test_classes[test_class_name]["methods"]:
f.write(
f"""
def {test_method_name}(self):
\"\"\"Test the {method} method.\"\"\"
# Arrange
# instance = {class_name}()
# Act
# result = instance.{method}()
# Assert
# assert result == expected_result
pass # TODO: Implement test
"""
)
else:
# Create a new test class
f.write(
f"""
class {test_class_name}:
\"\"\"Tests for the {class_name} class.\"\"\"
"""
)
for method in methods:
f.write(
f"""
def test_{method}(self):
\"\"\"Test the {method} method.\"\"\"
# Arrange
# instance = {class_name}()
# Act
# result = instance.{method}()
# Assert
# assert result == expected_result
pass # TODO: Implement test
"""
)
# Generate test functions for untested top-level functions
top_level_functions = [f for f in verified_functions if "." not in f]
untested_functions = []
for func in top_level_functions:
test_func_name = f"test_{func}"
test_class_name = f"Test{module_name.capitalize()}Functions"
# Check if there's a standalone test function or a method in a test class
if test_func_name not in existing_test_functions and (
test_class_name not in existing_test_classes
or test_func_name not in existing_test_classes[test_class_name]["methods"]
):
untested_functions.append(func)
if untested_functions:
test_class_name = f"Test{module_name.capitalize()}Functions"
# If the class already exists, we'll add methods to it
if test_class_name in existing_test_classes:
f.write(f"\n# Additional test methods for {test_class_name}\n")
for func in untested_functions:
test_method_name = f"test_{func}"
if test_method_name not in existing_test_classes[test_class_name]["methods"]:
f.write(
f"""
def {test_method_name}(self):
\"\"\"Test the {func} function.\"\"\"
# Arrange
# Act
# result = {func}()
# Assert
# assert result == expected_result
pass # TODO: Implement test
"""
)
else:
# Create a new test class
f.write(
f"""
class {test_class_name}:
\"\"\"Tests for top-level functions in the {module_name} module.\"\"\"
"""
)
for func in untested_functions:
f.write(
f"""
def test_{func}(self):
\"\"\"Test the {func} function.\"\"\"
# Arrange
# Act
# result = {func}()
# Assert
# assert result == expected_result
pass # TODO: Implement test
"""
)
print(f"Added tests for untested functions to {test_file}")
return
# If we're creating a new file or overwriting, generate the complete test file
content = f"""# Copyright (C) - 2023 - 2025 - Cosmo Tech
# This document and all information contained herein is the exclusive property -
# including all intellectual property rights pertaining thereto - of Cosmo Tech.
# Any use, reproduction, translation, broadcasting, transmission, distribution,
# etc., to any person is prohibited unless it has been previously and
# specifically authorized by written means by Cosmo Tech.
import pytest
from unittest.mock import MagicMock, patch
"""
# Add imports for verified functions
top_level_functions = [f for f in verified_functions if "." not in f]
if top_level_functions:
content += f"from {import_path} import {', '.join(top_level_functions)}\n"
# Add class definitions for class methods
class_methods = defaultdict(list)
for func in functions:
if "." in func:
class_name, method_name = func.split(".")
class_methods[class_name].append(method_name)
# Generate test classes
if class_methods:
for class_name, methods in class_methods.items():
content += f"""
class Test{class_name}:
\"\"\"Tests for the {class_name} class.\"\"\"
"""
for method in methods:
content += f"""
def test_{method}(self):
\"\"\"Test the {method} method.\"\"\"
# Arrange
# instance = {class_name}()
# Act
# result = instance.{method}()
# Assert
# assert result == expected_result
pass # TODO: Implement test
"""
# Generate test functions for top-level functions
top_level_functions = [f for f in functions if "." not in f]
if top_level_functions:
content += f"""
class Test{module_name.capitalize()}Functions:
\"\"\"Tests for top-level functions in the {module_name} module.\"\"\"
"""
for func in top_level_functions:
content += f"""
def test_{func}(self):
\"\"\"Test the {func} function.\"\"\"
# Arrange
# Act
# result = {func}()
# Assert
# assert result == expected_result
pass # TODO: Implement test
"""
# Write the test file
with open(test_file, "w", encoding="utf-8") as f:
f.write(content)
print(f"Generated test file: {test_file}")
def main():
"""Main function."""
parser = argparse.ArgumentParser(description="Generate test files for untested functions in cosmotech/coal/")
parser.add_argument("--module", help="Generate tests for a specific module (e.g., cosmotech/coal/aws/s3.py)")
parser.add_argument("--all", action="store_true", help="Generate tests for all untested functions")
parser.add_argument("--overwrite", action="store_true", help="Overwrite existing test files")
args = parser.parse_args()
untested_functions = find_untested_functions()
if args.module:
module_path = Path(args.module)
if module_path in untested_functions:
generate_test_file(module_path, untested_functions[module_path], args.overwrite)
else:
print(f"No untested functions found in {module_path}")
elif args.all:
for module_path, functions in untested_functions.items():
generate_test_file(module_path, functions, args.overwrite)
else:
print("Functions without tests:")
print("=======================")
for module, functions in sorted(untested_functions.items()):
if functions:
print(f"\n{module}:")
for func in sorted(functions):
print(f" - {func}")
# Print summary
total_untested = sum(len(funcs) for funcs in untested_functions.values())
print(f"\nTotal untested functions: {total_untested}")
print("\nTo generate test files, use --module or --all")
if __name__ == "__main__":
main()