-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_buggy.py
More file actions
57 lines (51 loc) · 1.49 KB
/
test_buggy.py
File metadata and controls
57 lines (51 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
"""Tiny unit tests for buggy.py – AI & Prompts Track
Tests two simple scenarios:
1. Normal CSV with valid numbers.
2. CSV with blanks and invalid entries (including 'NaN').
"""
import io
import os
from buggy import average_from_csv
# Helper to create a temporary CSV file for testing
def write_temp_csv(filename, lines):
with open(filename, 'w', encoding='utf-8') as f:
f.write("\n".join(lines))
# Test 1: normal CSV
def test_normal_csv():
# FIX: create a simple CSV with valid numbers
lines = [
"score",
"80",
"90",
"70"
]
filename = "test1.csv"
write_temp_csv(filename, lines)
avg = average_from_csv(filename)
assert round(avg, 2) == 80.00, f"Expected 80.00, got {avg:.2f}"
os.remove(filename)
# Test 2: CSV with blanks and bad values
def test_csv_with_bad_values():
# FIX: include header, blank lines, 'NaN', and a bad string
lines = [
"score",
"78",
"",
"oops",
"84",
"NaN",
"73"
]
filename = "test2.csv"
write_temp_csv(filename, lines)
avg = average_from_csv(filename)
# valid numbers: 78, 84, 73 → avg = 235 / 3 = 78.3333
assert round(avg, 2) == 78.33, f"Expected 78.33, got {avg:.2f}"
os.remove(filename)
# Run tests
if __name__ == "__main__":
test_normal_csv()
print("Test 1 passed ✅")
test_csv_with_bad_values()
print("Test 2 passed ✅")