|
| 1 | +import shutil |
| 2 | + |
| 3 | +import pytest, os, csv |
| 4 | + |
| 5 | +from backend.config.paths import TMP_FILE_DIR |
| 6 | +from backend.file_utils.csv_methods_writer import write_launch_info_to_csv |
| 7 | +from backend.file_utils.files import reset_dirs, save_upload_file |
| 8 | +from unittest.mock import Mock |
| 9 | +from fastapi import UploadFile |
| 10 | +from io import BytesIO |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "filepaths", |
| 15 | + [ |
| 16 | + ([TMP_FILE_DIR + "/test0"]), |
| 17 | + ([TMP_FILE_DIR + "/test1", TMP_FILE_DIR + "/test2"]), |
| 18 | + ], |
| 19 | +) |
| 20 | +def test_removing_tmp_files(filepaths): |
| 21 | + for path in filepaths: |
| 22 | + os.makedirs(path) |
| 23 | + reset_dirs(filepaths) |
| 24 | + for path in filepaths: |
| 25 | + assert not os.path.exists(path) |
| 26 | + |
| 27 | + |
| 28 | +def test_save_upload_file(): |
| 29 | + test_content = b"Hello, World!" * 1000 |
| 30 | + upload_dir = TMP_FILE_DIR + "/test_save_upload/" |
| 31 | + dest_path = upload_dir + "test.txt" |
| 32 | + |
| 33 | + mock_file = BytesIO(test_content) |
| 34 | + upload_file = Mock(spec=UploadFile) |
| 35 | + upload_file.file = mock_file |
| 36 | + |
| 37 | + save_upload_file(upload_file, dest_path) |
| 38 | + |
| 39 | + assert os.path.exists(dest_path) |
| 40 | + with open(dest_path, "rb") as f: |
| 41 | + saved_content = f.read() |
| 42 | + shutil.rmtree(upload_dir) |
| 43 | + assert saved_content == test_content |
| 44 | + |
| 45 | + |
| 46 | +def test_write_launch_info_to_csv(): |
| 47 | + upload_dir = TMP_FILE_DIR + "/test_write_launch_info_to_csv/" |
| 48 | + os.makedirs(upload_dir) |
| 49 | + output_file = upload_dir + "launch_info.csv" |
| 50 | + parsed_methods = [ |
| 51 | + "ManuallyCollected.dll,BinSearchMain", |
| 52 | + "ManuallyCollected.dll,BellmanFord", |
| 53 | + "ManuallyCollected.dll,BinaryMaze1BFS", |
| 54 | + ] |
| 55 | + |
| 56 | + write_launch_info_to_csv(parsed_methods=parsed_methods, output_file=output_file) |
| 57 | + |
| 58 | + assert os.path.exists(output_file) |
| 59 | + |
| 60 | + with open(output_file, "r") as f: |
| 61 | + reader = csv.reader(f) |
| 62 | + rows = list(reader) |
| 63 | + |
| 64 | + assert rows[0] == ["dll", "method"] |
| 65 | + |
| 66 | + expected_data = [ |
| 67 | + ["ManuallyCollected.dll", "BinSearchMain"], |
| 68 | + ["ManuallyCollected.dll", "BellmanFord"], |
| 69 | + ["ManuallyCollected.dll", "BinaryMaze1BFS"], |
| 70 | + ] |
| 71 | + shutil.rmtree(upload_dir) |
| 72 | + assert rows[1:] == expected_data |
0 commit comments