def test_complex_object_serialised_to_json(): entity1 = ComplexTypeEntity( one=ValueType(name="Value One"), two=ValueType(name="Value Two"), ) marshall = Marshall() assert marshall.to_json(entity1) == json.dumps( _make_complex_entity("Value One", "Value Two"))
def test_json_serialisable_class_to_json(): value = 123 entity = JsonSerialisableEntity(public=value) json_data = { "__fcn__": "tests.test_marshall.JsonSerialisableEntity", "public": 123 } marshall = Marshall() assert marshall.to_json(entity) == json.dumps(json_data)
def test_entity_with_custom_datetime_codec_serialised_to_json(): entity_name = "Entity One" dt1 = datetime(2020, 1, 2, 3, 4, 5, 123456) iso_dt1 = "2020-01-02T03:04:05.123456" entity1 = CustomTypeEntity(name=entity_name, timestamp=dt1) marshall = Marshall() marshall.register_codec(fcn="eventz.marshall.DatetimeCodec", codec=DatetimeCodec()) assert marshall.to_json(entity1) == json.dumps( _make_with_datetime_dict(name=entity_name, iso_dt=iso_dt1))
def test_mapping_to_json(): entity = MappingEntity(mapping=immutables.Map(a=1, b=2)) json_data = { "__fcn__": "tests.test_marshall.MappingEntity", "mapping": { "a": 1, "b": 2 } } marshall = Marshall() assert marshall.to_json(entity) == json.dumps(json_data)
def test_sequence_of_messages_serialised_to_json(): list_of_entities = [ SimpleTypeEntity(name="Event One", numbers=numbers1), SimpleTypeEntity(name="Event Two", numbers=numbers2), SimpleTypeEntity(name="Event Three", numbers=numbers3), ] marshall = Marshall() assert marshall.to_json(list_of_entities) == json.dumps([ _make_simple_entity("Event One", numbers1), _make_simple_entity("Event Two", numbers2), _make_simple_entity("Event Three", numbers3), ])
def test_enum_to_json(): entity = EnumEntity(option=Option.TWO) json_data = { "__fcn__": "tests.test_marshall.EnumEntity", "option": { "__fcn__": "tests.test_marshall.Option", "_value_": 2, "_name_": "TWO", }, } marshall = Marshall() assert marshall.to_json(entity) == json.dumps(json_data, sort_keys=True)
def test_single_message_serialisation_to_json(): entity1 = SimpleTypeEntity(name="Event One", numbers=numbers1) marshall = Marshall() assert marshall.to_json(entity1) == json.dumps( _make_simple_entity("Event One", numbers1))