Test the local AST analyzer with sample data
()
| 11 | import os |
| 12 | |
| 13 | def test_ast_analyzer(): |
| 14 | """Test the local AST analyzer with sample data""" |
| 15 | |
| 16 | test_blocks = [ |
| 17 | { |
| 18 | "type": "code", |
| 19 | "id": "1", |
| 20 | "content": "a = 42" |
| 21 | }, |
| 22 | { |
| 23 | "type": "code", |
| 24 | "id": "2", |
| 25 | "content": "b = 1 + a" |
| 26 | }, |
| 27 | { |
| 28 | "type": "sql", |
| 29 | "id": "3", |
| 30 | "content": "SELECT * FROM users WHERE id == {{someVariable}}" |
| 31 | }, |
| 32 | { |
| 33 | "type": "code", |
| 34 | "id": "4", |
| 35 | "content": "try:\n raise ValueError('error')\nexcept Exception:\n pass" |
| 36 | } |
| 37 | ] |
| 38 | |
| 39 | expected_result = [ |
| 40 | { |
| 41 | "id": "1", |
| 42 | "definedVariables": ["a"], |
| 43 | "usedVariables": [], |
| 44 | "importedModules": [] |
| 45 | }, |
| 46 | { |
| 47 | "id": "2", |
| 48 | "definedVariables": ["b"], |
| 49 | "usedVariables": ["a"], |
| 50 | "importedModules": [] |
| 51 | }, |
| 52 | { |
| 53 | "id": "3", |
| 54 | "definedVariables": [], |
| 55 | "usedVariables": ["someVariable", "users"], |
| 56 | "importedModules": [] |
| 57 | }, |
| 58 | { |
| 59 | "id": "4", |
| 60 | "definedVariables": [], |
| 61 | "usedVariables": [], |
| 62 | "importedModules": [] |
| 63 | } |
| 64 | ] |
| 65 | |
| 66 | with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as input_file: |
| 67 | json.dump({"blocks": test_blocks}, input_file) |
| 68 | input_path = input_file.name |
| 69 | |
| 70 | with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as output_file: |