-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
112 lines (76 loc) · 3.63 KB
/
test_main.py
File metadata and controls
112 lines (76 loc) · 3.63 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
import pytest
import sqlite3
from main import Database
@pytest.fixture
def database():
db = Database()
# In-memory database for testing
db.conn = sqlite3.connect(":memory:")
db.conn.row_factory = sqlite3.Row
db.c = db.conn.cursor()
# Create the table
db.table()
# Return the database instance so it can be tested
yield db
# Close the databse
db.close()
# Test if adding a question to the database works
def test_add(database):
# Add a question to the database
database.add("test", "What is the capital of Hungary?", "Budapest", "London", "Berlin", "Paris")
# Check if the question actually exists/ get it back from the database
database.c.execute('SELECT * FROM quiz WHERE question = ?', ("What is the capital of Hungary?",))
result = database.c.fetchone()
# Check for values
assert result["question"] == "What is the capital of Hungary?"
assert result["answer"] == "Budapest"
assert result["decoy_1"] == "London"
assert result["decoy_2"] == "Berlin"
assert result["decoy_3"] == "Paris"
# Test if deleting a question works
def test_delete(database):
# Add a question
database.add("test", "What is the capital of Hungary?", "Budapest", "London", "Berlin", "Paris")
# Now delete the question
database.delete("What is the capital of Hungary?")
# Check if the question is still in the database
database.c.execute('SELECT * FROM quiz WHERE question = ?', ("What is the capital of Hungary?",))
result = database.c.fetchone()
assert result is None # It should be None if it doen't exists
# Test if I can get all the questions of a certain type
def test_type_questions(database):
# Add sample questions
database.add("test", "What is the capital of Hungary?", "Budapest", "London", "Berlin", "Paris")
database.add("animal", "What is unique about chinchilla fur?", "density", "waterproof", "oily", "sticky")
# Get questions of type 'test'
questions = database.type_questions("test")
# Test it
assert len(questions) == 1 # There should be just one question for this type
assert questions[0]["question"] == "What is the capital of Hungary?"
# Get questions of type 'animal'
questions = database.type_questions("animal")
# Test it
assert len(questions) == 1
assert questions[0]["question"] == "What is unique about chinchilla fur?"
# Test if we can get all available types from the database
def test_types(database):
# Add sample questions
database.add("test", "What is the capital of Hungary?", "Budapest", "London", "Berlin", "Paris")
database.add("animal", "What is unique about chinchilla fur?", "density", "waterproof", "oily", "sticky")
# Get types
types = database.types()
# Check if these types exist
assert "test" in types
assert "animal" in types
# Test the question count for a certain type of questions
def test_type_questions_count(database):
# Add sample questions
database.add("test", "What is the capital of Hungary?", "Budapest", "London", "Berlin", "Paris")
database.add("animal", "What is unique about chinchilla fur?", "density", "waterproof", "oily", "sticky")
database.add("test", "What is the capital of Romania?", "Bucurest", "London", "Berlin", "Paris")
# Get questions of type 'test'
count = database.type_questions_count("test")
assert count == 2
# Get questions of type 'animal'
count = database.type_questions_count("animal")
assert count == 1