-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
44 lines (37 loc) · 1.87 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
import unittest
from app import app, db, Product
class FlaskAppTests(unittest.TestCase):
def setUp(self):
# Set up the test client and the application context
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
self.client = app.test_client()
with app.app_context():
db.create_all()
def tearDown(self):
# Clean up after each test
with app.app_context():
db.drop_all()
def test_create_product(self):
response = self.client.post('/product', json={'name': 'Test Product', 'price': 19.99})
self.assertEqual(response.status_code, 201)
self.assertIn('Product created successfully', response.get_json()['message'])
def test_get_products(self):
self.client.post('/product', json={'name': 'Test Product', 'price': 19.99})
response = self.client.get('/products')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.get_json()), 1)
def test_update_product(self):
response = self.client.post('/product', json={'name': 'Test Product', 'price': 19.99})
product_id = response.get_json()['product']['id']
response = self.client.put(f'/product/{product_id}', json={'name': 'Updated Product', 'price': 29.99})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['product']['name'], 'Updated Product')
def test_delete_product(self):
response = self.client.post('/product', json={'name': 'Test Product', 'price': 19.99})
product_id = response.get_json()['product']['id']
response = self.client.delete(f'/product/{product_id}')
self.assertEqual(response.status_code, 200)
self.assertIn('Product deleted successfully', response.get_json()['message'])
if __name__ == '__main__':
unittest.main()