async def test_dict_event_store_load_stream(): KEY = "<aggregate_id>" db = {KEY: [DummyStoreEvent(1), DummyStoreEvent(2), DummyStoreEvent(3)]} store = events.DictEventStore(db) assert await store.load_stream(KEY) == events.EventStream(version=len( db[KEY]), events=db[KEY])
async def test_dict_event_store_append_to_stream_fail_with_concurrency(): KEY = "<aggregate_id>" event_list = [DummyStoreEvent(1), DummyStoreEvent(2), DummyStoreEvent(3)] db = {} store = events.DictEventStore(db) await store.append_to_stream(global_id=KEY, events=event_list, expect_version=0) with pytest.raises(events.ConcurrentStreamWriteError): await store.append_to_stream(global_id=KEY, events=event_list, expect_version=0)
async def test_dict_event_store_append_to_stream(): KEY = "<aggregate_id>" event_list = [DummyStoreEvent(1), DummyStoreEvent(2), DummyStoreEvent(3)] db = {} store = events.DictEventStore(db) await store.append_to_stream(global_id=KEY, events=event_list[:], expect_version=0) assert db == {KEY: event_list} new_event = DummyStoreEvent(4) await store.append_to_stream(global_id=KEY, events=[new_event], expect_version=len(db[KEY])) assert db == {KEY: [*event_list, new_event]}
def test_dict_event_store_init(): store = events.DictEventStore() assert isinstance(store._db, dict) assert store._db == {}
print(aircraft.global_id, "landing in", airport) return Landed(airport=airport) async def think_reactor(aggregate_id, event, context): await asyncio.sleep(1) print("Did we forget to pack something important?") async def main(_aircrafts): async with aggregates.execute_transaction(_aircrafts) as aircraft: aircraft.execute(schedule, "DAILY2018") aircraft.execute(takeoff) print("Aircraft status:", aircraft) await asyncio.sleep(2) async with aggregates.execute_transaction(_aircrafts, "DAILY2018") as aircraft: aircraft.execute(land, "Paris CDG") print("Aircraft status:", aircraft) if __name__ == "__main__": air_traffic_bus = events.EventBus(registry=FlightEvent.registry) air_traffic_bus.subscribe(think_reactor, TakenOff.topic) aircrafts = AircraftRepository(events.DictEventStore(), bus=air_traffic_bus) air_traffic_bus.listen() loop = asyncio.get_event_loop() loop.run_until_complete(main(aircrafts)) air_traffic_bus.close(timeout=5)