forked from pallets-eco/flask-jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
50 lines (37 loc) · 1.09 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
import os
import unittest
import simplejson as json
from flask import Flask
from flask_jsonschema import JsonSchema, ValidationError
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['JSONSCHEMA_DIR'] = os.path.join(app.root_path, 'schemas')
jsonschema = JsonSchema(app)
@app.route('/books', methods=['POST'])
@jsonschema.validate('books', 'create')
def books():
return 'success'
@app.errorhandler(ValidationError)
def on_error(e):
return 'error'
client = app.test_client()
class JsonSchemaTests(unittest.TestCase):
def test_valid_json(self):
r = client.post(
'/books',
content_type='application/json',
data=json.dumps({
'title': 'Infinite Jest',
'author': 'David Foster Wallace'
})
)
self.assertIn('success', r.data)
def test_invalid_json(self):
r = client.post(
'/books',
content_type='application/json',
data=json.dumps({
'title': 'Infinite Jest'
})
)
self.assertIn('error', r.data)