8000 Added store and user system tests. · Megh05/testing-python-apps@c78f381 · GitHub
[go: up one dir, main page]

Skip to content

Commit c78f381

Browse files
committed
Added store and user system tests.
1 parent 97e91a3 commit c78f381

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

section4/video_code/tests/system/item_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_create_duplicate_item(self):
6464
with self.app() as c:
6565
with self.app_context():
6666
StoreModel('test').save_to_db()
67-
r = c.post('/item/test', data={'price': 17.99, 'store_id': 1})
67+
c.post('/item/test', data={'price': 17.99, 'store_id': 1})
6868
r = c.post('/item/test', data={'price': 17.99, 'store_id': 1})
6969

7070
self.assertEqual(r.status_code, 400)
Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,79 @@
1-
from models.store import StoreModel
21
from models.item import ItemModel
2+
from models.store import StoreModel
33
from tests.base_test import BaseTest
4+
import json
45

56

67
class StoreTest(BaseTest):
7-
pass
8+
def test_store_not_found(self):
9+
with self.app() as c:
10+
r = c.get('/store/test')
11+
self.assertEqual(r.status_code, 404)
12+
13+
def test_store_found(self):
14+
with self.app() as c:
15+
with self.app_context():
16+
StoreModel('test').save_to_db()
17+
r = c.get('/store/test')
18+
19+
self.assertEqual(r.status_code, 200)
20+
self.assertDictEqual(d1={'name': 'test', 'items': []},
21+
d2=json.loads(r.data))
22+
23+
def test_store_with_items_found(self):
24+
with self.app() as c:
25+
with self.app_context():
26+
StoreModel('test').save_to_db()
27+
ItemModel('test', 2.99, 1).save_to_db()
28+
r = c.get('/store/test')
29+
30+
self.assertEqual(r.status_code, 200)
31+
self.assertDictEqual(d1={'name': 'test', 'items': [{'name': 'test', 'price': 2.99}]},
32+
d2=json.loads(r.data))
33+
34+
def test_delete_store(self):
35+
with self.app() as c:
36+
with self.app_context():
37+
StoreModel('test').save_to_db()
38+
r = c.delete('/store/test')
39+
40+
self.assertEqual(r.status_code, 200)
41+
self.assertDictEqual(d1={'message': 'Store deleted'},
42+
d2=json.loads(r.data))
43+
44+
def test_create_store(self):
45+
with self.app() as c:
46+
with self.app_context():
47+
r = c.post('/store/test')
48+
49+
self.assertEqual(r.status_code, 201)
50+
self.assertIsNotNone(StoreModel.find_by_name('test'))
51+
self.assertDictEqual(d1={'name': 'test', 'items': []},
52+
d2=json.loads(r.data))
53+
54+
def test_create_duplicate_store(self):
55+
with self.app() as c:
56+
with self.app_context():
57+
c.post('/store/test')
58+
r = c.post('/store/test')
59+
60+
self.assertEqual(r.status_code, 400)
61+
62+
def test_store_list(self):
63+
with self.app() as c:
64+
with self.app_context():
65+
StoreModel('test').save_to_db()
66+
r = c.get('/stores')
67+
68+
self.assertDictEqual(d1={'stores': [{'name': 'test', 'items': []}]},
69+
d2=json.loads(r.data))
70+
71+
def test_store_with_items_list(self):
72+
with self.app() as c:
73+
with self.app_context():
74+
StoreModel('test').save_to_db()
75+
ItemModel('test', 17.99, 1).save_to_db()
76+
r = c.get('/stores')
77+
78+
self.assertDictEqual(d1={'stores': [{'name': 'test', 'items': [{'name': 'test', 'price': 17.99}]}]},
79+
d2=json.loads(r.data))
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
from models.user import UserModel
22
from tests.base_test import BaseTest
3+
import json
34

45

56
class UserTest(BaseTest):
6-
pass
7+
def test_register_user(self):
8+
with self.app() as c:
9+
with self.app_context():
10+
r = c.post('/register', data={'username': 'test', 'password': '1234'})
11+
12+
self.assertEqual(r.status_code, 201)
13+
self.assertIsNotNone(UserModel.find_by_username('test'))
14+
self.assertDictEqual(d1={'message': 'User created successfully.'},
15+
d2=json.loads(r.data))
16+
17+
def test_register_duplicate_user(self):
18+
with self.app() as c:
19+
with self.app_context():
20+
c.post('/register', data={'username': 'test', 'password': '1234'})
21+
r = c.post('/register', data={'username': 'test', 'password': '1234'})
22+
23+
self.assertEqual(r.status_code, 400)
24+
self.assertDictEqual(d1={'message': 'A user with that username already exists'},
25+
d2=json.loads(r.data))

0 commit comments

Comments
 (0)
0