-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
56 lines (41 loc) · 1.73 KB
/
tests.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
import server
import unittest
import json
from pymongo import MongoClient
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
self.app = server.app.test_client()
# Run app in testing mode to retrieve exceptions and stack traces
server.app.config['TESTING'] = True
# Inject test database into application
mongo = MongoClient('localhost', 27017)
db = mongo.test_database
server.app.db = db
# Drop collection (significantly faster than dropping entire db)
db.drop_collection('myobjects')
# MyObject tests
def test_posting_myobject(self):
response = self.app.post(
'/myobject/',
data=json.dumps(dict(name="A object")),
content_type='application/json')
responseJSON = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
assert 'application/json' in response.content_type
assert 'A object' in responseJSON["name"]
def test_getting_object(self):
response = self.app.post(
'/myobject/',
data=json.dumps(dict(name="Another object")),
content_type='application/json')
postResponseJSON = json.loads(response.data.decode())
postedObjectID = postResponseJSON["_id"]
response = self.app.get('/myobject/' + postedObjectID)
responseJSON = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
assert 'Another object' in responseJSON["name"]
def test_getting_non_existent_object(self):
response = self.app.get('/myobject/55f0cbb4236f44b7f0e3cb23')
self.assertEqual(response.status_code, 404)
if __name__ == '__main__':
unittest.main()