|
| 1 | +# testing/test_generate_log.py |
| 2 | + |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +from datetime import datetime |
| 6 | +from lib.generate_log import generate_log |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def log_data(): |
| 10 | + return ["Entry one", "Entry two", "Entry three"] |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def generated_file(log_data): |
| 14 | + filename = generate_log(log_data) |
| 15 | + yield filename |
| 16 | + if os.path.exists(filename): |
| 17 | + os.remove(filename) |
| 18 | + |
| 19 | +def test_log_file_created(generated_file): |
| 20 | + """Test that the log file is created with today's date in the filename.""" |
| 21 | + assert os.path.exists(generated_file), f"{generated_file} not found." |
| 22 | + |
| 23 | +def test_log_file_name_format(generated_file): |
| 24 | + """Test that the filename follows the expected naming convention.""" |
| 25 | + today = datetime.now().strftime("%Y%m%d") |
| 26 | + assert generated_file == f"log_{today}.txt", "Filename does not match expected format." |
| 27 | + |
| 28 | +def test_log_file_content_matches_input(generated_file, log_data): |
| 29 | + """Test that the content written to the log matches the input list.""" |
| 30 | + with open(generated_file, "r") as file: |
| 31 | + lines = [line.strip() for line in file.readlines()] |
| 32 | + assert lines == log_data, "Log file contents do not match input data." |
| 33 | + |
| 34 | +def test_generate_log_raises_error_on_invalid_input(): |
| 35 | + """Test that the function raises a ValueError when input is not a list.""" |
| 36 | + with pytest.raises(ValueError): |
| 37 | + generate_log("This should be a list") |
| 38 | + |
| 39 | +def test_empty_log_list_creates_empty_file(): |
| 40 | + """Test that passing an empty list still creates an empty log file.""" |
| 41 | + filename = generate_log([]) |
| 42 | + with open(filename, "r") as file: |
| 43 | + content = file.read() |
| 44 | + assert content == "" |
| 45 | + os.remove(filename) |
0 commit comments