Skip to content

Commit 98366f7

Browse files
committed
add starter logic to generate_log.py and update test_generate_log.py to match spec
1 parent b6091f2 commit 98366f7

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

.canvas/lesson.md

Whitespace-only changes.

lib/generate_log.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from datetime import datetime
2+
import os
3+
4+
def generate_log(data):
5+
# TODO: Implement log generation logic
6+
7+
# STEP 1: Validate input
8+
# Hint: Check if data is a list
9+
10+
# STEP 2: Generate a filename with today's date (e.g., "log_20250408.txt")
11+
# Hint: Use datetime.now().strftime("%Y%m%d")
12+
13+
# STEP 3: Write the log entries to a file using File I/O
14+
# Use a with open() block and write each line from the data list
15+
# Example: file.write(f"{entry}\n")
16+
17+
# STEP 4: Print a confirmation message with the filename
18+
19+
pass

testing/test_generate_log.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)