-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_flask_utils.py
58 lines (45 loc) · 1.96 KB
/
test_flask_utils.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
import flask
import flask.json.tag
import pytest
import flask_utils
@pytest.mark.parametrize('value', [
{'a': 1, 'b': 2},
])
def test_serializer(value):
serializer = flask.json.tag.TaggedJSONSerializer()
json = serializer.dumps(value)
value_ = serializer.loads(json)
assert value == value_
assert type(value) is type(value_)
def test_ordered_request():
app = flask.Flask(__name__)
with app.test_request_context('/?a=1&a=2&c=4&b=3'):
assert flask.request.args.getlist('a') == ['1', '2']
assert flask.request.args.getlist('c') == ['4']
assert flask.request.args.getlist('b') == ['3']
# prior to Werkzeug 3.1.0, we used OrderedMultiDict to also assert
# list(….items(multi=True)) == [('a', '1'), ('a', '2'), ('c', '4'), ('b', '3')]
# but in fact we don’t care about the order of a/b/c in the args:
# we only need a=1&a=2 to stay in order
# (which the ordinary MultiDict guarantees and we test above)
def test_set_json_support():
app = flask.Flask(__name__)
app.json = flask_utils.SetJSONProvider(app)
with app.test_request_context('/'):
expected = flask.jsonify(['x'])
actual = flask.jsonify(set(['x']))
assert expected.get_data(as_text=True) == actual.get_data(as_text=True)
def test_set_json_support_sorted_ints():
app = flask.Flask(__name__)
app.json = flask_utils.SetJSONProvider(app)
with app.test_request_context('/'):
expected = flask.jsonify([1, 2, 3, 5, 8])
actual = flask.jsonify(set([8, 1, 5, 1, 3, 2]))
assert expected.get_data(as_text=True) == actual.get_data(as_text=True)
def test_set_json_support_sorted_strs():
app = flask.Flask(__name__)
app.json = flask_utils.SetJSONProvider(app)
with app.test_request_context('/'):
expected = flask.jsonify(['a', 'bc', 'def'])
actual = flask.jsonify(set(['def', 'a', 'bc']))
assert expected.get_data(as_text=True) == actual.get_data(as_text=True)