|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +from unittest.mock import patch |
| 4 | +from file_processing.file import File |
| 5 | +from file_processing.errors import FileProcessingFailedError |
| 6 | +from file_processing_test_data import get_test_files_path |
| 7 | + |
| 8 | +# Get the directory where test files are stored |
| 9 | +test_files_path = get_test_files_path() |
| 10 | + |
| 11 | +# Java test file metadata expectations (filename, text length, encoding, num_lines, num_methods, num_classes) |
| 12 | +values = [ |
| 13 | + ('DataTypes.java', 59831, 'ascii', 1409, 70, 17), |
| 14 | + ('ExecutionJobVertex.java', 27219, 'ascii', 695, 24, 2), |
| 15 | + ('TopicAdmin.java', 39062, 'ascii', 770, 25, 3), |
| 16 | + ('Utils.java', 4721, 'ascii', 106, 7, 1), |
| 17 | +] |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "file_name, text_length, encoding, num_lines, num_methods, num_classes", |
| 21 | + values |
| 22 | +) |
| 23 | +def test_java_metadata_extraction(file_name, text_length, encoding, num_lines, num_methods, num_classes): |
| 24 | + """Tests Java file processing metadata extraction.""" |
| 25 | + java_file_path = test_files_path / file_name |
| 26 | + java_file = File(java_file_path) |
| 27 | + |
| 28 | + assert len(java_file.processor.metadata['text']) == text_length |
| 29 | + assert java_file.processor.metadata['encoding'] == encoding |
| 30 | + assert java_file.processor.metadata['num_lines'] == num_lines |
| 31 | + assert java_file.processor.metadata['num_methods'] == num_methods |
| 32 | + assert java_file.processor.metadata['num_classes'] == num_classes |
| 33 | + |
| 34 | +@pytest.mark.parametrize("file_name", [file_name for file_name, *_ in values]) |
| 35 | +def test_java_invalid_save_location(file_name): |
| 36 | + """Tests that saving to an invalid location raises an error.""" |
| 37 | + java_file = File(test_files_path / file_name) |
| 38 | + invalid_save_path = '/non_existent_folder/' + file_name |
| 39 | + with pytest.raises(FileProcessingFailedError): |
| 40 | + java_file.save(invalid_save_path) |
| 41 | + |
| 42 | +@pytest.mark.parametrize("file_name", [file_name for file_name, *_ in values]) |
| 43 | +def test_java_processor_open_file_false(file_name): |
| 44 | + """Tests that the file is not opened when open_file=False.""" |
| 45 | + java_file_path = test_files_path / file_name |
| 46 | + with patch("builtins.open") as mock_open: |
| 47 | + File(java_file_path, open_file=False) |
| 48 | + mock_open.assert_not_called() |
0 commit comments