-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
117 lines (98 loc) · 4.59 KB
/
test.py
File metadata and controls
117 lines (98 loc) · 4.59 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import unittest
import os
import io
import json
import datetime
from unittest.mock import patch, mock_open
# Import the modules being tested
from study_reminders.students_manager import StudentsManager
from study_reminders.students import Students
from study_reminders.reminder_generator import generate_reminder
from study_reminders.reminder_sender import send_reminder
from study_reminders.logger import log_reminder
# -------------------------------
# Tests for reminder-related functions
# -------------------------------
class TestReminderFunctions(unittest.TestCase):
def test_generate_reminder(self):
"""Test that generate_reminder creates a proper message."""
msg = generate_reminder("Alice", "Physics")
self.assertIn("Alice", msg)
self.assertIn("Physics", msg)
self.assertTrue(msg.startswith("Hi"))
def test_send_reminder_valid_email(self):
"""Test that send_reminder prints a message when email is valid."""
with patch("builtins.print") as mock_print:
send_reminder("alice@example.com", "Test Reminder")
mock_print.assert_called_once() # Ensure print was called once
args = mock_print.call_args[0][0] # Extract print text
self.assertIn("Sending reminder", args)
def test_send_reminder_missing_email(self):
"""Test that send_reminder raises an error if email is missing."""
with self.assertRaises(ValueError):
send_reminder("", "No email")
def test_log_reminder_creates_entry(self):
"""Test that log_reminder writes correct log entry content."""
student = {"name": "Alice"}
reminder = "Study Physics"
fake_file = io.StringIO()
with patch("builtins.open", mock_open()) as mock_file:
log_reminder(student, reminder)
mock_file.assert_called_once() # Ensure file was opened
handle = mock_file() # Get file handle
written = handle.write.call_args[0][0] # Get what was written
# Check that log contains student name, reminder text, and a timestamp
self.assertIn("Alice", written)
self.assertIn("Study Physics", written)
self.assertIn(str(datetime.datetime.now().year), written)
# -------------------------------
# Tests for StudentsManager class
# -------------------------------
class TestStudentsManager(unittest.TestCase):
def setUp(self):
"""Prepare a fake students.json for testing."""
fake_json = json.dumps([
{"name": "Alice", "email": "a@a.com", "course": "Math", "preferred_time": "08:00"}
])
# Patch the open_text function to return fake JSON data
self.patcher = patch("study_reminders.students_manager.resources.open_text", mock_open(read_data=fake_json))
self.mock_open_text = self.patcher.start()
self.sm = StudentsManager()
def tearDown(self):
"""Stop the patch after each test."""
self.patcher.stop()
def test_load_students(self):
"""Ensure that students.json is correctly loaded into memory."""
students = self.sm.get_students()
self.assertEqual(len(students), 1)
self.assertEqual(students[0]['name'], "Alice")
def test_add_student(self):
"""Test adding a new student and saving to JSON."""
with patch("builtins.open", mock_open()) as mock_file:
self.sm.add_student("Bob", "b@b.com", "Physics")
# Verify new student was added
self.assertTrue(any(s["name"] == "Bob" for s in self.sm.get_students()))
mock_file.assert_called_once() # Ensure JSON file was saved
def test_remove_student(self):
"""Test removing an existing student."""
with patch("builtins.open", mock_open()):
self.sm.add_student("Charlie", "c@c.com", "Chemistry")
self.sm.remove_student("Charlie")
# Verify the student was removed
self.assertFalse(any(s["name"] == "Charlie" for s in self.sm.get_students()))
# -------------------------------
# Tests for Students class (simpler student container)
# -------------------------------
class TestStudentsClass(unittest.TestCase):
def test_add_and_remove_student(self):
"""Ensure Students class can add and remove students correctly."""
s = Students()
s.add_student("Alice", "alice@example.com", "Math")
self.assertEqual(len(s.get_students()), 1)
s.remove_student("Alice")
self.assertEqual(len(s.get_students()), 0)
# -------------------------------
# Run all tests
# -------------------------------
if __name__ == "__main__":
unittest.main()