-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_database.py
More file actions
68 lines (55 loc) · 2.31 KB
/
Copy pathtest_database.py
File metadata and controls
68 lines (55 loc) · 2.31 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
import os
import unittest
from unittest.mock import MagicMock , Mock , call
from unittest.mock import create_autospec
from db import db, datab, db_decorator, save_data, insert_book_db, load_books, load_prefs, insert_pref_db, mark_finished
from interfaces import Book, Preference
datab = Mock()
class TestDB(unittest.TestCase):
def init_test_db(self):
if hasattr(self,"testDb"):
return self.testDb
testDb = db
testDb.db_name = "test.db"
setattr(self,"testDb",testDb())
return self.testDb
def test_db_creation(self):
# cleanup before running tests
self.clean_up("test.db")
tdb = self.init_test_db()
self.assertEqual(os.path.exists(tdb.db_name),True,"Should create database if the isnt any")
def test_decorator(self):
datab = Mock()
datab.connect = Mock(return_value = Mock(return_value="hello friend"))
@db_decorator(datab)
def test(conn,text):
return conn(text)
self.assertEqual(test("") ,"hello friend" ,"Should execute code on mock" )
def test_insert_book(self):
a = Book(name = 'test', path = 'test', page = 0)
insert_book_db([a])
books = load_books()
self.assertEqual(books[0].name, a.name, "Should be returned from database")
self.assertEqual(books[0].path, a.path, "Should be returned from database")
self.assertEqual(int(books[0].page), a.page, "Should be returned from database")
def test_insert_books(self):
a = Book(name = 'books1', path = 'test', page = 0)
b = Book(name = 'books2', path = 'test', page = 0)
insert_book_db([a,b])
books = load_books()
self.assertEqual( len(books) ,3,"Should be returned from database")
def test_pref_rw(self):
a = Preference(name="tpref", style="None", zoom = -2)
insert_pref_db(a)
prefs = load_prefs()
self.assertEqual(a in prefs ,True, "Should be in returned list")
def test_mark_finished(self):
book = load_books()[0]
mark_finished(book.name)
books = load_books()
self.assertEqual(book in books, False, "Should not be in list")
def clean_up(self, filename):
if os.path.exists(filename):
os.remove(filename)
if __name__ == "__main__":
unittest.main()