From fd8b95952c3891c402b18d1d35c6a2d263b8c6fb Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 2 Jun 2017 10:01:30 -0700 Subject: [PATCH] add tests for flask.json.tag --- tests/test_basic.py | 47 ++++++++++++++---------------- tests/test_json_tag.py | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 tests/test_json_tag.py diff --git a/tests/test_basic.py b/tests/test_basic.py index e4f68832..108c1409 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -435,34 +435,31 @@ def test_session_special_types(app, client): now = datetime.utcnow().replace(microsecond=0) the_uuid = uuid.uuid4() - @app.after_request - def modify_session(response): - flask.session['m'] = flask.Markup('Hello!') - flask.session['u'] = the_uuid - flask.session['dt'] = now - flask.session['b'] = b'\xff' - flask.session['t'] = (1, 2, 3) - flask.session['spacefirst'] = {' t': 'not-a-tuple'} - flask.session['withunderscores'] = {' t__': 'not-a-tuple'} - flask.session['notadict'] = {' di': 'not-a-dict'} - return response - @app.route('/') def dump_session_contents(): - return pickle.dumps(dict(flask.session)) + flask.session['t'] = (1, 2, 3) + flask.session['b'] = b'\xff' + flask.session['m'] = flask.Markup('') + flask.session['u'] = the_uuid + flask.session['d'] = now + flask.session['t_tag'] = {' t': 'not-a-tuple'} + flask.session['di_t_tag'] = {' t__': 'not-a-tuple'} + flask.session['di_tag'] = {' di': 'not-a-dict'} + return '', 204 - client.get('/') - rv = pickle.loads(client.get('/').data) - assert rv['m'] == flask.Markup('Hello!') - assert type(rv['m']) == flask.Markup - assert rv['dt'] == now - assert rv['u'] == the_uuid - assert rv['b'] == b'\xff' - assert type(rv['b']) == bytes - assert rv['t'] == (1, 2, 3) - assert rv['spacefirst'] == {' t': 'not-a-tuple'} - assert rv['withunderscores'] == {' t__': 'not-a-tuple'} - assert rv['notadict'] == {' di': 'not-a-dict'} + with client: + client.get('/') + s = flask.session + assert s['t'] == (1, 2, 3) + assert type(s['b']) == bytes + assert s['b'] == b'\xff' + assert type(s['m']) == flask.Markup + assert s['m'] == flask.Markup('') + assert s['u'] == the_uuid + assert s['d'] == now + assert s['t_tag'] == {' t': 'not-a-tuple'} + assert s['di_t_tag'] == {' t__': 'not-a-tuple'} + assert s['di_tag'] == {' di': 'not-a-dict'} def test_session_cookie_setting(app): diff --git a/tests/test_json_tag.py b/tests/test_json_tag.py new file mode 100644 index 00000000..b8cb6550 --- /dev/null +++ b/tests/test_json_tag.py @@ -0,0 +1,65 @@ +from datetime import datetime +from uuid import uuid4 + +import pytest + +from flask import Markup +from flask.json.tag import TaggedJSONSerializer, JSONTag + + +@pytest.mark.parametrize("data", ( + {' t': (1, 2, 3)}, + {' t__': b'a'}, + {' di': ' di'}, + {'x': (1, 2, 3), 'y': 4}, + (1, 2, 3), + [(1, 2, 3)], + b'\xff', + Markup(''), + uuid4(), + datetime.utcnow().replace(microsecond=0), +)) +def test_dump_load_unchanged(data): + s = TaggedJSONSerializer() + assert s.loads(s.dumps(data)) == data + + +def test_duplicate_tag(): + class TagDict(JSONTag): + key = ' d' + + s = TaggedJSONSerializer() + pytest.raises(KeyError, s.register, TagDict) + s.register(TagDict, force=True, index=0) + assert isinstance(s.tags[' d'], TagDict) + assert isinstance(s.order[0], TagDict) + + +def test_custom_tag(): + class Foo(object): + def __init__(self, data): + self.data = data + + class TagFoo(JSONTag): + __slots__ = () + key = ' f' + + def check(self, value): + return isinstance(value, Foo) + + def to_json(self, value): + return self.serializer.tag(value.data) + + def to_python(self, value): + return Foo(value) + + s = TaggedJSONSerializer() + s.register(TagFoo) + assert s.loads(s.dumps(Foo('bar'))).data == 'bar' + + +def test_tag_interface(): + t = JSONTag(None) + pytest.raises(NotImplementedError, t.check, None) + pytest.raises(NotImplementedError, t.to_json, None) + pytest.raises(NotImplementedError, t.to_python, None)