-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_tests.py
92 lines (74 loc) · 2.5 KB
/
app_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
import unittest
from peewee import *
from models import User, Tag, Journal
MODELS = [User, Tag, Journal]
# use an in-memory SQLite for tests.
test_db = SqliteDatabase(':memory:')
USER_DATA = {
'email': 'test_0@example.com',
'password': 'password'
}
class BaseTestCase(unittest.TestCase):
def setUp(self):
# Bind model classes to test db. Since we have a complete list of
# all models, we do not need to recursively bind dependencies.
test_db.bind(MODELS, bind_refs=False, bind_backrefs=False)
test_db.connect()
test_db.create_tables(MODELS)
@staticmethod
def create_users(count=2):
"""Create 2 users"""
for i in range(count):
User.create_user(
email='test_{}@example.com'.format(i),
password='password'
)
def test_create_user(self):
"""Test User"""
self.create_users()
self.assertEqual(User.select().count(), 2)
self.assertNotEqual(
User.select().get().password,
'password'
)
def test_create_duplicate_user(self):
"""Test duplicate user"""
self.create_users()
with self.assertRaises(ValueError):
User.create_user(
email='test_1@example.com',
password='password'
)
def test_journal_creation(self):
"""Create Journal / 1 Entry"""
self.create_users()
user = User.select().get()
Journal.create(
user=user,
title='Space',
date='2018-10-12',
time_spent=12,
learned='Test',
resources='Test'
)
journal = Journal.select().get()
self.assertEqual(Journal.select().count(), 1)
self.assertEqual(journal.user, user)
def test_tag_creation(self):
"""Create Tag"""
self.create_users()
Tag.create(
title='Space',
)
self.assertEqual(Tag.select().count(), 1)
def tearDown(self):
# Not strictly necessary since SQLite in-memory databases only live
# for the duration of the connection, and in the next step we close
# the connection...but a good practice all the same.
test_db.drop_tables(MODELS)
# Close connection to db.
test_db.close()
# If we wanted, we could re-bind the models to their original
# database here. But for tests this is probably not necessary.
if __name__ == '__main__':
unittest.main()