-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_database.py
More file actions
535 lines (444 loc) · 19.9 KB
/
Copy pathtest_database.py
File metadata and controls
535 lines (444 loc) · 19.9 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
"""Tests for the student progress tracker database layer."""
import os
import tempfile
import pytest
# Override database path before importing
_tmp = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
_tmp.close()
os.environ["STUDENT_PROGRESS_DB"] = _tmp.name
import src.student_progress.database as db
@pytest.fixture(autouse=True)
def _clean_db():
"""Re-initialize the database before each test."""
conn = db.get_connection()
for table in [
"question_history",
"topic_mastery",
"assessment_results",
"assessment_topics",
"assessments",
"topics",
"enrollments",
"courses",
"students",
]:
conn.execute(f"DELETE FROM {table}")
conn.commit()
conn.close()
yield
@pytest.fixture()
def sample_student():
return db.create_student("stu-001", "Alice Johnson", "[email protected]", "9th Grade")
@pytest.fixture()
def sample_course():
return db.create_course("course-001", "Algebra I", "Mathematics", "9th Grade")
@pytest.fixture()
def sample_topic(sample_course):
return db.create_topic("topic-001", sample_course["id"], "Linear Equations")
@pytest.fixture()
def sample_assessment(sample_course):
return db.create_assessment(
"assess-001", sample_course["id"], "Chapter 1 Quiz", "quiz", 100, 10
)
@pytest.fixture()
def enrolled_student(sample_student, sample_course):
"""Student enrolled in the sample course."""
db.enroll_student("stu-001", "course-001")
return sample_student
class TestStudentOperations:
def test_create_student(self):
student = db.create_student("stu-test", "Bob Smith", "[email protected]", "10th Grade")
assert student["id"] == "stu-test"
assert student["name"] == "Bob Smith"
assert student["email"] == "[email protected]"
assert student["grade_level"] == "10th Grade"
def test_get_student(self, sample_student):
student = db.get_student("stu-001")
assert student is not None
assert student["name"] == "Alice Johnson"
def test_get_student_not_found(self):
assert db.get_student("nonexistent") is None
def test_list_students(self, sample_student):
students = db.list_students()
assert len(students) == 1
assert students[0]["name"] == "Alice Johnson"
def test_list_students_by_course(self, enrolled_student, sample_course):
students = db.list_students("course-001")
assert len(students) == 1
def test_list_students_empty_course(self, sample_course):
students = db.list_students("course-001")
assert len(students) == 0
class TestStudentCRUD:
def test_update_student_name(self, sample_student):
updated = db.update_student("stu-001", name="Alice Smith")
assert updated is not None
assert updated["name"] == "Alice Smith"
assert updated["email"] == "[email protected]"
def test_update_student_email(self, sample_student):
updated = db.update_student("stu-001", email="[email protected]")
assert updated["email"] == "[email protected]"
assert updated["name"] == "Alice Johnson"
def test_update_student_not_found(self):
assert db.update_student("nonexistent", name="X") is None
def test_delete_student(self, sample_student):
assert db.delete_student("stu-001") is True
assert db.get_student("stu-001") is None
def test_delete_student_not_found(self):
assert db.delete_student("nonexistent") is False
def test_delete_student_cascades_enrollment(self, enrolled_student, sample_course):
db.delete_student("stu-001")
students = db.list_students("course-001")
assert len(students) == 0
class TestCourseOperations:
def test_create_course(self):
course = db.create_course("c-test", "Biology 101", "Science", "10th Grade")
assert course["id"] == "c-test"
assert course["subject"] == "Science"
def test_get_course(self, sample_course):
course = db.get_course("course-001")
assert course is not None
assert course["name"] == "Algebra I"
def test_get_course_not_found(self):
assert db.get_course("nonexistent") is None
def test_list_courses(self, sample_course):
courses = db.list_courses()
assert len(courses) == 1
assert courses[0]["student_count"] == 0
def test_enroll_student(self, sample_student, sample_course):
assert db.enroll_student("stu-001", "course-001") is True
def test_enroll_student_duplicate(self, sample_student, sample_course):
db.enroll_student("stu-001", "course-001")
assert db.enroll_student("stu-001", "course-001") is False
class TestCourseCRUD:
def test_update_course_name(self, sample_course):
updated = db.update_course("course-001", name="Algebra II")
assert updated is not None
assert updated["name"] == "Algebra II"
assert updated["subject"] == "Mathematics"
def test_update_course_subject(self, sample_course):
updated = db.update_course("course-001", subject="Science")
assert updated["subject"] == "Science"
def test_update_course_not_found(self):
assert db.update_course("nonexistent", name="X") is None
def test_delete_course(self, sample_course):
assert db.delete_course("course-001") is True
assert db.get_course("course-001") is None
def test_delete_course_not_found(self):
assert db.delete_course("nonexistent") is False
def test_unenroll_student(self, enrolled_student, sample_course):
assert db.unenroll_student("stu-001", "course-001") is True
students = db.list_students("course-001")
assert len(students) == 0
def test_unenroll_student_not_enrolled(self, sample_student, sample_course):
assert db.unenroll_student("stu-001", "course-001") is False
class TestTopicOperations:
def test_create_topic(self, sample_course):
topic = db.create_topic("t-test", "course-001", "Quadratic Equations", weight=1.5)
assert topic["name"] == "Quadratic Equations"
assert topic["weight"] == 1.5
def test_list_topics(self, sample_topic):
topics = db.list_topics("course-001")
assert len(topics) == 1
assert topics[0]["name"] == "Linear Equations"
class TestAssessmentOperations:
def test_create_assessment(self, sample_course):
assessment = db.create_assessment(
"a-test", "course-001", "Midterm", "test", 200, 50, 3600
)
assert assessment["name"] == "Midterm"
assert assessment["total_points"] == 200
def test_get_assessment(self, sample_assessment):
assessment = db.get_assessment("assess-001")
assert assessment is not None
assert assessment["assessment_type"] == "quiz"
def test_get_assessment_not_found(self):
assert db.get_assessment("nonexistent") is None
def test_list_assessments(self, sample_assessment):
assessments = db.list_assessments("course-001")
assert len(assessments) == 1
assert assessments[0]["name"] == "Chapter 1 Quiz"
assert assessments[0]["results_count"] == 0
def test_list_assessments_with_results(self, enrolled_student, sample_assessment):
db.record_assessment_result(
"result-la", "stu-001", "assess-001",
points_earned=80, points_possible=100,
)
assessments = db.list_assessments("course-001")
assert assessments[0]["results_count"] == 1
def test_list_assessments_empty(self, sample_course):
assessments = db.list_assessments("course-001")
assert len(assessments) == 0
class TestResultRecording:
def test_record_result(self, enrolled_student, sample_assessment):
result = db.record_assessment_result(
"result-001", "stu-001", "assess-001",
points_earned=85, points_possible=100,
)
assert result["percentage"] == 85.0
def test_record_result_with_time(self, enrolled_student, sample_assessment):
result = db.record_assessment_result(
"result-time", "stu-001", "assess-001",
points_earned=80, points_possible=100,
time_spent_seconds=1800,
)
assert result["percentage"] == 80.0
def test_record_result_with_questions(
self, enrolled_student, sample_assessment, sample_topic
):
result = db.record_assessment_result(
"result-002", "stu-001", "assess-001",
points_earned=90, points_possible=100,
question_results=[
{
"question_id": "q1",
"topic_id": "topic-001",
"is_correct": True,
"difficulty": 0.3,
},
{
"question_id": "q2",
"topic_id": "topic-001",
"is_correct": True,
"difficulty": 0.5,
},
{
"question_id": "q3",
"topic_id": "topic-001",
"is_correct": False,
"difficulty": 0.7,
},
],
)
assert result["percentage"] == 90.0
mastery = db.get_topic_mastery("stu-001")
assert len(mastery) == 1
assert mastery[0]["topic_name"] == "Linear Equations"
assert mastery[0]["questions_attempted"] == 3
assert mastery[0]["questions_correct"] == 2
def test_retake_updates_existing(self, enrolled_student, sample_assessment):
db.record_assessment_result(
"result-001", "stu-001", "assess-001",
points_earned=70, points_possible=100,
)
# Retake with a new result_id - should update existing row
result = db.record_assessment_result(
"result-002", "stu-001", "assess-001",
points_earned=90, points_possible=100,
)
assert result["percentage"] == 90.0
# The ID should be the original result's ID
assert result["id"] == "result-001"
class TestEnrollmentCheck:
def test_check_enrollment_enrolled(self, enrolled_student, sample_assessment):
assert db.check_enrollment("stu-001", "assess-001") is True
def test_check_enrollment_not_enrolled(self, sample_student, sample_assessment):
assert db.check_enrollment("stu-001", "assess-001") is False
class TestAnalytics:
def test_get_student_profile(self, enrolled_student, sample_course):
profile = db.get_student_profile("stu-001")
assert profile["name"] == "Alice Johnson"
assert len(profile["courses"]) == 1
def test_get_student_profile_not_found(self):
assert db.get_student_profile("nonexistent") is None
def test_get_student_profile_with_assessment(
self, enrolled_student, sample_course, sample_assessment
):
db.record_assessment_result(
"result-prof", "stu-001", "assess-001",
points_earned=85, points_possible=100,
)
profile = db.get_student_profile("stu-001")
assert profile["statistics"]["total_assessments"] == 1
assert profile["statistics"]["avg_percentage"] == 85.0
assert len(profile["recent_assessments"]) == 1
def test_get_topic_mastery_with_course_filter(
self, enrolled_student, sample_assessment, sample_topic
):
db.record_assessment_result(
"result-mf", "stu-001", "assess-001",
points_earned=80, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
],
)
# Filter by the correct course
mastery = db.get_topic_mastery("stu-001", "course-001")
assert len(mastery) == 1
# Filter by a different course returns empty
mastery_other = db.get_topic_mastery("stu-001", "course-other")
assert len(mastery_other) == 0
def test_get_student_history_with_topic_filter(
self, enrolled_student, sample_assessment, sample_topic
):
db.record_assessment_result(
"result-hf", "stu-001", "assess-001",
points_earned=80, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
],
)
# Filter by existing topic
history = db.get_student_history("stu-001", topic_id="topic-001")
assert len(history) == 1
# Filter by non-existing topic returns empty
history_other = db.get_student_history("stu-001", topic_id="topic-other")
assert len(history_other) == 0
def test_get_learning_gaps(self, enrolled_student, sample_assessment, sample_topic):
db.record_assessment_result(
"result-gap", "stu-001", "assess-001",
points_earned=50, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q2", "topic_id": "topic-001", "is_correct": False},
{"question_id": "q3", "topic_id": "topic-001", "is_correct": False},
],
)
gaps = db.get_learning_gaps("stu-001", 0.7)
assert len(gaps) == 1
assert gaps[0]["topic_name"] == "Linear Equations"
def test_get_student_history(self, enrolled_student, sample_assessment, sample_topic):
db.record_assessment_result(
"result-hist", "stu-001", "assess-001",
points_earned=80, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
],
)
history = db.get_student_history("stu-001")
assert len(history) == 1
assert history[0]["is_correct"] == 1
def test_get_class_analytics(self, enrolled_student, sample_course, sample_assessment):
db.record_assessment_result(
"result-class", "stu-001", "assess-001",
points_earned=88, points_possible=100,
)
analytics = db.get_class_analytics("course-001")
assert analytics["overall"]["students_assessed"] == 1
assert analytics["overall"]["avg_percentage"] == 88.0
class TestTrendDetection:
def test_improving_trend(self, enrolled_student, sample_assessment, sample_topic):
"""Low initial mastery followed by high accuracy triggers improving trend."""
# First attempt: low score (1/3 correct = 0.33 mastery)
db.record_assessment_result(
"result-t1", "stu-001", "assess-001",
points_earned=33, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q2", "topic_id": "topic-001", "is_correct": False},
{"question_id": "q3", "topic_id": "topic-001", "is_correct": False},
],
)
mastery = db.get_topic_mastery("stu-001")
assert mastery[0]["trend"] == "stable" # First attempt is always stable
# Second assessment with high accuracy (recent_accuracy=1.0 vs mastery=0.33)
assess2 = db.create_assessment(
"assess-002", "course-001", "Quiz 2", "quiz", 100, 10
)
db.record_assessment_result(
"result-t2", "stu-001", assess2["id"],
points_earned=100, points_possible=100,
question_results=[
{"question_id": "q4", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q5", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q6", "topic_id": "topic-001", "is_correct": True},
],
)
mastery = db.get_topic_mastery("stu-001")
assert mastery[0]["trend"] == "improving"
def test_declining_trend(self, enrolled_student, sample_assessment, sample_topic):
"""High initial mastery followed by low accuracy triggers declining trend."""
# First attempt: high score (3/3 correct = 1.0 mastery)
db.record_assessment_result(
"result-d1", "stu-001", "assess-001",
points_earned=100, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q2", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q3", "topic_id": "topic-001", "is_correct": True},
],
)
# Second assessment with low accuracy (recent_accuracy=0.0 vs mastery=1.0)
assess2 = db.create_assessment(
"assess-002", "course-001", "Quiz 2", "quiz", 100, 10
)
db.record_assessment_result(
"result-d2", "stu-001", assess2["id"],
points_earned=0, points_possible=100,
question_results=[
{"question_id": "q4", "topic_id": "topic-001", "is_correct": False},
{"question_id": "q5", "topic_id": "topic-001", "is_correct": False},
{"question_id": "q6", "topic_id": "topic-001", "is_correct": False},
],
)
mastery = db.get_topic_mastery("stu-001")
assert mastery[0]["trend"] == "declining"
class TestAdditionalEdgeCases:
def test_create_student_minimal_args(self):
student = db.create_student("stu-min", "Minimal Student")
assert student["id"] == "stu-min"
assert student["name"] == "Minimal Student"
assert student["email"] is None
assert student["grade_level"] is None
def test_list_courses_with_student_count(self, enrolled_student, sample_course):
courses = db.list_courses()
assert courses[0]["student_count"] == 1
def test_delete_course_cascades_topics(self, sample_course, sample_topic):
db.delete_course("course-001")
topics = db.list_topics("course-001")
assert len(topics) == 0
def test_delete_course_cascades_assessments(self, sample_course, sample_assessment):
db.delete_course("course-001")
assert db.get_assessment("assess-001") is None
def test_get_class_analytics_with_topics(
self, enrolled_student, sample_course, sample_assessment, sample_topic
):
db.record_assessment_result(
"result-ca", "stu-001", "assess-001",
points_earned=75, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q2", "topic_id": "topic-001", "is_correct": False},
],
)
analytics = db.get_class_analytics("course-001")
assert len(analytics["by_topic"]) == 1
assert analytics["by_topic"][0]["name"] == "Linear Equations"
assert analytics["by_topic"][0]["students_attempted"] == 1
def test_get_learning_gaps_no_gaps(
self, enrolled_student, sample_assessment, sample_topic
):
"""Student above threshold has no gaps."""
db.record_assessment_result(
"result-ng", "stu-001", "assess-001",
points_earned=100, points_possible=100,
question_results=[
{"question_id": "q1", "topic_id": "topic-001", "is_correct": True},
{"question_id": "q2", "topic_id": "topic-001", "is_correct": True},
],
)
gaps = db.get_learning_gaps("stu-001", 0.7)
assert len(gaps) == 0
class TestDatabaseConfig:
def test_wal_mode(self):
conn = db.get_connection()
try:
cursor = conn.cursor()
cursor.execute("PRAGMA journal_mode")
mode = cursor.fetchone()[0]
assert mode == "wal"
finally:
conn.close()
def test_foreign_keys_enabled(self):
conn = db.get_connection()
try:
cursor = conn.cursor()
cursor.execute("PRAGMA foreign_keys")
assert cursor.fetchone()[0] == 1
finally:
conn.close()
def teardown_module():
"""Clean up temp database."""
try:
os.unlink(_tmp.name)
except OSError:
pass