forked from vishakha-lall/MapBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_databaseconnect.py
138 lines (111 loc) · 4.58 KB
/
test_databaseconnect.py
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
import databaseconnect
import config
import pytest
import mysql.connector as mysql
class TestClass:
@pytest.fixture(scope="session", autouse=True)
def setup_init(self):
# Will be executed before the first test
main_database = config.database
config.database = "test"
try:
test_db = mysql.connect(
host=config.host,
user=config.user,
passwd=config.password,
database=config.database,
)
cursor = test_db.cursor()
cursor.execute("CREATE DATABASE {}".format(config.database))
print("test database created")
except Exception:
print("Failed to create test database")
# rolling back to main db
config.database = main_database
pytest.exit("Exiting test!")
yield test_db
# Will be executed after the last test is executed
try:
mycursor = test_db.cursor()
mycursor.execute("DROP DATABASE {}".format(config.database))
mycursor.close()
print("test database deleted.")
except Exception:
print("Failed to delete test database.")
config.database = main_database
def test_setup_database(self):
db = databaseconnect.setup_database()
cursor = db.cursor()
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
expected_tables = [
("chat_table"),
("statement_table"),
("question_table"),
("directions_table"),
]
assert tables.sort() == expected_tables.sort()
def test_add_to_database_of_chat_table(self):
db = databaseconnect.add_to_database("C", "subject", "root", "verb", "H")
cursor = db.cursor()
cursor.execute(
"select * from chat_table where root_word='root' and verb='verb' and sentence='H'"
)
res = cursor.fetchone()[0]
assert res == 1
def test_add_to_database_of_question_table(self):
db = databaseconnect.add_to_database("Q", "subject", "root", "verb", "H")
cursor = db.cursor()
cursor.execute(
"select * from question_table where subject='subject' and root_word='root' and verb='verb' and sentence='H'"
)
res = cursor.fetchone()[0]
assert res == 1
def test_add_to_database_of_statement_table(self):
db = databaseconnect.add_to_database("O", "subject", "root", "verb", "H")
cursor = db.cursor()
cursor.execute(
"select * from statement_table where subject='subject' and root_word='root' and verb='verb' and sentence='H'"
)
res = cursor.fetchone()[0]
assert res == 1
def test_get_chat_response(self):
response = databaseconnect.get_chat_response()
assert type(response) is str
def test_get_question_response_without_subject(self):
response = databaseconnect.get_question_response("[]", "root", "verb")
assert type(response) is tuple
def test_get_question_response_with_subject(self):
response = databaseconnect.get_question_response("subject", "root", "verb")
assert type(response) is tuple
def test_add_learnt_statement_to_database(self):
db = databaseconnect.add_learnt_statement_to_database("subject", "root", "verb")
cursor = db.cursor()
cursor.execute(
"select * from question_table where subject='subject' and root_word='root' and verb='verb'"
)
res = cursor.fetchone()[0]
assert res == 1
def test_learn_question_response(self):
response = databaseconnect.learn_question_response("H")
assert type(response) is tuple
def test_clear_table_with_chat_table(self, monkeypatch):
from io import StringIO
yes = StringIO("y\n")
monkeypatch.setattr("sys.stdin", yes)
db = databaseconnect.clear_table("chat_table")
cursor = db.cursor()
cursor.execute("select * from chat_table")
entries = cursor.fetchone()
assert entries is None
def test_clear_table_with_statement_or_question_table(self, monkeypatch):
from io import StringIO
yes = StringIO("y\n")
monkeypatch.setattr("sys.stdin", yes)
db = databaseconnect.clear_table("statement_table")
cursor = db.cursor()
cursor.execute("select * from statement_table")
entries_1 = cursor.fetchone()
cursor.execute("select * from question_table")
entries_2 = cursor.fetchone()
assert entries_1 is None and entries_2 is None