This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
64 lines (52 loc) · 1.92 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
#!/usr/bin/env python
from datetime import datetime, timedelta
import unittest
from app import create_app, db
from app.models import User, Course
from config import Config
class TestConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = 'sqlite://'
ELASTICSEARCH_URL = None
class UserModelCase(unittest.TestCase):
def setUp(self):
self.app = create_app(TestConfig)
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_password_hashing(self):
u = User(username='susan')
u.set_password('cat')
self.assertFalse(u.check_password('dog'))
self.assertTrue(u.check_password('cat'))
def test_avatar(self):
u = User(username='john', email='john@example.com')
self.assertEqual(u.avatar(128), ('https://www.gravatar.com/avatar/'
'd4c74594d841139328695756648b6bd6'
'?d=identicon&s=128'))
def test_enrollment(self):
u = User(username='john', email='john@example.com')
c = Course(title='matematika')
db.session.add(u)
db.session.add(c)
db.session.commit()
self.assertEqual(u.courses.all(), [])
self.assertEqual(c.users.all(), [])
u.enroll(c)
db.session.commit()
self.assertTrue(u.is_enrolled(c))
self.assertEqual(u.courses.count(), 1)
self.assertEqual(u.courses.first().title, 'matematika')
self.assertEqual(c.users.count(), 1)
self.assertEqual(c.users.first().username, 'john')
u.withdraw(c)
db.session.commit()
self.assertFalse(u.is_enrolled(c))
self.assertEqual(u.courses.count(), 0)
self.assertEqual(c.users.count(), 0)
if __name__ == '__main__':
unittest.main(verbosity=2)