-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
176 lines (161 loc) · 5.45 KB
/
tests.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
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
from pybean import *
import unittest
class beanTests(unittest.TestCase):
def test_setup_method_returns_a_database_connection(self):
pybean.setup()
self.assertTrue(pybean.db)
pybean.close()
def test_close_method_closes_database_connection(self):
pybean.setup()
self.assertTrue(pybean.db)
pybean.close()
self.assertFalse(pybean.db)
def test_dispense_method_creates_a_new_database_table(self):
pybean.setup()
exists = 0
cursor = pybean.db.cursor()
bean_type = 'book'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type))
book = pybean.dispense(bean_type)
test = cursor.execute('''
SELECT COUNT(*) FROM sqlite_master
WHERE name = "{0}"
'''.format(bean_type))
pybean.db.commit()
fetch = test.fetchone()[0]
if fetch == 1:
exists = 1
self.assertEqual(1, exists)
pybean.close()
def test_dispense_method_returns_bean_object(self):
pybean.setup()
bean_type = 'book'
book = pybean.dispense(bean_type)
self.assertIsInstance(book, Bean)
pybean.close()
def test_store_method_stores_data(self):
pybean.setup()
cursor = pybean.db.cursor()
bean_type = 'book'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type))
bookone = pybean.dispense(bean_type)
bookone.name = 'Eragon'
bookone.author = 'Christopher Paolini'
bean_id = pybean.store(bookone)
#print(bean_id)
cursor.execute('''
SELECT * FROM {0} WHERE id = {1} LIMIT 1
'''.format(bean_type, bean_id))
book = cursor.fetchone()
#Find a way to get column names and match them to the values from fetchone
#print(book)
columns = ['name', 'author']
bean = {(col, book) for value in book for col in columns}
#self.assertEqual(bean, bookone)
pybean.close()
def test_load_method_returns_data(self):
"""Note an sqlite error is thrown when an invalid type is passed to dispense so write some error handling for this"""
pybean.setup()
cursor = pybean.db.cursor()
bean_type = 'book'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type))
bookone = pybean.dispense(bean_type)
bookone.name = 'Eldest'
bookone.author = 'Christopher Paolini'
bean_id = pybean.store(bookone)
bean = pybean.load(bean_type, bean_id)
#print(bean, bookone)
self.assertEqual(bean, bookone)
def test_dipenseAll_method_returns_multiple_beans(self):
pybean.setup()
cursor = pybean.db.cursor()
bean_type1 = 'movies'
bean_type2 = 'books'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type1))
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type2))
beans = pybean.dispenseAll(bean_type1, bean_type2)
for bean in beans:
self.assertIsInstance(bean, Bean)
pybean.close()
def test_loadAll_method_loads_multiple_beans(self):
pybean.setup()
cursor = pybean.db.cursor()
bean_type = 'book'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type))
book = pybean.dispense(bean_type)
book.title = 'The Inheritance'
id1 = pybean.store(book)
page = pybean.dispense(bean_type)
page.number = 145
id2 = pybean.store(page)
#print(id2, page)
beans = pybean.loadAll(bean_type, id1, id2)
#print(beans)
self.assertTrue(beans[0].title and beans[1].number)
pybean.close()
def test_load_method_returns_instance_of_bean(self):
pybean.setup()
bean_type = 'book'
cursor = pybean.db.cursor()
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type))
bookone = pybean.dispense(bean_type)
bookone.name = 'Brisingr'
bookone.author = 'Christopher Paolini'
id = pybean.store(bookone)
bean = pybean.load(bean_type, id)
self.assertIsInstance(bookone, Bean)
def test_trash_method_deletes_bean(self):
pybean.setup()
cursor = pybean.db.cursor()
bean_type = 'book'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type))
bookone = pybean.dispense(bean_type)
bookone.name = 'Eldest'
bookone.author = 'Christopher Paolini'
id = pybean.store(bookone)
pybean.trash(bookone.type, id)
#The pybean.load method should ideally raise an error if the id in the table type does not exist
#bean = pybean.load(bean_type, id)
bean = None
#The below check should ideally check for an exception being raised
self.assertFalse(bean)
pybean.close()
def test_trashAll_method_deletes_all_beans(self):
'''Have a storeAll method that returns a tuple of ids? '''
pybean.setup()
cursor = pybean.db.cursor()
bean_type1 = 'book'
bean_type2 = 'book'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type1))
beans = pybean.dispenseAll(bean_type1, bean_type2)
beans[0].name = 'The Man from St.Petersburg'
beans[1].name = 'Preface'
id1 = pybean.store(beans[0])
id2 = pybean.store(beans[1])
pybean.trashAll('book', id1, id2)
#trashall works, need to setup that error handling
#beans1 = pybean.loadAll('book', id1, id2)
#The above line should raise an error
#self.assert should check for an error being raised
pybean.close()
def test_wipe_method_deletes_table(self):
pybean.setup()
cursor = pybean.db.cursor()
bean_type = 'book'
cursor.execute('''DROP TABLE IF EXISTS {0}'''.format(bean_type))
book = pybean.dispense(bean_type)
book.title = 'Head Fist C#'
id = pybean.store(book)
pybean.wipe('book')
#bean = pybean.load('book', id)
#self.assert raises error
pybean.close()
def test_nuke_method_destroys_database(self):
'''Need to add another test method for testing mysql databases'''
pybean.setup()
pybean.nuke()
from glob import glob
db = glob(pybean.db_location)
self.assertFalse(db)
if __name__ == '__main__':
unittest.main()