-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
67 lines (53 loc) · 2.29 KB
/
test_app.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
import unittest
from app import app, db, User, Post
class FlaskAppTests(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
db.create_all()
self.client = app.test_client()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_home_page(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Welcome to the Home Page', response.data)
def test_register_page(self):
response = self.client.get('/register')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Register', response.data)
def test_login_page(self):
response = self.client.get('/login')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Login', response.data)
def test_dashboard_page(self):
# Create a test user
user = User(name='Test User', email='test@example.com', password='test')
db.session.add(user)
db.session.commit()
# Login as the test user
with self.client.session_transaction() as session:
session['user_id'] = user.id
# Test dashboard page with no filter
response = self.client.get('/dashboard')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Test User', response.data)
# Test dashboard page with filter
response = self.client.post('/dashboard', data=dict(filter_text='test'))
self.assertEqual(response.status_code, 200)
self.assertIn(b'Test User', response.data)
def test_api_all_posts(self):
# Create test posts
post1 = Post(title='Post 1', content='Test content 1', upvotes=10, comment_count=5)
post2 = Post(title='Post 2', content='Test content 2', upvotes=20, comment_count=10)
db.session.add_all([post1, post2])
db.session.commit()
# Test API endpoint
response = self.client.get('/api/allposts/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Post 1', response.data)
self.assertIn(b'Post 2', response.data)
if __name__ == '__main__':
unittest.main()
# I can't pass all the tests, unfortunately, can we solve it together?