Browse Source

add tests for flask.json.tag

pull/2352/head
David Lord 7 years ago
parent
commit
fd8b95952c
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
  1. 45
      tests/test_basic.py
  2. 65
      tests/test_json_tag.py

45
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('<html>')
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
with client:
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'}
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('<html>')
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):

65
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('<html>'),
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)
Loading…
Cancel
Save