-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_sql.py
More file actions
43 lines (34 loc) · 1.41 KB
/
test_sql.py
File metadata and controls
43 lines (34 loc) · 1.41 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
import unittest
import mysql.connector as sql
class TestSQL(unittest.TestCase):
def setUp(self):
self.dbname = "test_db"
self.table_name = "test_table"
self.column_name = "test_column"
self.where = ""
# Create a test table
self.create_test_table()
def tearDown(self):
# Drop the test table
self.drop_test_table()
def create_test_table(self):
db = sql.connect(host="localhost", user="root", password="root", database=self.dbname)
cursor = db.cursor()
cursor.execute("CREATE TABLE %s (%s VARCHAR(255));" %(self.table_name, self.column_name))
db.close()
def drop_test_table(self):
db = sql.connect(host="localhost", user="root", password="root", database=self.dbname)
cursor = db.cursor()
cursor.execute("DROP TABLE %s;" %self.table_name)
db.close()
def test_select_without_where(self):
expected_result = [('value1',), ('value2',), ('value3',)]
result = sql.select(self.dbname, self.table_name, self.column_name, self.where)
self.assertEqual(result, expected_result)
def test_select_with_where(self):
expected_result = [('value2',)]
where = "test_column = 'value2'"
result = sql.select(self.dbname, self.table_name, self.column_name, where)
self.assertEqual(result, expected_result)
if __name__ == '__main__':
unittest.main()