def test_auto_deserialization(): ejson.cleanup_deserialization_registry() @ejson.register_deserializer(Person) def deserialize_person(data): return Person(data['name'], data['age']) person = ejson.loads( '{"__class__": "tests.test_ejson.Person", "__value__": {"age": 25, "name": "Lincoln"}}') person.should.be.a(Person) person.name.should.be.equals('Lincoln') person.age.should.be.equals(25)
def test_deserialization(): ejson.cleanup_deserialization_registry() @ejson.register_deserializer(Person) def deserialize_person(data): return Person(data['name'], data['age']) obj = ejson.loads('{"age": 25, "name": "Lincoln"}') person = ejson.deserialize(Person, obj) person.should.be.a(Person) person.name.should.be.equals('Lincoln') person.age.should.be.equals(25)
def test_deserialization_registry(): ejson.cleanup_deserialization_registry() dict(ejson.DESERIALIZE_REGISTRY).should.be.equal({}) @ejson.register_deserializer(Person) def deserialize_person(data): return Person(data['name'], data['age']) ejson.DESERIALIZE_REGISTRY.should.have.length_of(1) ejson.DESERIALIZE_REGISTRY[Person].should.be.equal(deserialize_person) # Testing the cleanup for real ejson.cleanup_deserialization_registry() ejson.DESERIALIZE_REGISTRY.should.have.length_of(0)
def test_deserialization_with_no_objects_registered(): ejson.cleanup_deserialization_registry() obj = ejson.loads('{"age": 25, "name": "Lincoln"}') ejson.deserialize.when.called_with(Person, obj).should.throw( TypeError, "There is no deserializer registered to handle instances of 'Person'")