-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
163 lines (103 loc) · 4.58 KB
/
test.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
import crud
import os
from unittest import TestCase
from server import app
from flask import session
from model import User, Collection, Picture, connect_to_db, db
def example_data():
"""Create sample data"""
User.query.delete()
Collection.query.delete()
Picture.query.delete()
user1 = crud.create_user('[email protected]', 'test')
user2 = crud.create_user('[email protected]', 'test')
collection1 = crud.create_collection(1, "notes", "01/01/2021")
collection2 = crud.create_collection(2, "notes", "01/01/2021")
picture1 = crud.create_picture(1, "google.com")
picture2 = crud.create_picture(2, "google.com")
db.session.add_all([user1, user2])
db.session.commit()
class FlaskTests(TestCase):
"""Testing flask server"""
def setUp(self):
"""Stuff to do before every test."""
# Get the Flask test client
client = app.test_client()
# Show Flask errors that happen during tests
app.config['TESTING'] = True
# Connect to test database
connect_to_db(app, "postgresql:///testdb")
# Create tables
db.create_all()
def tearDown(self):
"""Do at end of every test."""
db.session.remove()
db.drop_all()
db.engine.dispose()
def test_homepage(self):
"""Check homepage loads."""
client = app.test_client()
result = client.get('/')
self.assertEqual(result.status_code, 200)
self.assertIn(b'<p>Already have an account? <a href="/">Sign in</a>.</p>', result.data)
def test_return_images(self):
"""Check images load with user input."""
client = app.test_client()
result = client.post('/save-city', data={'city': 'california'})
self.assertEqual(result.status_code, 200)
self.assertIn(b"https://www.flickr.com/", result.data)
def test_create_user(self):
"""Check user creation."""
result = self.client.post('/newusers',
data={'register-email': '[email protected]',
'register-password': 'test1'},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn(b'account has been created', result.data)
class FlaskTestsLoggedIn(TestCase):
"""Flask tests that use the database while user is logged in"""
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'placeholderkey'
self.client = app.test_client()
connect_to_db(app, 'postgresql:///testdb', echo=False)
db.create_all()
example_data()
def tearDown(self):
"""Stuff to do after each test."""
db.session.remove()
db.drop_all()
db.engine.dispose()
def test_login(self):
"""Test login to account"""
result = self.client.post('/login',
data={'email': '[email protected]',
'password': 'test'},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn(b'<div id="image-results">', result.data)
def test_query_collection(self):
"""Check collection query."""
self.assertEqual(str(crud.get_collections()), "[<Collection collection_id=1 user=1>, <Collection collection_id=2 user=2>]")
def test_query_collection_by_id(self):
"""Check collection_id query."""
self.assertEqual(str(crud.get_collection_by_id(1)), "<Collection collection_id=1 user=1>")
def test_get_collection_by_email(self):
"""Check query by email returns all collections"""
self.assertEqual(str(crud.get_collection_by_email("[email protected]")), "[<Collection collection_id=1 user=1>]")
def test_get_pictures_by_collection(self):
"""Check query by collections returns all pictures"""
self.assertEqual(str(crud.get_pictures_by_collection(1)), "[<Picture picture_id=1 URL=google.com>]")
def test_create_collection(self):
"""Check collection creation"""
self.assertEqual(str(crud.create_collection(1, "notes", "01/01/2021")), "<Collection collection_id=3 user=1>")
def test_create_picture(self):
"""Check picture creation"""
crud.create_collection(1, "notes", "01/01/2021")
self.assertEqual(str(crud.create_picture(1, "google.com")), "<Picture picture_id=3 URL=google.com>")
if __name__ == "__main__":
import unittest
os.system('dropdb testdb')
os.system('createdb testdb')
unittest.main(verbosity=2)