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_from_json(): value = 123 json_data = { "__fcn__": "tests.test_marshall.JsonSerialisableEntity", "public": 123 } marshall = Marshall() assert marshall.from_json( json.dumps(json_data)) == JsonSerialisableEntity(public=value)
def test_mapping_from_json(): mapping = {"a": 1, "b": 2} json_data = { "__fcn__": "tests.test_marshall.MappingEntity", "mapping": mapping } marshall = Marshall() assert marshall.from_json(json.dumps(json_data)) == MappingEntity( mapping=immutables.Map(mapping))
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_enum_from_json(): json_data = { "__fcn__": "tests.test_marshall.EnumEntity", "option": { "__fcn__": "tests.test_marshall.Option", "_value_": 2, "_name_": "TWO", }, } marshall = Marshall() assert marshall.from_json( json.dumps(json_data)) == EnumEntity(option=Option.TWO)
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_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_sequence_of_messages_deserialised_from_json(): marshall = Marshall() json_string = json.dumps([ _make_simple_entity("Event One", numbers1), _make_simple_entity("Event Two", numbers2), _make_simple_entity("Event Three", numbers3), ]) assert marshall.from_json(json_string) == [ SimpleTypeEntity(name="Event One", numbers=numbers1), SimpleTypeEntity(name="Event Two", numbers=numbers2), SimpleTypeEntity(name="Event Three", numbers=numbers3), ]
def test_code_deregistration(): marshall = Marshall() fcn = "eventz.marshall.DatetimeCodec" marshall.register_codec(fcn=fcn, codec=DatetimeCodec()) assert marshall.has_codec(fcn) is True marshall.deregister_codec(fcn) assert marshall.has_codec(fcn) is False
def test_new_sequence_of_events_can_be_persisted(json_events, parent_created_event, child_chosen_event): storage_path = str(Path(__file__).absolute().parent) + "/storage" store = EventStoreJsonFile( storage_path=storage_path, marshall=Marshall({"eventz.marshall.DatetimeCodec": DatetimeCodec()}), recreate_storage=True, ) assert store.fetch(parent_id1) == () store.persist(parent_id1, [parent_created_event, child_chosen_event]) with open(f"{storage_path}/{parent_id1}.json", "r+") as json_file: persisted_json = json.load(json_file) assert persisted_json == json.dumps(json_events, sort_keys=True)
def test_sequence_of_events_can_be_read(json_events, parent_created_event, child_chosen_event): # set up the store storage_path = str(Path(__file__).absolute().parent) + "/storage" store = EventStoreJsonFile( storage_path=storage_path, marshall=Marshall({"eventz.marshall.DatetimeCodec": DatetimeCodec()}), recreate_storage=True, ) # insert fixture data into the storage if not os.path.isdir(storage_path): os.mkdir(storage_path) os.chmod(storage_path, 0o777) with open(f"{storage_path}/{parent_id1}.json", "w+") as json_file: json.dump(json_events, json_file) # run test and make assertion assert store.fetch(parent_id1) == (parent_created_event, child_chosen_event)
def test_sequence_of_events_can_be_read(json_events, parent_created_event, child_chosen_event): bucket_name = "TestBucket" region = "us-east-1" store = EventStoreJsonS3( bucket_name=bucket_name, region=region, marshall=Marshall({"eventz.marshall.DatetimeCodec": DatetimeCodec()}), recreate_storage=True, ) # insert fixture data into the storage client = boto3.client("s3", region_name=region) client.put_object(Bucket=bucket_name, Key=parent_id1, Body=json.dumps(json_events)) # run test and make assertion assert store.fetch(parent_id1) == (parent_created_event, child_chosen_event)
def test_new_sequence_of_events_can_be_persisted(json_events, parent_created_event, child_chosen_event): bucket_name = "TestBucket" region = "us-east-1" store = EventStoreJsonS3( bucket_name=bucket_name, region=region, marshall=Marshall({"eventz.marshall.DatetimeCodec": DatetimeCodec()}), recreate_storage=True, ) assert store.fetch(parent_id1) == () store.persist(parent_id1, [parent_created_event, child_chosen_event]) client = boto3.client("s3", region_name=region) obj = client.get_object(Bucket=bucket_name, Key=parent_id1) json_string = obj.get("Body").read().decode("utf-8") persisted_json = json.loads(json_string) assert persisted_json == json_events
def test_single_message_deserialisation_from_json(): marshall = Marshall() json_string = json.dumps(_make_simple_entity("Event One", numbers1)) assert marshall.from_json(json_string) == SimpleTypeEntity( name="Event One", numbers=numbers1)
def test_complex_object_deserialised_from_json(): json_string = json.dumps(_make_complex_entity("Value One", "Value Two")) marshall = Marshall() assert marshall.from_json(json_string) == ComplexTypeEntity( one=ValueType(name="Value One"), two=ValueType(name="Value Two"))
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))