-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_project.py
More file actions
66 lines (52 loc) · 1.71 KB
/
test_project.py
File metadata and controls
66 lines (52 loc) · 1.71 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
import os
import json
import project
def test_load_questions():
if os.path.exists(project.QUESTIONS_FILE):
os.rename(project.QUESTIONS_FILE, "questions_backup.json")
with open(project.QUESTIONS_FILE, "w") as f:
json.dump({
"Sample Category": [
{
"question": "Sample Question?",
"options": ["A", "B", "C", "D"],
"answer": "A"
}
]
}, f)
questions = project.load_questions()
assert isinstance(questions, dict)
os.remove(project.QUESTIONS_FILE)
if os.path.exists("questions_backup.json"):
os.rename("questions_backup.json", project.QUESTIONS_FILE)
def test_save_answer():
q = "What is your name?"
a = "My name is test"
if os.path.exists(project.ANSWERS_LOG):
os.remove(project.ANSWERS_LOG)
project.save_answer(q, a)
assert os.path.exists(project.ANSWERS_LOG)
with open(project.ANSWERS_LOG, "r") as f:
content = f.read()
assert "What is your name?" in content
assert "My name is test" in content
def test_add_question_logic():
test_q = "Why this university?"
test_sample = "Because it fits my goals."
qs = {
"Test Category": []
}
qs["Test Category"].append({
"question": test_q,
"options": ["Option1", "Option2", "Option3", "Option4"],
"answer": "Option1"
})
with open(project.QUESTIONS_FILE, "w") as f:
json.dump(qs, f, indent=4)
qs2 = project.load_questions()
found = False
for question in qs2["Test Category"]:
if question["question"] == test_q:
found = True
assert found
print("All tests passed ✅")