Skip to content
Myr

add tests for flask.json.tag

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 9bee250 · Browse files · 2 files+87 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+87 added−25 removed

  • tests:+87 added−25 removed

2 files+87 added−25 removed

modified:tests/test_basic.py+22 added-25 removed
433 unmodified lines
434 app.secret_key = 'development-key'435 now = datetime.utcnow().replace(microsecond=0)436 the_uuid = uuid.uuid4()437438 @app.after_request439 def modify_session(response):440 flask.session['m'] = flask.Markup('Hello!')441 flask.session['u'] = the_uuid442 flask.session['dt'] = now443 flask.session['b'] = b'\xff'444 flask.session['t'] = (1, 2, 3)445 flask.session['spacefirst'] = {' t': 'not-a-tuple'}446 flask.session['withunderscores'] = {' t__': 'not-a-tuple'}447 flask.session['notadict'] = {' di': 'not-a-dict'}448 return response449438 @app.route('/')439 def dump_session_contents():452 return pickle.dumps(dict(flask.session))440 flask.session['t'] = (1, 2, 3)441 flask.session['b'] = b'\xff'442 flask.session['m'] = flask.Markup('<html>')443 flask.session['u'] = the_uuid444 flask.session['d'] = now445 flask.session['t_tag'] = {' t': 'not-a-tuple'}446 flask.session['di_t_tag'] = {' t__': 'not-a-tuple'}447 flask.session['di_tag'] = {' di': 'not-a-dict'}448 return '', 204449454 client.get('/')455 rv = pickle.loads(client.get('/').data)456 assert rv['m'] == flask.Markup('Hello!')457 assert type(rv['m']) == flask.Markup458 assert rv['dt'] == now459 assert rv['u'] == the_uuid460 assert rv['b'] == b'\xff'461 assert type(rv['b']) == bytes462 assert rv['t'] == (1, 2, 3)463 assert rv['spacefirst'] == {' t': 'not-a-tuple'}464 assert rv['withunderscores'] == {' t__': 'not-a-tuple'}465 assert rv['notadict'] == {' di': 'not-a-dict'}450 with client:451 client.get('/')452 s = flask.session453 assert s['t'] == (1, 2, 3)454 assert type(s['b']) == bytes455 assert s['b'] == b'\xff'456 assert type(s['m']) == flask.Markup457 assert s['m'] == flask.Markup('<html>')458 assert s['u'] == the_uuid459 assert s['d'] == now460 assert s['t_tag'] == {' t': 'not-a-tuple'}461 assert s['di_t_tag'] == {' t__': 'not-a-tuple'}462 assert s['di_tag'] == {' di': 'not-a-dict'}463464465def test_session_cookie_setting(app):466 app.secret_key = 'dev key'
1366 unmodified lines
added:tests/test_json_tag.py+65 added
1from datetime import datetime2from uuid import uuid434import pytest56from flask import Markup7from flask.json.tag import TaggedJSONSerializer, JSONTag8910@pytest.mark.parametrize("data", (11 {' t': (1, 2, 3)},12 {' t__': b'a'},13 {' di': ' di'},14 {'x': (1, 2, 3), 'y': 4},15 (1, 2, 3),16 [(1, 2, 3)],17 b'\xff',18 Markup('<html>'),19 uuid4(),20 datetime.utcnow().replace(microsecond=0),21))22def test_dump_load_unchanged(data):23 s = TaggedJSONSerializer()24 assert s.loads(s.dumps(data)) == data252627def test_duplicate_tag():28 class TagDict(JSONTag):29 key = ' d'3031 s = TaggedJSONSerializer()32 pytest.raises(KeyError, s.register, TagDict)33 s.register(TagDict, force=True, index=0)34 assert isinstance(s.tags[' d'], TagDict)35 assert isinstance(s.order[0], TagDict)363738def test_custom_tag():39 class Foo(object):40 def __init__(self, data):41 self.data = data4243 class TagFoo(JSONTag):44 __slots__ = ()45 key = ' f'4647 def check(self, value):48 return isinstance(value, Foo)4950 def to_json(self, value):51 return self.serializer.tag(value.data)5253 def to_python(self, value):54 return Foo(value)5556 s = TaggedJSONSerializer()57 s.register(TagFoo)58 assert s.loads(s.dumps(Foo('bar'))).data == 'bar'596061def test_tag_interface():62 t = JSONTag(None)63 pytest.raises(NotImplementedError, t.check, None)64 pytest.raises(NotImplementedError, t.to_json, None)65 pytest.raises(NotImplementedError, t.to_python, None)