-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_app.py
166 lines (134 loc) · 5.33 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
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
164
165
166
import os
from my_app import create_app, db, babel
import unittest
from unittest import mock
import tempfile
import geoip2.records
import coverage
cov = coverage.coverage(
omit = [
'/Users/shalabh.aggarwal/workspace/cookbook10/lib/python3.6/site-packages/*',
'app_tests.py'
]
)
cov.start()
class CatalogTestCase(unittest.TestCase):
def setUp(self):
test_config = {}
self.test_db_file = tempfile.mkstemp()[1]
test_config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + self.test_db_file
test_config['TESTING'] = True
test_config['WTF_CSRF_ENABLED'] = False
self.app = create_app(test_config)
db.init_app(self.app)
babel.init_app(self.app)
self.geoip_city_patcher = mock.patch('geoip2.models.City',
location=geoip2.records.Location(time_zone = 'America/Los_Angeles')
)
PatchedGeoipCity = self.geoip_city_patcher.start()
self.geoip_reader_patcher = mock.patch('geoip2.database.Reader')
PatchedGeoipReader = self.geoip_reader_patcher.start()
PatchedGeoipReader().city.return_value = PatchedGeoipCity
with self.app.app_context():
db.create_all()
from my_app.catalog.views import catalog
self.app.register_blueprint(catalog)
self.client = self.app.test_client()
def tearDown(self):
self.geoip_city_patcher.stop()
self.geoip_reader_patcher.stop()
os.remove(self.test_db_file)
def test_home(self):
rv = self.client.get('/')
self.assertEqual(rv.status_code, 200)
def test_products(self):
"Test Products list page"
rv = self.client.get('/en/products')
self.assertEqual(rv.status_code, 200)
self.assertTrue('No Previous Page' in rv.data.decode("utf-8"))
self.assertTrue('No Next Page' in rv.data.decode("utf-8"))
def test_create_category(self):
"Test creation of new category"
rv = self.client.get('/en/category-create')
self.assertEqual(rv.status_code, 200)
rv = self.client.post('/en/category-create')
self.assertEqual(rv.status_code, 200)
self.assertTrue('This field is required.' in rv.data.decode("utf-8"))
rv = self.client.get('/en/categories')
self.assertEqual(rv.status_code, 200)
self.assertFalse('Phones' in rv.data.decode("utf-8"))
rv = self.client.post('/en/category-create', data={
'name': 'Phones',
})
self.assertEqual(rv.status_code, 302)
rv = self.client.get('/en/categories')
self.assertEqual(rv.status_code, 200)
self.assertTrue('Phones' in rv.data.decode("utf-8"))
rv = self.client.get('/en/category/1')
self.assertEqual(rv.status_code, 200)
self.assertTrue('Phones' in rv.data.decode("utf-8"))
def test_create_product(self):
"Test creation of new product"
rv = self.client.get('/en/product-create')
self.assertEqual(rv.status_code, 200)
# Raise a ValueError for a valid category not found
self.assertRaises(ValueError, self.client.post, '/en/product-create')
# Create a category to be used in product creation
rv = self.client.post('/en/category-create', data={
'name': 'Phones',
})
self.assertEqual(rv.status_code, 302)
rv = self.client.post('/en/product-create', data={
'name': 'iPhone 5',
'price': 549.49,
'company': 'Apple',
'category': 1,
'image': tempfile.NamedTemporaryFile()
})
self.assertEqual(rv.status_code, 302)
rv = self.client.get('/en/product/1')
self.assertEqual(rv.status_code, 200)
self.assertTrue('iPhone 5' in rv.data.decode("utf-8"))
self.assertTrue('America/Los_Angeles' in rv.data.decode("utf-8"))
def test_search_product(self):
"Test searching product"
# Create a category to be used in product creation
rv = self.client.post('/en/category-create', data={
'name': 'Phones',
})
self.assertEqual(rv.status_code, 302)
# Create a product
rv = self.client.post('/en/product-create', data={
'name': 'iPhone 5',
'price': 549.49,
'company': 'Apple',
'category': 1,
'image': tempfile.NamedTemporaryFile()
})
self.assertEqual(rv.status_code, 302)
# Create another product
rv = self.client.post('/en/product-create', data={
'name': 'Galaxy S5',
'price': 549.49,
'company': 'Samsung',
'category': 1,
'image': tempfile.NamedTemporaryFile()
})
self.assertEqual(rv.status_code, 302)
self.client.get('/')
rv = self.client.get('/en/product-search?name=iPhone')
self.assertEqual(rv.status_code, 200)
self.assertTrue('iPhone 5' in rv.data.decode("utf-8"))
self.assertFalse('Galaxy S5' in rv.data.decode("utf-8"))
rv = self.client.get('/en/product-search?name=iPhone 6')
self.assertEqual(rv.status_code, 200)
self.assertFalse('iPhone 6' in rv.data.decode("utf-8"))
if __name__ == '__main__':
try:
unittest.main()
finally:
cov.stop()
cov.save()
cov.report()
cov.html_report(directory = 'coverage')
cov.erase()